1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2025-06-20 00:15:17 +00:00

Can create post images

This commit is contained in:
2020-07-08 11:03:17 +02:00
parent ea1ed285f8
commit 8753f77227
6 changed files with 87 additions and 4 deletions

View File

@ -66,5 +66,8 @@ pub const PATH_GROUPS_LOGOS: &str = "groups_logo";
/// The group logo to use when no custom logo have been uploaded
pub const DEFAULT_GROUP_LOGO: &str = "groups_logo/default.png";
/// The path where image associated with posts should be stored
pub const PATH_POST_IMAGES: &str ="imgpost";
/// Maximum requests size (50 Mo)
pub const MAX_REQUEST_SIZE: usize = 50000000;

View File

@ -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!();

View File

@ -555,6 +555,11 @@ impl InsertQuery {
self
}
pub fn add_usize(mut self, key: &str, value: usize) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value));
self
}
pub fn add_u32(mut self, key: &str, value: u32) -> InsertQuery {
self.values.insert(key.to_string(), Value::from(value));
self

View File

@ -64,7 +64,7 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
};
// Start insert query
let insert_query = database::InsertQuery::new(POSTS_TABLE)
let mut insert_query = database::InsertQuery::new(POSTS_TABLE)
.add_user_id("ID_personne", user_id)
.add_u64("ID_amis", friend_id.map(|f| f.id()).unwrap_or(0))
.add_u64("group_id", group_id.map(|f| f.id()).unwrap_or(0))
@ -74,6 +74,26 @@ pub fn create(p: &Post) -> ResultBoxError<u64> {
.add_str("type", &p.kind.to_db())
.add_opt_str("texte", p.content.as_ref());
match &p.kind {
PostKind::POST_KIND_TEXT => {/* nothing to do */},
// Posts with associated file
POST_KIND_IMAGE(file) | POST_KIND_PDF(file) => {
insert_query = insert_query.add_str("path", &file.path)
.add_usize("size", file.size)
.add_opt_str("file_type", file.file_type.as_ref());
}
_ => unimplemented!()
/*
POST_KIND_WEBLINK(_) => {},
POST_KIND_MOVIE(_) => {},
POST_KIND_COUNTDOWN(_) => {},
POST_KIND_SURVEY => {},
POST_KIND_YOUTUBE(_) => {},*/
}
// Execute insertion
let post_id = insert_query.insert()?
.ok_or(ExecError::new("Insert post query did not return a result!"))?;