Skip to content

Commit

Permalink
stream body from web-proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
wesl-ee committed May 17, 2024
1 parent ab76d67 commit dce7a36
Show file tree
Hide file tree
Showing 12 changed files with 353 additions and 141 deletions.
369 changes: 292 additions & 77 deletions Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[workspace]
members = ["hooyad", "hooya-gtk", "packages/*"]
resolver = "2"

[profile.release]
opt-level = 3
Expand Down
9 changes: 4 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
FROM rust:1.73.0-alpine as builder
RUN apk add openssl-dev musl-dev protoc ffmpeg
FROM rust:1.78.0-alpine as builder
RUN apk add openssl-dev musl-dev protoc
WORKDIR /wd
COPY . /wd

ENV OPENSSL_DIR=/usr
RUN cargo build --bin hooyad --bin hooya-web-proxy --release

FROM scratch as hooyad
FROM alpine as hooyad
RUN apk add --no-cache ffmpeg
COPY --from=builder /wd/target/release/hooyad /

EXPOSE 8531
CMD ["/hooyad"]

FROM scratch as hooya-web-proxy
COPY --from=builder /wd/target/release/hooya-web-proxy /

EXPOSE 8532
CMD ["/hooya-web-proxy"]
4 changes: 2 additions & 2 deletions hooya-gtk/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"
authors = [ "wesl-ee <[email protected]>" ]

