From aaa364baafc920cb4aea05c1cb67bb9a7b1ecbf7 Mon Sep 17 00:00:00 2001 From: dw5 <29355584+dw5@users.noreply.github.com> Date: Thu, 9 May 2024 13:51:01 +0300 Subject: [PATCH] ... --- src/main.rs | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-) diff --git a/src/main.rs b/src/main.rs index 562591c..6a006ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -518,26 +518,30 @@ impl MainHandler { // in a new temporary directory under the OS temporary directory. match multipart.save().size_limit(self.upload_size_limit).temp() { SaveResult::Full(entries) => { - for (_, fields) in entries.fields { - for field in fields { - let mut data = field.data.readable().unwrap(); - let headers = &field.headers; - let mut target_path = path.clone(); - - target_path.push(headers.filename.clone().unwrap()); - if let Err(errno) = std::fs::File::create(target_path) - .and_then(|mut file| io::copy(&mut data, &mut file)) - { - return Err(( - status::InternalServerError, - format!("Copy file failed: {}", errno), - )); - } else { - println!( - " >> File saved: {}", - headers.filename.clone().unwrap() - ); - } + + // Grab all the fields named files + let files_fields = match entries.fields.get("files") { + Some(fields) => fields, + None => { + return Err((status::BadRequest, String::from("no files provided"))) + } + }; + + for field in files_fields { + let mut data = field.data.readable().unwrap(); + let headers = &field.headers; + let mut target_path = path.to_owned(); + + target_path.push(headers.filename.clone().unwrap()); + if let Err(errno) = std::fs::File::create(target_path) + .and_then(|mut file| io::copy(&mut data, &mut file)) + { + return Err(( + status::InternalServerError, + format!("Copy file failed: {}", errno), + )); + } else { + println!(" >> File saved: {}", headers.filename.clone().unwrap()); } } Ok(())