From 23da151635da7a31d11718130c8ef72bc616dc7a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Sat, 20 Jun 2020 15:22:41 +0200 Subject: [PATCH] Ready to resize & save images --- Cargo.lock | 206 ++++++++++++++++++++ Cargo.toml | 3 +- src/controllers/conversations_controller.rs | 5 +- src/controllers/server.rs | 7 +- src/data/http_request_handler.rs | 32 ++- 5 files changed, 245 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7d77538..a135070 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -468,6 +468,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3b5ca7a04898ad4bcd41c90c5285445ff5b791899bb1b0abdd2a2aa791211d7" +[[package]] +name = "bytemuck" +version = "1.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "37fa13df2292ecb479ec23aa06f4507928bef07839be9ef15281411076629431" + [[package]] name = "byteorder" version = "1.3.4" @@ -522,6 +528,12 @@ dependencies = [ "bitflags", ] +[[package]] +name = "color_quant" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0dbbb57365263e881e805dc77d94697c9118fd94d8da011240555aa7b23445bd" + [[package]] name = "comunic_server" version = "0.1.0" @@ -533,6 +545,7 @@ dependencies = [ "chrono", "encoding_rs", "futures", + "image", "mailchecker", "mysql", "percent-encoding", @@ -594,6 +607,64 @@ dependencies = [ "cfg-if", ] +[[package]] +name = "crossbeam-deque" +version = "0.7.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9f02af974daeee82218205558e51ec8768b48cf524bd01d550abe5573a608285" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.8.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "058ed274caafc1f60c4997b5fc07bf7dc7cca454af7c6e81edffe5f33f70dace" +dependencies = [ + "autocfg 1.0.0", + "cfg-if", + "crossbeam-utils", + "lazy_static", + "maybe-uninit", + "memoffset", + "scopeguard", +] + +[[package]] +name = "crossbeam-queue" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "774ba60a54c213d409d5353bda12d49cd68d14e45036a285234c8d6f91f92570" +dependencies = [ + "cfg-if", + "crossbeam-utils", + "maybe-uninit", +] + +[[package]] +name = "crossbeam-utils" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" +dependencies = [ + "autocfg 1.0.0", + "cfg-if", + "lazy_static", +] + +[[package]] +name = "deflate" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e7e5d2a2273fed52a7f947ee55b092c4057025d7a3e04e5ecdbd25d6c3fb1bd7" +dependencies = [ + "adler32", + "byteorder", +] + [[package]] name = "derive_more" version = "0.99.7" @@ -869,6 +940,16 @@ dependencies = [ "wasi", ] +[[package]] +name = "gif" +version = "0.10.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "471d90201b3b223f3451cd4ad53e34295f16a1df17b1edf3736d47761c3981af" +dependencies = [ + "color_quant", + "lzw", +] + [[package]] name = "h2" version = "0.2.5" @@ -955,6 +1036,24 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "image" +version = "0.23.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d534e95ad8b9d5aa614322d02352b4f1bf962254adcf02ac6f2def8be18498e8" +dependencies = [ + "bytemuck", + "byteorder", + "gif", + "jpeg-decoder", + "num-iter", + "num-rational", + "num-traits", + "png", + "scoped_threadpool", + "tiff", +] + [[package]] name = "indexmap" version = "1.3.2" @@ -1002,6 +1101,16 @@ version = "0.4.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b8b7a7c0c47db5545ed3fef7468ee7bb5b74691498139e4b3f6a20685dc6dd8e" +[[package]] +name = "jpeg-decoder" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b47b4c4e017b01abdc5bcc126d2d1002e5a75bbe3ce73f9f4f311a916363704" +dependencies = [ + "byteorder", + "rayon", +] + [[package]] name = "kernel32-sys" version = "0.2.2" @@ -1109,6 +1218,12 @@ dependencies = [ "linked-hash-map", ] +[[package]] +name = "lzw" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7d947cbb889ed21c2a84be6ffbaebf5b4e0f4340638cba0444907e38b56be084" + [[package]] name = "mailchecker" version = "3.3.6" @@ -1130,12 +1245,27 @@ version = "0.1.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08" +[[package]] +name = "maybe-uninit" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00" + [[package]] name = "memchr" version = "2.3.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" +[[package]] +name = "memoffset" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4fc2c02a7e374099d4ee95a193111f72d2110197fe200272371758f6c3643d8" +dependencies = [ + "autocfg 1.0.0", +] + [[package]] name = "mime" version = "0.3.16" @@ -1324,6 +1454,28 @@ dependencies = [ "num-traits", ] +[[package]] +name = "num-iter" +version = "0.1.41" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a6e6b7c748f995c4c29c5f5ae0248536e04a5739927c74ec0fa564805094b9f" +dependencies = [ + "autocfg 1.0.0", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-rational" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c000134b5dbf44adc5cb772486d335293351644b801551abe8f75c84cfa4aef" +dependencies = [ + "autocfg 1.0.0", + "num-integer", + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.11" @@ -1456,6 +1608,18 @@ version = "0.3.17" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677" +[[package]] +name = "png" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34ccdd66f6fe4b2433b07e4728e9a013e43233120427046e93ceb709c3a439bf" +dependencies = [ + "bitflags", + "crc32fast", + "deflate", + "miniz_oxide", +] + [[package]] name = "ppv-lite86" version = "0.2.8" @@ -1539,6 +1703,31 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rayon" +version = "1.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f02856753d04e03e26929f820d0a0a337ebe71f849801eea335d464b349080" +dependencies = [ + "autocfg 1.0.0", + "crossbeam-deque", + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e92e15d89083484e11353891f1af602cc661426deb9564c298b270c726973280" +dependencies = [ + "crossbeam-deque", + "crossbeam-queue", + "crossbeam-utils", + "lazy_static", + "num_cpus", +] + [[package]] name = "redox_syscall" version = "0.1.56" @@ -1625,6 +1814,12 @@ dependencies = [ "winapi 0.3.8", ] +[[package]] +name = "scoped_threadpool" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d51f5df5af43ab3f1360b429fa5e0152ac5ce8c0bd6485cae490332e96846a8" + [[package]] name = "scopeguard" version = "1.1.0" @@ -1825,6 +2020,17 @@ dependencies = [ "num_cpus", ] +[[package]] +name = "tiff" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f3b8a87c4da944c3f27e5943289171ac71a6150a79ff6bacfff06d159dfff2f" +dependencies = [ + "byteorder", + "lzw", + "miniz_oxide", +] + [[package]] name = "time" version = "0.1.43" diff --git a/Cargo.toml b/Cargo.toml index a760607..68a02ee 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -21,4 +21,5 @@ mailchecker = "3.3.6" sha1 = "0.6.0" rand = "0.7.3" chrono = "0.4.11" -bytes = "0.5.4" \ No newline at end of file +bytes = "0.5.4" +image = "0.23.5" \ No newline at end of file diff --git a/src/controllers/conversations_controller.rs b/src/controllers/conversations_controller.rs index dc92024..8806aea 100644 --- a/src/controllers/conversations_controller.rs +++ b/src/controllers/conversations_controller.rs @@ -233,10 +233,13 @@ pub fn refresh_single(r: &mut HttpRequestHandler) -> RequestResult { /// Send a new message pub fn send_message(r: &mut HttpRequestHandler) -> RequestResult { + let mut image_path = None; if r.has_file("image") { - println!("File is detected."); + image_path = Some(r.save_post_image("image", "conversations", 1200, 1200)?); } + println!("image: {:?}", image_path); + r.success("implement me") } \ No newline at end of file diff --git a/src/controllers/server.rs b/src/controllers/server.rs index f0a4e50..ff8580a 100644 --- a/src/controllers/server.rs +++ b/src/controllers/server.rs @@ -17,7 +17,7 @@ use crate::constants::MAX_REQUEST_SIZE; use crate::controllers::routes::{get_routes, RequestResult, Route}; use crate::controllers::routes::Method::{GET, POST}; use crate::data::config::Config; -use crate::data::http_request_handler::{HttpRequestHandler, RequestValue}; +use crate::data::http_request_handler::{HttpRequestHandler, PostFile, RequestValue}; /// Main server functions /// @@ -165,7 +165,10 @@ impl FromRequest for CustomRequest { } body_args.insert(name.to_string(), - RequestValue::File(filename.to_string(), buf.to_bytes())); + RequestValue::File(PostFile { + name: filename.to_string(), + buff: buf.to_bytes(), + })); } // It is a simple field diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 6f08934..b7b6bba 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -4,6 +4,7 @@ use std::str::FromStr; use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::http::{HeaderName, HeaderValue}; +use bytes::Bytes; use serde::Serialize; use crate::api_data::http_error::HttpError; @@ -12,18 +13,22 @@ use crate::data::api_client::APIClient; use crate::data::config::conf; use crate::data::error::{ExecError, ResultBoxError}; use crate::data::user::UserID; -use crate::helpers::{account_helper, api_helper, user_helper, conversations_helper}; +use crate::helpers::{account_helper, api_helper, conversations_helper, user_helper}; use crate::utils::virtual_directories_utils::check_virtual_directory; -use bytes::Bytes; /// Http request handler /// /// @author Pierre Hubert +pub struct PostFile { + pub name: String, + pub buff: Bytes, +} + /// Single request body value pub enum RequestValue { String(String), - File(String, Bytes), + File(PostFile), } #[derive(Serialize)] @@ -253,9 +258,28 @@ 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 } + if let Some(RequestValue::File(_)) = self.body.get(name) { true } else { false } } + /// Get a file included in the request + pub fn post_file(&mut self, name: &str) -> ResultBoxError<&PostFile> { + if self.has_file(name) { + if let RequestValue::File(f) = self.post_parameter(name)? { + return Ok(f); + } + } else { + self.bad_request(format!("File {} not included in request!", name))?; + } + + unreachable!(); + } + + /// Save an image in user data directory + pub fn save_post_image(&mut self, name: &str, folder: &str, max_w: u32, max_h: u32) -> ResultBoxError { + let file = self.post_file(name)?; + + Ok(format!("f {} size: {}", file.name, file.buff.len())) + } /// Get an integer included in the POST request pub fn post_i64(&mut self, name: &str) -> ResultBoxError {