From 24d3b289da085261966fb338113610905dfca8c9 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Fri, 12 May 2023 16:11:29 +0200 Subject: [PATCH] Properly handle Content-Type --- plume-models/src/medias.rs | 24 +++++++++++++++--------- src/routes/medias.rs | 16 ++++++++-------- src/routes/mod.rs | 10 ++++++---- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/plume-models/src/medias.rs b/plume-models/src/medias.rs index 8c6fecd3..8468caf3 100644 --- a/plume-models/src/medias.rs +++ b/plume-models/src/medias.rs @@ -294,6 +294,8 @@ impl Media { #[cfg(feature = "s3")] { + use rocket::http::ContentType; + let dest = determine_mirror_s3_path(&remote_url); let media = request::get( @@ -301,18 +303,22 @@ impl Media { User::get_sender(), CONFIG.proxy().cloned(), )?; - let content_type = media.headers().get(reqwest::header::CONTENT_TYPE).cloned(); + + let content_type = media + .headers() + .get(reqwest::header::CONTENT_TYPE) + .and_then(|x| x.to_str().ok()) + .and_then(ContentType::parse_flexible) + .unwrap_or(ContentType::Binary); + let bytes = media.bytes()?; let bucket = CONFIG.s3.as_ref().unwrap().get_bucket(); - match content_type.as_ref().and_then(|x| x.to_str().ok()) { - Some(ct) => { - bucket.put_object_with_content_type_blocking(&dest, &bytes, ct)?; - } - None => { - bucket.put_object_blocking(&dest, &bytes)?; - } - } + bucket.put_object_with_content_type_blocking( + &dest, + &bytes, + &content_type.to_string() + )?; dest } diff --git a/src/routes/medias.rs b/src/routes/medias.rs index b9a22dee..05119d23 100644 --- a/src/routes/medias.rs +++ b/src/routes/medias.rs @@ -131,14 +131,14 @@ fn save_uploaded_file(file: &SavedField) -> Result, plume_models: }; let bucket = CONFIG.s3.as_ref().unwrap().get_bucket(); - match &file.headers.content_type { - Some(ct) => { - bucket.put_object_with_content_type_blocking(&dest, &bytes, &ct.to_string())?; - } - None => { - bucket.put_object_blocking(&dest, &bytes)?; - } - } + let content_type = match &file.headers.content_type { + Some(ct) => ct.to_string(), + None => ContentType::from_extension(&ext) + .unwrap_or(ContentType::Binary) + .to_string(), + }; + + bucket.put_object_with_content_type_blocking(&dest, &bytes, &content_type)?; Ok(Some(dest)) } diff --git a/src/routes/mod.rs b/src/routes/mod.rs index ac016b9d..5b4adb6f 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -269,13 +269,15 @@ pub fn plume_media_files(file: PathBuf) -> Option { #[cfg(feature="s3")] { - let ct = file.extension() - .and_then(|ext| ContentType::from_extension(&ext.to_string_lossy())) - .unwrap_or(ContentType::Binary); - let data = CONFIG.s3.as_ref().unwrap().get_bucket() .get_object_blocking(format!("static/media/{}", file.to_string_lossy())).ok()?; + let ct = data.headers().get("content-type") + .and_then(|x| ContentType::parse_flexible(&x)) + .or_else(|| file.extension() + .and_then(|ext| ContentType::from_extension(&ext.to_string_lossy()))) + .unwrap_or(ContentType::Binary); + Some(CachedFile { inner: FileKind::S3 ( data.to_vec(), ct), cache_control: CacheControl(vec![CacheDirective::MaxAge(60 * 60 * 24 * 30)]),