From 0e4532828a9bb96ae5631423a3a750566b7bc96d Mon Sep 17 00:00:00 2001 From: Paul Z Date: Fri, 20 Oct 2023 14:18:23 +0200 Subject: [PATCH] multipart ttl --- src/main.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 032785c..7ac3f52 100644 --- a/src/main.rs +++ b/src/main.rs @@ -226,6 +226,8 @@ async fn post_item( let mut etag_hasher = Sha3_256::new(); let mut size: u64 = 0; + let mut ttl = params.ttl.unwrap_or(24 * 3600); + match data { MultipartOrStream::Stream(mut stream) => { while let Some(chunk) = stream.next().await { @@ -241,12 +243,13 @@ async fn post_item( }; } MultipartOrStream::Multipart(mut multipart) => { + let mut file_read = false; while let Some(mut field) = multipart .next_field() .await .map_err(|_| Error::InvalidMultipart)? { - if field.name().unwrap_or_default() == "file" { + if field.name().unwrap_or_default() == "file" && !file_read { while let Some(chunk) = field.chunk().await.unwrap_or_default() { let mut buf = chunk.to_vec(); etag_hasher.update(&buf); @@ -260,7 +263,14 @@ async fn post_item( .map(|x| x.to_string()) .unwrap_or("application/octet-stream".to_string()), ); - break; + file_read = true; + } + if field.name().unwrap_or_default() == "ttl" { + if let Some(mp_ttl) = + field.text().await.ok().and_then(|x| x.parse::().ok()) + { + ttl = mp_ttl; + } } } } @@ -268,7 +278,6 @@ async fn post_item( writer.flush().await?; let now = SystemTime::now().duration_since(UNIX_EPOCH)?.as_secs(); - let ttl = params.ttl.unwrap_or(24 * 3600); let expires_at = match u64::MAX - ttl > now { true => now + ttl, false => u64::MAX,