From 27a71ae862a1a812b6d1a3599b4d10b1173af8c8 Mon Sep 17 00:00:00 2001 From: minish Date: Sat, 13 Jul 2024 23:10:52 -0400 Subject: [PATCH] v0.2.4 --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/config.rs | 4 ++++ src/disk.rs | 2 +- src/engine.rs | 8 ++++++++ src/main.rs | 1 + src/new.rs | 4 +++- 7 files changed, 19 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d585118..e44e22f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -246,7 +246,7 @@ checksum = "cf4b9d6a944f767f8e5e0db018570623c85f3d925ac718db4e06d0187adb21c1" [[package]] name = "breeze" -version = "0.2.3" +version = "0.2.4" dependencies = [ "anyhow", "async-recursion", diff --git a/Cargo.toml b/Cargo.toml index aa2a8e2..54b1221 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "breeze" -version = "0.2.3" +version = "0.2.4" edition = "2021" [dependencies] diff --git a/src/config.rs b/src/config.rs index 13615e3..5e53245 100644 --- a/src/config.rs +++ b/src/config.rs @@ -33,6 +33,10 @@ pub struct EngineConfig { /// Configuration for cache system pub cache: CacheConfig, + /// Maximum size of an upload that will be accepted. + /// Files above this size can not be uploaded. + pub max_upload_len: Option, + /// Maximum lifetime of a temporary upload #[serde_as(as = "DurationSeconds")] pub max_temp_lifetime: Duration, diff --git a/src/disk.rs b/src/disk.rs index 3a3fa80..488cd47 100644 --- a/src/disk.rs +++ b/src/disk.rs @@ -60,7 +60,7 @@ impl Disk { /// Create a background I/O task pub async fn start_save(&self, saved_name: &str) -> Sender { // start a task that handles saving files to disk (we can save to cache/disk in parallel that way) - let (tx, mut rx): (Sender, Receiver) = mpsc::channel(1); + let (tx, mut rx): (Sender, Receiver) = mpsc::channel(256); let p = self.path_for(saved_name); diff --git a/src/engine.rs b/src/engine.rs index baac692..ddd02de 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -31,6 +31,9 @@ pub enum ProcessOutcome { /// We give the user their file's URL Success(String), + /// Occurs when an upload exceeds the chosen maximum file size. + UploadTooLarge, + /// Occurs when a temporary upload is too big to fit in the cache. TemporaryUploadTooLarge, @@ -285,6 +288,11 @@ impl Engine { lifetime: Option, keep_exif: bool, ) -> Result { + // if the upload size is greater than our max file size, deny it now + if self.cfg.max_upload_len.is_some_and(|l| provided_len > l) { + return Ok(ProcessOutcome::UploadTooLarge); + } + // if the upload size is smaller than the specified maximum, we use the cache! let use_cache: bool = self.cache.will_use(provided_len); diff --git a/src/main.rs b/src/main.rs index 158ea76..0a1eee0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -68,6 +68,7 @@ async fn main() { .await .expect("failed to bind to given `http.listen_on` address! make sure it's valid, and the port isn't already bound"); + info!("starting server."); axum::serve(listener, app) .with_graceful_shutdown(shutdown_signal()) .await diff --git a/src/new.rs b/src/new.rs index cb1393d..1889398 100644 --- a/src/new.rs +++ b/src/new.rs @@ -81,7 +81,9 @@ pub async fn new( ProcessOutcome::Success(url) => Ok(url), // 413 Payload Too Large - ProcessOutcome::TemporaryUploadTooLarge => Err(StatusCode::PAYLOAD_TOO_LARGE), + ProcessOutcome::UploadTooLarge | ProcessOutcome::TemporaryUploadTooLarge => { + Err(StatusCode::PAYLOAD_TOO_LARGE) + } // 400 Bad Request ProcessOutcome::TemporaryUploadLifetimeTooLong => Err(StatusCode::BAD_REQUEST),