mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 08:25:16 +00:00
Ready to resize & save images
This commit is contained in:
@ -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")
|
||||
}
|
@ -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
|
||||
|
@ -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<String> {
|
||||
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<i64> {
|
||||
|
Reference in New Issue
Block a user