From a02dc8b7faf7ecce9e08ae0f5a3d0ee083267273 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 20 Jun 2020 14:06:59 +0200 Subject: [PATCH] Can check if a file is present in the request --- src/controllers/conversations_controller.rs | 5 +++++ src/controllers/server.rs | 4 ++-- src/data/http_request_handler.rs | 7 ++++++- 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index 9b549b3..dc92024 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -233,5 +233,10 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult { /// Send a new message pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult { + + if r.has_file("image") { + println!("File is detected."); + } + r.success("implement me") } \ No newline at end of file diff --git a/src/controllers/server.rs b/src/controllers/server.rs index 166f4e5..f0a4e50 100644 --- a/src/controllers/server.rs +++ b/src/controllers/server.rs @@ -150,12 +150,12 @@ impl FromRequest for CustomRequest { let mut field = el?; let content_type = field.content_disposition().ok_or( - ErrorInternalServerError("F1"))?; + ErrorInternalServerError("Missing content type"))?; let name = content_type.get_name().ok_or( ErrorInternalServerError("Missing field name!"))?; // Handle file upload - if content_type.get_filename().is_some() && name.eq("file") { + if content_type.get_filename().is_some() { let filename = content_type.get_filename().unwrap(); let mut buf = BytesMut::new(); diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index d33d273..6f08934 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -160,7 +160,6 @@ impl HttpRequestHandler { pub fn has_post_parameter(&self, name: &str) -> bool { self.body.contains_key(name) } - /// Get a post parameter pub fn post_parameter(&mut self, name: &str) -> ResultBoxError<&RequestValue> { if !self.has_post_parameter(name) { @@ -252,6 +251,12 @@ impl HttpRequestHandler { } } + /// Check out whether a file was included in the request or not + pub fn has_file(&self, name: &str) -> bool { + if let Some(RequestValue::File(_, _)) = self.body.get(name) { true } else { false } + } + + /// Get an integer included in the POST request pub fn post_i64(&mut self, name: &str) -> ResultBoxError { Ok(self.post_string(name)?.parse::()?)