From 12c0d71958572616e481fbd859ae8c29e6973e61 Mon Sep 17 00:00:00 2001 From: Jens Reimann Date: Wed, 12 Jun 2024 17:15:12 +0200 Subject: [PATCH] fix: translate the id into a hash before fetching from the storage --- .../fundamental/src/advisory/endpoints/mod.rs | 26 ++++++++++++++----- 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/modules/fundamental/src/advisory/endpoints/mod.rs b/modules/fundamental/src/advisory/endpoints/mod.rs index 6f5081775..55bb42b2e 100644 --- a/modules/fundamental/src/advisory/endpoints/mod.rs +++ b/modules/fundamental/src/advisory/endpoints/mod.rs @@ -102,11 +102,11 @@ struct UploadParams { #[utoipa::path( tag = "advisory", context_path = "/api", - request_body = Vec , - params( UploadParams ), + request_body = Vec, + params(UploadParams), responses( - (status = 201, description = "Upload a file"), - (status = 400, description = "The file could not be parsed as an advisory"), + (status = 201, description = "Upload a file"), + (status = 400, description = "The file could not be parsed as an advisory"), ) )] #[post("/v1/advisory")] @@ -135,12 +135,26 @@ pub async fn upload( )] #[get("/v1/advisory/{key}/download")] pub async fn download( - service: web::Data, + ingestor: web::Data, + advisory: web::Data, key: web::Path, ) -> Result { let hash_key = Id::from_str(&key).map_err(Error::HashKey)?; - let stream = service + let Some(adv) = advisory.fetch_advisory(hash_key, ()).await? else { + return Ok(HttpResponse::NotFound().finish()); + }; + + let Some(hash) = adv.head.hashes.into_iter().find_map(|hash| match hash { + Id::Sha256(hash) => Some(hash), + _ => None, + }) else { + return Ok(HttpResponse::NotFound().finish()); + }; + + let hash_key = Id::Sha256(hash); + + let stream = ingestor .get_ref() .storage() .clone()