From ac469f86e87e74ed6c9dec80c509094c0be035c6 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 22 Jun 2020 09:08:13 +0200 Subject: [PATCH] Load, resize & save post image --- src/data/http_request_handler.rs | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index 59732f6..8dafbad 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -5,6 +5,7 @@ use std::str::FromStr; use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::http::{HeaderName, HeaderValue}; use bytes::Bytes; +use image::ImageFormat; use serde::Serialize; use crate::api_data::http_error::HttpError; @@ -277,13 +278,23 @@ impl HttpRequestHandler { /// 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)?; + // Load image + let file = self.post_file(name)?; + let image = image::load_from_memory(file.buff.as_ref())?; + let image = image.resize(max_w, max_h, image::imageops::FilterType::Nearest); + + + // Determine image destination let target_user_data_folder = prepare_file_creation(self.user_id()?, folder)?; let target_file_path = generate_new_user_data_file_name(target_user_data_folder.as_path(), "png")?; let target_sys_path = user_data_path(target_file_path.as_path()); - Ok(target_sys_path.to_str().unwrap().to_string()) + + // Save image + image.save_with_format(target_sys_path, ImageFormat::Png)?; + + Ok(target_file_path.to_string_lossy().to_string()) } /// Get an integer included in the POST request