1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-11-26 15:29:21 +00:00

Load, resize & save post image

This commit is contained in:
Pierre HUBERT 2020-06-22 09:08:13 +02:00
parent 1da7a30419
commit ac469f86e8

View File

@ -5,6 +5,7 @@ use std::str::FromStr;
use actix_web::{HttpRequest, HttpResponse, web}; use actix_web::{HttpRequest, HttpResponse, web};
use actix_web::http::{HeaderName, HeaderValue}; use actix_web::http::{HeaderName, HeaderValue};
use bytes::Bytes; use bytes::Bytes;
use image::ImageFormat;
use serde::Serialize; use serde::Serialize;
use crate::api_data::http_error::HttpError; use crate::api_data::http_error::HttpError;
@ -277,13 +278,23 @@ impl HttpRequestHandler {
/// Save an image in user data directory /// 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<String> { pub fn save_post_image(&mut self, name: &str, folder: &str, max_w: u32, max_h: u32) -> ResultBoxError<String> {
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_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_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()); 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 /// Get an integer included in the POST request