[dependencies]
tonic = { version = "0.9" }
prost = { version = "0.11" }
tonic = { version = "0.11" }
prost = { version = "0.12" }
tokio = { version = "1.0", features = [ "macros", "rt-multi-thread"] }
tokio-stream = { version = "0.1" }
hooya = { path = "../packages/hooya" }
Expand Down
1 change: 1 addition & 0 deletions hooya-gtk/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ async fn request_cid_thumbnail(

let thumbs = match ext_file {
hooya::proto::file::ExtFile::Image(i) => i.thumbnails,
_ => todo!(),
};

let thumbnail = closest_thumbnail(&thumbs, 1280);
Expand Down
7 changes: 4 additions & 3 deletions hooyad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ name = "hooya-web-proxy"
path = "src/web-proxy.rs"

[dependencies]
tonic = { version = "0.9" }
axum = { version = "0.6.20", features = [ "json" ] }
prost = { version = "0.11" }
tonic = { version = "0.11" }
axum = { version = "0.7", features = [ "json" ] }
prost = { version = "0.12" }
tokio = { version = "1.0", features = [ "macros", "rt-multi-thread"] }
tokio-stream = { version = "0.1" }
hooya = { path = "../packages/hooya" }
Expand All @@ -30,5 +30,6 @@ rand = "0.8"
dotenv = "0.15"
anyhow = "1.0"
futures-util = "0.3"
futures = "0.3"
async-stream = "0.3"
serde = { version = "1.0", features = [ "derive" ] }
4 changes: 2 additions & 2 deletions hooyad/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ impl Control for IControl {
let existing_tags = req.tag_query;
let suggest_string = req.suggest_string;

let tag_suggestion = match suggest_string.split_once(":") {
let tag_suggestion = match suggest_string.split_once(':') {
Some((namespace, incomplete_descriptor)) => self
.runtime
.suggest_tags_within_namespace(
Expand All @@ -342,7 +342,7 @@ impl Control for IControl {
.await
.map_err(|e| Status::internal(e.to_string()))?,
None => {
if !existing_tags.is_empty() {
if existing_tags.len() > 0 || suggest_string.len() > 0 {
self.runtime
.suggest_tags_without_namespace(
&existing_tags,
Expand Down
65 changes: 30 additions & 35 deletions hooyad/src/web-proxy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use axum::{
};
use clap::{command, Arg};
use dotenv::dotenv;
use futures_util::TryStreamExt;
use hooya::proto::{
control_client::ControlClient, AllFilesRequest, CidInfoRequest,
CidThumbnailRequest, ContentAtCidRequest, LocalFilePageRequest,
Expand Down Expand Up @@ -64,17 +65,18 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.route("/suggest-tag", get(suggest_tag))
.with_state(state);

axum::Server::bind(
&matches
let listener = tokio::net::TcpListener::bind::<String>(
matches
.get_one::<String>("proxy-endpoint")
.unwrap()
.parse()
.unwrap(),
)
.serve(app.into_make_service())
.await
.unwrap();

axum::serve(listener, app).await.unwrap();

Ok(())
}

Expand All @@ -91,17 +93,14 @@ async fn cid_content(
};

let mut client = state.client;
let mut chunk_stream = client
let chunk_stream = client
.content_at_cid(ContentAtCidRequest { cid: cid.clone() })
.await
.unwrap()
.into_inner();

let mut body = vec![];
while let Some(mut m) = chunk_stream.message().await.unwrap() {
// TODO Stream body
body.append(&mut m.data);
}
.into_inner()
.into_stream()
.and_then(|f| futures::future::ok(FileChunk(f)));
let body = axum::body::Body::from_stream(chunk_stream);

let file_info = client
.cid_info(CidInfoRequest { cid })
Expand Down Expand Up @@ -185,20 +184,16 @@ async fn cid_thumbnail_medium(
.try_into()
.unwrap();

let mut chunk_stream = client
let chunk_stream = client
.cid_thumbnail(CidThumbnailRequest {
source_cid: cid,
long_edge,
})
.await
.unwrap()
.into_inner();

let mut body = vec![];
while let Some(mut m) = chunk_stream.message().await.unwrap() {
// TODO Stream body
body.append(&mut m.data);
}
.into_inner()
.and_then(|f| futures::future::ok(FileChunk(f)));
let body = axum::body::Body::from_stream(chunk_stream);

headers.append(
axum::http::header::CACHE_CONTROL,
Expand Down Expand Up @@ -270,20 +265,16 @@ async fn cid_thumbnail_small(
.try_into()
.unwrap();

let mut chunk_stream = client
let chunk_stream = client
.cid_thumbnail(CidThumbnailRequest {
source_cid: cid,
long_edge,
})
.await
.unwrap()
.into_inner();

let mut body = vec![];
while let Some(mut m) = chunk_stream.message().await.unwrap() {
// TODO Stream body
body.append(&mut m.data);
}
.into_inner()
.and_then(|f| futures::future::ok(FileChunk(f)));
let body = axum::body::Body::from_stream(chunk_stream);

headers.append(
axum::http::header::CACHE_CONTROL,
Expand Down Expand Up @@ -362,20 +353,16 @@ async fn cid_thumbnail(
}
};

let mut chunk_stream = client
let chunk_stream = client
.cid_thumbnail(CidThumbnailRequest {
source_cid: cid,
long_edge,
})
.await
.unwrap()
.into_inner();

let mut body = vec![];
while let Some(mut m) = chunk_stream.message().await.unwrap() {
// TODO Stream body
body.append(&mut m.data);
}
.into_inner()
.and_then(|f| futures::future::ok(FileChunk(f)));
let body = axum::body::Body::from_stream(chunk_stream);

headers.append(
axum::http::header::CACHE_CONTROL,
Expand Down Expand Up @@ -824,3 +811,11 @@ mod proxy_response {
}
}
}

struct FileChunk(hooya::proto::FileChunk);

impl From<FileChunk> for axum::body::Bytes {
fn from(value: FileChunk) -> Self {
value.0.data.into()
}
}
6 changes: 3 additions & 3 deletions packages/hooya/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ edition = "2021"
authors = [ "wesl-ee <[email protected]>" ]

[dependencies]
tonic = { version = "0.9" }
prost = { version = "0.11" }
tonic = { version = "0.11" }
prost = { version = "0.12" }
cid = "0.10"
ring = "0.16"
tokio = { version = "1.0", features = [ "macros", "rt-multi-thread"] }
Expand All @@ -19,4 +19,4 @@ kamadak-exif = "0.5"
serde = { version = "1.0", features = [ "derive" ] }

[build-dependencies]
tonic-build = "0.9"
tonic-build = "0.11"
2 changes: 1 addition & 1 deletion packages/hooya/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ impl From<&str> for proto::Tag {

impl ToString for proto::Tag {
fn to_string(&self) -> String {
vec![self.namespace.clone(), self.descriptor.clone()].join(":")
[self.namespace.clone(), self.descriptor.clone()].join(":")
}
}
24 changes: 12 additions & 12 deletions packages/hooya/src/video.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use anyhow::Result;
use std::{path::PathBuf, process::Command};
use std::{path::Path, process::Command};

pub fn preview(
in_video: &PathBuf,
out_file: &PathBuf,
in_video: &Path,
out_file: &Path,
long_edge: u32,
) -> Result<(u32, u32)> {
let in_video_str = in_video
Expand Down Expand Up @@ -48,7 +48,7 @@ pub fn preview(
* does exactly what I want for now.
*/

command.args(&[
command.args([
"-filter_complex",
&format!(
"[0:v]trim=start={t1}:end={t2},setpts=PTS-STARTPTS,scale={w}:{h}[clip1];[0:v]trim=start={t3}:end={t4},setpts=PTS-STARTPTS,scale={w}:{h}[clip2];[0:v]trim=start={t5}:end={t6},setpts=PTS-STARTPTS,scale={w}:{h}[clip3];[0:v]trim=start={t7}:end={t8},setpts=PTS-STARTPTS,scale={w}:{h}[clip4];[clip1][clip2][clip3][clip4]concat=n=4:v=1:a=0[outv]",
Expand Down Expand Up @@ -82,14 +82,14 @@ pub fn preview(
}
}

pub fn extract_video_metadata(in_video: &PathBuf) -> Result<VideoMetadata> {
pub fn extract_video_metadata(in_video: &Path) -> Result<VideoMetadata> {
let in_video_str = in_video
.to_str()
.ok_or_else(|| anyhow::anyhow!("Invalid input path"))?;

// Get video duration, width, and height using `ffprobe`
let output = Command::new("ffprobe")
.args(&[
.args([
"-v",
"error",
"-select_streams",
Expand All @@ -116,16 +116,16 @@ pub fn extract_video_metadata(in_video: &PathBuf) -> Result<VideoMetadata> {
let mut duration = 0.0;

for line in ffprobe_output.lines() {
if line.starts_with("width=") {
width = line[6..]
if let Some(stripped) = line.strip_prefix("width=") {
width = stripped
.parse::<u32>()
.map_err(|_| anyhow::anyhow!("Invalid width format"))?;
} else if line.starts_with("height=") {
height = line[7..]
} else if let Some(stripped) = line.strip_prefix("height=") {
height = stripped
.parse::<u32>()
.map_err(|_| anyhow::anyhow!("Invalid height format"))?;
} else if line.starts_with("duration=") {
duration = line[9..]
} else if let Some(stripped) = line.strip_prefix("duration=") {
duration = stripped
.parse::<f64>()
.map_err(|_| anyhow::anyhow!("Invalid duration format"))?;
}
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.73.0
1.78.0

0 comments on commit dce7a36

Please sign in to comment.