From 6d7908f14439a7b84371cc1d7bb1583de190bbbb Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sat, 2 Aug 2025 15:58:58 +0800 Subject: [PATCH 1/7] fix: handle missing filename in file upload to prevent panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Replace unwrap() calls with proper error handling when processing uploaded files. When no filename is provided, the server now logs a warning and continues processing instead of panicking. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main.rs | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8fba822..085ebd1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -583,8 +583,22 @@ impl MainHandler { let headers = &field.headers; let mut target_path = path.to_owned(); - let raw_filename = headers.filename.clone().unwrap(); - let filename = Path::new(&raw_filename).file_name().unwrap(); + let raw_filename = match headers.filename.clone() { + Some(name) => name, + None => { + println!("[Warning]: Skipping field with no filename"); + continue; + } + }; + + let filename = match Path::new(&raw_filename).file_name() { + Some(name) => name, + None => { + println!("[Warning]: Invalid filename: {}", raw_filename); + continue; + } + }; + target_path.push(filename); if let Err(errno) = std::fs::File::create(target_path) .and_then(|mut file| io::copy(&mut data, &mut file)) @@ -594,7 +608,7 @@ impl MainHandler { format!("Copy file failed: {errno}"), )); } else { - println!(" >> File saved: {}", headers.filename.clone().unwrap()); + println!(" >> File saved: {}", raw_filename); } } Ok(()) From 0d1b5676e18c5c3ad8e1064fd4add8def36c4708 Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sat, 2 Aug 2025 16:15:40 +0800 Subject: [PATCH 2/7] fix: handle missing filename in file upload to prevent panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace unwrap() calls with proper error handling when processing uploaded files - Use if-let pattern for filename validation as requested - When no filename is provided, server now logs warning and continues instead of panicking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 085ebd1..5e5184d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -591,12 +591,11 @@ impl MainHandler { } }; - let filename = match Path::new(&raw_filename).file_name() { - Some(name) => name, - None => { - println!("[Warning]: Invalid filename: {}", raw_filename); - continue; - } + let filename = if let Some(name) = Path::new(&raw_filename).file_name() { + name + } else { + println!("[Warning]: Invalid filename: {}", raw_filename); + continue; }; target_path.push(filename); From b1a5f1713b6ab9b302b54ac1aaa0c7860f97c632 Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sat, 2 Aug 2025 16:20:15 +0800 Subject: [PATCH 3/7] fix: handle missing filename in file upload to prevent panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace unwrap() calls with proper error handling when processing uploaded files - Use if-let-else pattern for both filename extraction and validation - When no filename is provided, server now logs warning and continues instead of panicking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main.rs | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/main.rs b/src/main.rs index 5e5184d..1b24903 100644 --- a/src/main.rs +++ b/src/main.rs @@ -583,12 +583,11 @@ impl MainHandler { let headers = &field.headers; let mut target_path = path.to_owned(); - let raw_filename = match headers.filename.clone() { - Some(name) => name, - None => { - println!("[Warning]: Skipping field with no filename"); - continue; - } + let raw_filename = if let Some(name) = headers.filename.clone() { + name + } else { + println!("[Warning]: Skipping field with no filename"); + continue; }; let filename = if let Some(name) = Path::new(&raw_filename).file_name() { @@ -1086,3 +1085,6 @@ impl MainHandler { Ok(resp) } } + + + From e881b85f4a386b99400627f1dfb7deed1e814a02 Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sat, 2 Aug 2025 16:21:37 +0800 Subject: [PATCH 4/7] fix: handle missing filename in file upload to prevent panic MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace unwrap() calls with proper error handling when processing uploaded files - Use if-let-else pattern for both filename extraction and validation - When no filename is provided, server now logs warning and continues instead of panicking 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- src/main.rs | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 1b24903..6bc7e22 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1085,6 +1085,3 @@ impl MainHandler { Ok(resp) } } - - - From 150118ffd3f48a9fa7e0b7f00c82b999ae39db64 Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sun, 3 Aug 2025 12:50:02 +0800 Subject: [PATCH 5/7] fix: handle missing filename in file upload to prevent panic - Replace unsafe unwrap() calls with safe if-let-else pattern - Add proper validation for missing and invalid filenames - Skip malformed uploads with warning messages instead of panicking - Use modern Rust idioms for cleaner error handling Addresses potential crashes when users upload files without proper filename headers or with invalid path names. --- src/main.rs | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/main.rs b/src/main.rs index 6bc7e22..7cb4b7a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -583,17 +583,13 @@ impl MainHandler { let headers = &field.headers; let mut target_path = path.to_owned(); - let raw_filename = if let Some(name) = headers.filename.clone() { - name - } else { + let Some(raw_filename) = headers.filename.clone() else { println!("[Warning]: Skipping field with no filename"); continue; }; - let filename = if let Some(name) = Path::new(&raw_filename).file_name() { - name - } else { - println!("[Warning]: Invalid filename: {}", raw_filename); + let Some(filename) = Path::new(&raw_filename).file_name() else { + println!("[Warning]: Invalid filename: {raw_filename}"); continue; }; From d41230048e84a94dd3e7c1f7bd784677bdb9dbbb Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sun, 3 Aug 2025 13:02:26 +0800 Subject: [PATCH 6/7] fix: handle missing filename in file upload to prevent panic - Replace unsafe unwrap() calls with safe if-let-else pattern - Add proper validation for missing and invalid filenames - Skip malformed uploads with warning messages instead of panicking - Use modern Rust idioms for cleaner error handling Addresses potential crashes when users upload files without proper filename headers or with invalid path names. --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 7cb4b7a..8053c23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -602,7 +602,7 @@ impl MainHandler { format!("Copy file failed: {errno}"), )); } else { - println!(" >> File saved: {}", raw_filename); + println!(" >> File saved: {raw_filename}"); } } Ok(()) From 3b42c826101994db527e0ab3a3aa3989fe7b0794 Mon Sep 17 00:00:00 2001 From: yuancjun <153783375+yuancjun@users.noreply.github.com> Date: Sun, 3 Aug 2025 15:05:07 +0800 Subject: [PATCH 7/7] Format main.rs --- src/main.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 8053c23..a5fb1af 100644 --- a/src/main.rs +++ b/src/main.rs @@ -587,12 +587,12 @@ impl MainHandler { println!("[Warning]: Skipping field with no filename"); continue; }; - + let Some(filename) = Path::new(&raw_filename).file_name() else { println!("[Warning]: Invalid filename: {raw_filename}"); continue; }; - + target_path.push(filename); if let Err(errno) = std::fs::File::create(target_path) .and_then(|mut file| io::copy(&mut data, &mut file))