From 07c61cbe87b924122db57782a482c3b0cf9bd91a Mon Sep 17 00:00:00 2001 From: "Aode (lion)" Date: Thu, 7 Apr 2022 19:07:30 -0500 Subject: [PATCH] Add download backgrounded --- src/main.rs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/src/main.rs b/src/main.rs index 3a61984..ec5ede3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -272,6 +272,41 @@ async fn download( }))) } +#[instrument(name = "Downloading file for background", skip(client))] +async fn download_backgrounded( + client: web::Data, + repo: web::Data, + store: web::Data, + query: web::Query, +) -> Result { + let res = client.get(&query.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"); + let identifier = backgrounded + .identifier() + .expect("Identifier exists") + .to_bytes()?; + + queue::queue_ingest(&**repo, identifier, upload_id, None, true).await?; + + Ok(HttpResponse::Accepted().json(&serde_json::json!({ + "msg": "ok", + "uploads": [{ + "upload_id": upload_id.to_string(), + }] + }))) +} + /// cache an image from a URL #[instrument(name = "Caching file", skip(client, repo))] async fn cache( @@ -834,6 +869,10 @@ async fn launch( ), ) .service(web::resource("/download").route(web::get().to(download::))) + .service( + web::resource("/download_backgrounded") + .route(web::get().to(download_backgrounded::)), + ) .service(web::resource("/cache").route(web::get().to(cache::))) .service( web::resource("/delete/{delete_token}/{filename}")