From 4cca7d0f864599450d13a8732f8502f9bee7eab0 Mon Sep 17 00:00:00 2001 From: asonix Date: Sun, 23 Jul 2023 12:10:36 -0500 Subject: [PATCH] Prepare for proxying media --- src/lib.rs | 112 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 87 insertions(+), 25 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index cef18a4..daf3334 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -638,7 +638,14 @@ async fn process_details( ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => { Serde::into_inner(alias) } - ProcessSource::Proxy { proxy } => todo!("proxy URL"), + ProcessSource::Proxy { proxy } => { + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().json(&serde_json::json!({ + "msg": "No images associated with provided proxy url" + }))); + }; + alias + } }; let (_, thumbnail_path, _) = prepare_process(&config, operations, ext.as_str())?; @@ -705,7 +712,13 @@ async fn process( ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => { Serde::into_inner(alias) } - ProcessSource::Proxy { proxy } => todo!("proxy URL"), + ProcessSource::Proxy { proxy } => { + if let Some(alias) = repo.related(proxy).await? { + alias + } else { + todo!("proxy URL") + } + } }; let (format, thumbnail_path, thumbnail_args) = @@ -836,7 +849,12 @@ async fn process_head( ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => { Serde::into_inner(alias) } - ProcessSource::Proxy { proxy } => todo!("proxy URL"), + ProcessSource::Proxy { proxy } => { + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias + } }; let (_, thumbnail_path, _) = prepare_process(&config, operations, ext.as_str())?; @@ -904,7 +922,12 @@ async fn process_backgrounded( ProcessSource::Alias { alias } | ProcessSource::Source { src: alias } => { Serde::into_inner(alias) } - ProcessSource::Proxy { proxy } => todo!("proxy URL"), + ProcessSource::Proxy { proxy } => { + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias + } }; let (target_format, process_path, process_args) = @@ -942,9 +965,14 @@ async fn details_query( config: web::Data, ) -> Result { let alias = match alias_query { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().json(&serde_json::json!({ + "msg": "Provided proxy URL has not been cached", + }))) + }; + alias } }; @@ -959,11 +987,11 @@ async fn details( store: web::Data, config: web::Data, ) -> Result { - do_details(alias.into_inner(), repo, store, config).await + do_details(Serde::into_inner(alias.into_inner()), repo, store, config).await } async fn do_details( - alias: Serde, + alias: Alias, repo: web::Data, store: web::Data, config: web::Data, @@ -983,9 +1011,13 @@ async fn serve_query( config: web::Data, ) -> Result { let alias = match alias_query { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + if let Some(alias) = repo.related(proxy).await? { + alias + } else { + todo!("proxy URL") + } } }; @@ -1001,18 +1033,25 @@ async fn serve( store: web::Data, config: web::Data, ) -> Result { - do_serve(range, alias.into_inner(), repo, store, config).await + do_serve( + range, + Serde::into_inner(alias.into_inner()), + repo, + store, + config, + ) + .await } async fn do_serve( range: Option>, - alias: Serde, + alias: Alias, repo: web::Data, store: web::Data, config: web::Data, ) -> Result { let (hash, alias, not_found) = if let Some(hash) = repo.hash(&alias).await? { - (hash, Serde::into_inner(alias), false) + (hash, alias, false) } else { let Some((alias, hash)) = not_found_hash(&repo).await? else { return Ok(HttpResponse::NotFound().finish()); @@ -1050,9 +1089,12 @@ async fn serve_query_head( config: web::Data, ) -> Result { let alias = match alias_query { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias } }; @@ -1067,12 +1109,19 @@ async fn serve_head( store: web::Data, config: web::Data, ) -> Result { - do_serve_head(range, alias.into_inner(), repo, store, config).await + do_serve_head( + range, + Serde::into_inner(alias.into_inner()), + repo, + store, + config, + ) + .await } async fn do_serve_head( range: Option>, - alias: Serde, + alias: Alias, repo: web::Data, store: web::Data, config: web::Data, @@ -1255,9 +1304,13 @@ async fn set_not_found( } let alias = match json.into_inner() { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + if let Some(alias) = repo.related(proxy).await? { + alias + } else { + todo!("proxy URL") + } } }; @@ -1285,9 +1338,12 @@ async fn purge( } let alias = match alias_query { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias } }; @@ -1312,9 +1368,12 @@ async fn aliases( repo: web::Data, ) -> Result { let alias = match alias_query { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias } }; @@ -1332,9 +1391,12 @@ async fn identifier( repo: web::Data, ) -> Result { let alias = match alias_query { - AliasQuery::Alias { alias } => alias, + AliasQuery::Alias { alias } => Serde::into_inner(alias), AliasQuery::Proxy { proxy } => { - todo!("Proxy URL") + let Some(alias) = repo.related(proxy).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + alias } };