From 607f07b6e189485d51c9f805d67a23d205077456 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Orhun=20Parmaks=C4=B1z?= Date: Sun, 14 May 2023 01:30:35 +0300 Subject: [PATCH] feat(server): support server-side default expiry time --- config.toml | 1 + src/config.rs | 5 ++++- src/header.rs | 12 ++++++------ src/server.rs | 11 ++++++++++- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/config.toml b/config.toml index 536e592..19fad3a 100644 --- a/config.toml +++ b/config.toml @@ -43,4 +43,5 @@ mime_blacklist = [ "application/java-vm", ] duplicate_files = true +# default_expiry = "1h" delete_expired_files = { enabled = true, interval = "1h" } diff --git a/src/config.rs b/src/config.rs index 9fe9455..6bad254 100644 --- a/src/config.rs +++ b/src/config.rs @@ -62,8 +62,11 @@ pub struct PasteConfig { /// Media type blacklist. #[serde(default)] pub mime_blacklist: Vec, - /// Allow duplicate uploads + /// Allow duplicate uploads. pub duplicate_files: Option, + /// Default expiry time. + #[serde(default, with = "humantime_serde")] + pub default_expiry: Option, /// Delete expired files. pub delete_expired_files: Option, } diff --git a/src/header.rs b/src/header.rs index 6277dbd..3d78e55 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,19 +1,18 @@ -use crate::util; use actix_web::http::header::{ ContentDisposition as ActixContentDisposition, DispositionParam, DispositionType, HeaderMap, }; use actix_web::{error, Error as ActixError}; +use std::time::Duration; /// Custom HTTP header for expiry dates. pub const EXPIRE: &str = "expire"; /// Parses the expiry date from the [`custom HTTP header`](EXPIRE). -pub fn parse_expiry_date(headers: &HeaderMap) -> Result, ActixError> { +pub fn parse_expiry_date(headers: &HeaderMap, time: Duration) -> Result, ActixError> { if let Some(expire_time) = headers.get(EXPIRE).and_then(|v| v.to_str().ok()) { - let timestamp = util::get_system_time()?; let expire_time = humantime::parse_duration(expire_time).map_err(error::ErrorInternalServerError)?; - Ok(timestamp.checked_add(expire_time).map(|t| t.as_millis())) + Ok(time.checked_add(expire_time).map(|t| t.as_millis())) } else { Ok(None) } @@ -62,9 +61,9 @@ impl ContentDisposition { #[cfg(test)] mod tests { use super::*; + use crate::util; use actix_web::http::header::{HeaderName, HeaderValue}; use std::thread; - use std::time::Duration; #[test] fn test_content_disposition() -> Result<(), ActixError> { @@ -97,7 +96,8 @@ mod tests { HeaderName::from_static(EXPIRE), HeaderValue::from_static("5ms"), ); - let expiry_time = parse_expiry_date(&headers)?.unwrap_or_default(); + let time = util::get_system_time()?; + let expiry_time = parse_expiry_date(&headers, time)?.unwrap_or_default(); assert!(expiry_time > util::get_system_time()?.as_millis()); thread::sleep(Duration::from_millis(10)); assert!(expiry_time < util::get_system_time()?.as_millis()); diff --git a/src/server.rs b/src/server.rs index 39a2c2f..317c3b4 100644 --- a/src/server.rs +++ b/src/server.rs @@ -160,7 +160,16 @@ async fn upload( .as_ref() .cloned()), )?; - let expiry_date = header::parse_expiry_date(request.headers())?; + let time = util::get_system_time()?; + let mut expiry_date = header::parse_expiry_date(request.headers(), time)?; + if expiry_date.is_none() { + expiry_date = config + .read() + .map_err(|_| error::ErrorInternalServerError("cannot acquire config"))? + .paste + .default_expiry + .and_then(|v| time.checked_add(v).map(|t| t.as_millis())); + } let mut urls: Vec = Vec::new(); while let Some(item) = payload.next().await { let mut field = item?;