From c885a71295c7f22f6ba022f55671edac55e0bfbc Mon Sep 17 00:00:00 2001 From: kieran Date: Fri, 7 Feb 2025 09:50:46 +0000 Subject: [PATCH] feat: return thumbnail url in meta --- src/routes/mod.rs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/routes/mod.rs b/src/routes/mod.rs index 976aeee..a9b6c9a 100644 --- a/src/routes/mod.rs +++ b/src/routes/mod.rs @@ -59,22 +59,27 @@ struct PagedResult { impl Nip94Event { pub fn from_upload(settings: &Settings, upload: &FileUpload) -> Self { let hex_id = hex::encode(&upload.id); + let ext = if upload.mime_type != "application/octet-stream" { + mime2ext::mime2ext(&upload.mime_type) + } else { + None + }; let mut tags = vec![ vec![ "url".to_string(), - format!( - "{}/{}{}", - &settings.public_url, - &hex_id, - mime2ext::mime2ext(&upload.mime_type) - .map(|m| format!(".{m}")) - .unwrap_or("".to_string()) - ), + format!("{}/{}.{}", &settings.public_url, &hex_id, ext.unwrap_or("")), ], - vec!["x".to_string(), hex_id], + vec!["x".to_string(), hex_id.clone()], vec!["m".to_string(), upload.mime_type.clone()], vec!["size".to_string(), upload.size.to_string()], ]; + if upload.mime_type.starts_with("image/") || upload.mime_type.starts_with("video/") { + tags.push(vec![ + "thumb".to_string(), + format!("{}/thumb/{}.webp", &settings.public_url, &hex_id), + ]); + } + if let Some(bh) = &upload.blur_hash { tags.push(vec!["blurhash".to_string(), bh.clone()]); } @@ -402,6 +407,10 @@ pub async fn get_blob_thumb( return Err(Status::NotFound); }; + if !(info.mime_type.starts_with("image/") || info.mime_type.starts_with("video/")) { + return Err(Status::NotFound); + } + let file_path = fs.get(&id); let mut thumb_file = temp_dir().join(format!("thumb_{}", sha256));