mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 00:45:18 +00:00
Can create post images
This commit is contained in:
@ -4,14 +4,30 @@
|
||||
|
||||
use crate::api_data::post_api::PostAPI;
|
||||
use crate::api_data::res_create_post::ResCreatePost;
|
||||
use crate::constants::PATH_POST_IMAGES;
|
||||
use crate::controllers::routes::RequestResult;
|
||||
use crate::data::error::ExecError;
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::group::GroupAccessLevel;
|
||||
use crate::data::http_request_handler::HttpRequestHandler;
|
||||
use crate::data::post::{Post, PostAccessLevel, PostKind, PostPageKind, PostVisibilityLevel};
|
||||
use crate::data::post::{Post, PostAccessLevel, PostFile, PostKind, PostPageKind, PostVisibilityLevel};
|
||||
use crate::helpers::{groups_helper, posts_helper, user_helper};
|
||||
use crate::utils::date_utils::time;
|
||||
use crate::utils::string_utils::check_string_before_insert;
|
||||
use crate::utils::user_data_utils::user_data_path;
|
||||
|
||||
impl PostFile {
|
||||
/// Initialize a `PostFile` instance based on a file that have just been created
|
||||
pub fn new_from_created_file(path: &str) -> ResultBoxError<PostFile> {
|
||||
let data = std::fs::metadata(user_data_path(path.as_ref()))?;
|
||||
Ok(PostFile {
|
||||
path: path.to_string(),
|
||||
size: data.len() as usize,
|
||||
file_type: mime_guess::from_path(path)
|
||||
.first()
|
||||
.map(|m| format!("{}/{}", m.type_(), m.subtype())),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the list of posts of a user
|
||||
pub fn get_list_user(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
@ -113,6 +129,18 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
PostKind::POST_KIND_TEXT
|
||||
}
|
||||
|
||||
|
||||
// Image post
|
||||
"image" => {
|
||||
if !r.has_file("image") {
|
||||
r.bad_request("An error occured while receiving the image!".to_string())?;
|
||||
}
|
||||
|
||||
let path = r.save_post_image("image", PATH_POST_IMAGES, 2000, 2000)?;
|
||||
|
||||
PostKind::POST_KIND_IMAGE(PostFile::new_from_created_file(&path)?)
|
||||
}
|
||||
|
||||
_ => {
|
||||
r.internal_error(ExecError::boxed_new("Unsupported kind of post!"))?;
|
||||
unreachable!();
|
||||
|
Reference in New Issue
Block a user