From 5f19b631669717904c3e088bbe740e871eb5075b Mon Sep 17 00:00:00 2001 From: "Aode (lion)" Date: Fri, 8 Apr 2022 13:03:00 -0500 Subject: [PATCH] Extract details processing --- README.md | 13 +++++++- src/main.rs | 95 +++++++++++++++++++---------------------------------- 2 files changed, 45 insertions(+), 63 deletions(-) diff --git a/README.md b/README.md index 865bbf2..0fe78a5 100644 --- a/README.md +++ b/README.md @@ -275,7 +275,18 @@ pict-rs offers the following endpoints: "files": [ { "delete_token": "OxRpM3sf0Y", - "file": "1hJaYfGE01.jpg" + "file": "1hJaYfGE01.jpg", + "details": { + "width": 400, + "height": 400, + "content_type": "image/jpeg", + "created_at": [ + 2020, + 345, + 67376, + 394363487 + ] + } } ], "msg": "ok" diff --git a/src/main.rs b/src/main.rs index 939c54e..1a8b4b0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -80,6 +80,28 @@ static PROCESS_SEMAPHORE: Lazy = Lazy::new(|| { .in_scope(|| Semaphore::new(num_cpus::get().saturating_sub(1).max(1))) }); +async fn ensure_details( + repo: &R, + store: &S, + alias: &Alias, +) -> Result { + let identifier = repo.identifier_from_alias::(alias).await?; + let details = repo.details(&identifier).await?; + + if let Some(details) = details { + debug!("details exist"); + Ok(details) + } else { + debug!("generating new details from {:?}", identifier); + let hint = details_hint(alias); + let new_details = Details::from_store(store.clone(), identifier.clone(), hint).await?; + debug!("storing details for {:?}", identifier); + repo.relate_details(&identifier, &new_details).await?; + debug!("stored"); + Ok(new_details) + } +} + /// Handle responding to succesful uploads #[instrument(name = "Uploaded files", skip(value))] async fn upload( @@ -104,22 +126,7 @@ async fn upload( info!("Uploaded {} as {:?}", image.filename, alias); let delete_token = image.result.delete_token().await?; - let identifier = repo.identifier_from_alias::(alias).await?; - let details = repo.details(&identifier).await?; - - let details = if let Some(details) = details { - debug!("details exist"); - details - } else { - debug!("generating new details from {:?}", identifier); - let hint = details_hint(alias); - let new_details = - Details::from_store((**store).clone(), identifier.clone(), hint).await?; - debug!("storing details for {:?}", identifier); - repo.relate_details(&identifier, &new_details).await?; - debug!("stored"); - new_details - }; + let details = ensure_details(&**repo, &**store, alias).await?; files.push(serde_json::json!({ "file": alias.to_string(), @@ -188,8 +195,9 @@ struct ClaimQuery { /// Claim a backgrounded upload #[instrument(name = "Waiting on upload", skip(repo))] -async fn claim_upload( +async fn claim_upload( repo: web::Data, + store: web::Data, query: web::Query, ) -> Result { let upload_id = Serde::into_inner(query.into_inner().upload_id); @@ -201,11 +209,14 @@ async fn claim_upload( match upload_result { UploadResult::Success { alias, token } => { + let details = ensure_details(&**repo, &**store, &alias).await?; + Ok(HttpResponse::Ok().json(&serde_json::json!({ "msg": "ok", "files": [{ "file": alias.to_string(), "delete_token": token.to_string(), + "details": details, }] }))) } @@ -269,18 +280,7 @@ async fn do_download_inline( let alias = session.alias().expect("alias should exist").to_owned(); let delete_token = session.delete_token().await?; - let identifier = repo.identifier_from_alias::(&alias).await?; - - let details = repo.details(&identifier).await?; - - let details = if let Some(details) = details { - details - } else { - let hint = details_hint(&alias); - let new_details = Details::from_store((**store).clone(), identifier.clone(), hint).await?; - repo.relate_details(&identifier, &new_details).await?; - new_details - }; + let details = ensure_details(&**repo, &**store, &alias).await?; session.disarm(); @@ -430,16 +430,7 @@ async fn process( .await?; if let Some(identifier) = identifier_opt { - let details_opt = repo.details(&identifier).await?; - - let details = if let Some(details) = details_opt { - details - } else { - let hint = details_hint(&alias); - let details = Details::from_store((**store).clone(), identifier.clone(), hint).await?; - repo.relate_details(&identifier, &details).await?; - details - }; + let details = ensure_details(&**repo, &**store, &alias).await?; return ranged_file_resp(&**store, identifier, range, details).await; } @@ -520,18 +511,7 @@ async fn details( ) -> Result { let alias = alias.into_inner(); - let identifier = repo.identifier_from_alias::(&alias).await?; - - let details = repo.details(&identifier).await?; - - let details = if let Some(details) = details { - details - } else { - let hint = details_hint(&alias); - let new_details = Details::from_store((**store).clone(), identifier.clone(), hint).await?; - repo.relate_details(&identifier, &new_details).await?; - new_details - }; + let details = ensure_details(&**repo, &**store, &alias).await?; Ok(HttpResponse::Ok().json(&details)) } @@ -550,16 +530,7 @@ async fn serve( let identifier = repo.identifier_from_alias::(&alias).await?; - let details = repo.details(&identifier).await?; - - let details = if let Some(details) = details { - details - } else { - let hint = details_hint(&alias); - let details = Details::from_store((**store).clone(), identifier.clone(), hint).await?; - repo.relate_details(&identifier, &details).await?; - details - }; + let details = ensure_details(&**repo, &**store, &alias).await?; ranged_file_resp(&**store, identifier, range, details).await } @@ -842,7 +813,7 @@ async fn launch( .route(web::post().to(upload_backgrounded::)), ) .service( - web::resource("/claim").route(web::get().to(claim_upload::)), + web::resource("/claim").route(web::get().to(claim_upload::)), ), ) .service(web::resource("/download").route(web::get().to(download::)))