From 69d8bd1d8e4ff5fcb74e5dc8ff5ce22ba691b942 Mon Sep 17 00:00:00 2001 From: Paul Z Date: Fri, 20 Oct 2023 13:58:44 +0200 Subject: [PATCH] browser upload, multipart, ttl --- src/item_explanation.md | 2 +- src/main.rs | 33 +++++++++++++++++++++++++-------- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/item_explanation.md b/src/item_explanation.md index b18b8a4..fb1233c 100644 --- a/src/item_explanation.md +++ b/src/item_explanation.md @@ -15,7 +15,7 @@ After uploading data you can access it by accessing with an optional f ## Pipe into curl `$ tar -cz my-files | curl -H "Content-Type: application/gzip" -T - ` -## Enryption +## Encryption ``` $ tar -cz my-files | gpg -co tmp.tar.gz $ curl -H "Content-Type: application/octet-stream" -T tmp.tar.gz diff --git a/src/main.rs b/src/main.rs index e637a8f..032785c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -224,7 +224,7 @@ async fn post_item( let mut writer = BufWriter::new(file); let mut etag_hasher = Sha3_256::new(); - let mut size = 0; + let mut size: u64 = 0; match data { MultipartOrStream::Stream(mut stream) => { @@ -235,6 +235,10 @@ async fn post_item( cipher.apply_keystream(&mut buf); writer.write_all(&buf).await?; } + metadata.content_type = match content_type { + Some(content_type) => Some(content_type.to_string()), + None => Some("application/octet-stream".to_string()), + }; } MultipartOrStream::Multipart(mut multipart) => { while let Some(mut field) = multipart @@ -250,6 +254,13 @@ async fn post_item( cipher.apply_keystream(&mut buf); writer.write_all(&buf).await?; } + metadata.content_type = Some( + field + .content_type() + .map(|x| x.to_string()) + .unwrap_or("application/octet-stream".to_string()), + ); + break; } } } @@ -259,16 +270,13 @@ async fn post_item( 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 => u64::MAX, - false => now + ttl, + true => now + ttl, + false => u64::MAX, }; metadata.etag = Some(hex::encode(etag_hasher.finalize())); metadata.size = Some(size); - metadata.content_type = match content_type { - Some(content_type) => Some(content_type.to_string()), - None => Some("application/octet-stream".to_string()), - }; + metadata.expires_at = expires_at; metadata.to_file(&metadata_path).await?; @@ -316,6 +324,11 @@ async fn get_item(
{raw!(body.as_str())} +

{"Browser upload"}

+
+ {} + +
@@ -371,7 +384,11 @@ where let is_multipart = req .headers() .get(CONTENT_TYPE) - .map(|x| x == "multipart/form-data") + .and_then(|x| { + x.to_str() + .ok() + .map(|y| y.starts_with("multipart/form-data")) + }) .unwrap_or_default(); if is_multipart { Ok(Self::Multipart(