mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 16:35:17 +00:00
Ready to resize & save images
This commit is contained in:
@ -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