From 7436f15267a814692249b97a7bdb78e8b9a33b09 Mon Sep 17 00:00:00 2001 From: "Aode (lion)" Date: Fri, 8 Apr 2022 13:16:22 -0500 Subject: [PATCH] Dry download client logic --- src/main.rs | 46 +++++++++++++++++----------------------------- 1 file changed, 17 insertions(+), 29 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1a8b4b0..3f01fef 100644 --- a/src/main.rs +++ b/src/main.rs @@ -250,22 +250,7 @@ async fn download( store: web::Data, query: web::Query, ) -> Result { - if query.backgrounded { - do_download_backgrounded(client, repo, store, &query.url, query.ephemeral).await - } else { - do_download_inline(client, repo, store, &query.url, query.ephemeral).await - } -} - -#[instrument(name = "Downloading file inline", skip(client, repo))] -async fn do_download_inline( - client: web::Data, - repo: web::Data, - store: web::Data, - url: &str, - is_cached: bool, -) -> Result { - let res = client.get(url).send().await?; + let res = client.get(&query.url).send().await?; if !res.status().is_success() { return Err(UploadError::Download(res.status()).into()); @@ -275,6 +260,20 @@ async fn do_download_inline( .map_err(Error::from) .limit((CONFIG.media.max_file_size * MEGABYTES) as u64); + if query.backgrounded { + do_download_backgrounded(stream, repo, store, query.ephemeral).await + } else { + do_download_inline(stream, repo, store, query.ephemeral).await + } +} + +#[instrument(name = "Downloading file inline", skip(stream))] +async fn do_download_inline( + stream: impl Stream>, + repo: web::Data, + store: web::Data, + is_cached: bool, +) -> Result { let mut session = ingest::ingest(&**repo, &**store, stream, None, true, is_cached).await?; let alias = session.alias().expect("alias should exist").to_owned(); @@ -294,24 +293,13 @@ async fn do_download_inline( }))) } -#[instrument(name = "Downloading file in background", skip(client))] +#[instrument(name = "Downloading file in background", skip(stream))] async fn do_download_backgrounded( - client: web::Data, + stream: impl Stream>, repo: web::Data, store: web::Data, - url: &str, is_cached: bool, ) -> Result { - let res = client.get(url).send().await?; - - if !res.status().is_success() { - return Err(UploadError::Download(res.status()).into()); - } - - let stream = res - .map_err(Error::from) - .limit((CONFIG.media.max_file_size * MEGABYTES) as u64); - let backgrounded = Backgrounded::proxy((**repo).clone(), (**store).clone(), stream).await?; let upload_id = backgrounded.upload_id().expect("Upload ID exists");