From 0566e6709c2634b0586bdc6c60cbf8051319f2c0 Mon Sep 17 00:00:00 2001 From: Amneesh Singh Date: Sun, 16 Nov 2025 18:18:56 +0530 Subject: [PATCH] return html only if user accepts it Signed-off-by: Amneesh Singh --- src/main.rs | 51 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index dd05635..cdc3dbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -210,10 +210,7 @@ fn clean_filename(filename: &str) -> String { let mut prev_dash = false; for c in filename.to_lowercase().chars() { - if c.is_ascii_alphanumeric() { - slug.push(c); - prev_dash = false; - } else if c == '.' { + if c.is_ascii_alphanumeric() || c == '.' { slug.push(c); prev_dash = false; } else if !prev_dash { @@ -225,9 +222,18 @@ fn clean_filename(filename: &str) -> String { slug.trim_matches('-').to_string() } -async fn upload(mut payload: Multipart) -> Result { +async fn upload( + headers: HeaderMap, + mut payload: Multipart, +) -> Result { let mut responses = Vec::new(); + let wants_html = headers + .get("accept") + .and_then(|h| h.to_str().ok()) + .map(|a| a.contains("text/html")) + .unwrap_or(false); + if let Ok(ref key) = CONFIG.key { if let Some(field) = payload.next_field().await.unwrap() { if field.name() == Some("key") { @@ -288,13 +294,22 @@ async fn upload(mut payload: Multipart) -> Result .map_err(|_| YamafError::InternalError("Internal i/o error".into()))?; } - responses.push(format!( - r#"{proto}://{host}/{file} (size ~ {size:.2}k)"#, - proto = CONFIG.external_protocol, - host = CONFIG.external_host, - file = filename, - size = written as f64 / 1024 as f64 + let url = format!( + "{proto}://{host}/{file}", + proto = CONFIG.external_protocol, + host = CONFIG.external_host, + file = filename, + ); + + if wants_html { + responses.push(format!( + r#"{url} (size ~ {size:.2}k)"#, + url = url, + size = written as f64 / 1024.0, )); + } else { + responses.push(url); + } } } @@ -302,11 +317,15 @@ async fn upload(mut payload: Multipart) -> Result return Err(YamafError::BadRequest("No files uploaded".into())); } - Ok(Html(format!( - "Here are your file(s):
{}", - responses.join("
") - )) - .into_response()) + if wants_html { + Ok(Html(format!( + "Here are your file(s):
{}", + responses.join("
") + )) + .into_response()) + } else { + Ok(responses.join("\n").into_response()) + } } async fn serve_file(Path(filename): Path) -> Result {