mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-20 00:15:17 +00:00
Can create PDF posts
This commit is contained in:
@ -67,7 +67,10 @@ pub const PATH_GROUPS_LOGOS: &str = "groups_logo";
|
||||
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";
|
||||
pub const PATH_POST_IMAGES: &str = "imgpost";
|
||||
|
||||
/// The path were PDF included with posts should be stored
|
||||
pub const PATH_POST_PDF: &str = "post_pdf";
|
||||
|
||||
/// Maximum requests size (50 Mo)
|
||||
pub const MAX_REQUEST_SIZE: usize = 50000000;
|
@ -4,7 +4,7 @@
|
||||
|
||||
use crate::api_data::post_api::PostAPI;
|
||||
use crate::api_data::res_create_post::ResCreatePost;
|
||||
use crate::constants::PATH_POST_IMAGES;
|
||||
use crate::constants::{PATH_POST_IMAGES, PATH_POST_PDF};
|
||||
use crate::controllers::routes::RequestResult;
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
use crate::data::group::GroupAccessLevel;
|
||||
@ -174,6 +174,16 @@ pub fn create_post(r: &mut HttpRequestHandler) -> RequestResult {
|
||||
})
|
||||
}
|
||||
|
||||
"pdf" => {
|
||||
if !r.has_file("pdf") {
|
||||
r.bad_request("Missing PDF in 'pdf'!".to_string())?;
|
||||
}
|
||||
|
||||
let file = r.save_post_pdf("pdf", PATH_POST_PDF)?;
|
||||
|
||||
PostKind::POST_KIND_PDF(PostFile::new_from_created_file(&file)?)
|
||||
}
|
||||
|
||||
// TODO : add support for next types
|
||||
|
||||
_ => {
|
||||
|
@ -20,6 +20,7 @@ use crate::data::post::{Post, PostAccessLevel};
|
||||
use crate::data::user::UserID;
|
||||
use crate::helpers::{account_helper, api_helper, conversations_helper, friends_helper, groups_helper, movies_helper, posts_helper, user_helper, virtual_directory_helper};
|
||||
use crate::helpers::virtual_directory_helper::VirtualDirType;
|
||||
use crate::utils::pdf_utils::is_valid_pdf;
|
||||
use crate::utils::string_utils::{check_url, remove_html_nodes};
|
||||
use crate::utils::user_data_utils::{generate_new_user_data_file_name, prepare_file_creation, user_data_path};
|
||||
use crate::utils::virtual_directories_utils::check_virtual_directory;
|
||||
@ -339,6 +340,28 @@ impl HttpRequestHandler {
|
||||
Ok(target_file_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// Save a pdf included in the request
|
||||
pub fn save_post_pdf(&mut self, name: &str, folder: &str) -> ResultBoxError<String> {
|
||||
let file = self.post_file(name)?;
|
||||
|
||||
if !is_valid_pdf(&file.buff)? {
|
||||
self.bad_request(format!("Invalid PDF specified in {} !", name))?;
|
||||
unreachable!();
|
||||
}
|
||||
|
||||
// Avoid memory warnings
|
||||
let copied_buff = file.buff.clone();
|
||||
|
||||
// Determine pdf file destination
|
||||
let target_user_data_folder = prepare_file_creation(self.user_id_ref()?, folder)?;
|
||||
let target_file_path = generate_new_user_data_file_name(target_user_data_folder.as_path(), "pdf")?;
|
||||
let target_sys_path = user_data_path(target_file_path.as_path());
|
||||
|
||||
std::fs::write(target_sys_path, &copied_buff.as_ref())?;
|
||||
|
||||
Ok(target_file_path.to_string_lossy().to_string())
|
||||
}
|
||||
|
||||
/// Get an integer included in the POST request
|
||||
pub fn post_i64(&mut self, name: &str) -> ResultBoxError<i64> {
|
||||
Ok(self.post_string(name)?.parse::<i64>()?)
|
||||
|
@ -6,4 +6,5 @@ pub mod crypt_utils;
|
||||
pub mod user_data_utils;
|
||||
pub mod virtual_directories_utils;
|
||||
pub mod date_utils;
|
||||
pub mod string_utils;
|
||||
pub mod string_utils;
|
||||
pub mod pdf_utils;
|
22
src/utils/pdf_utils.rs
Normal file
22
src/utils/pdf_utils.rs
Normal file
@ -0,0 +1,22 @@
|
||||
//! # PDF utilities
|
||||
//!
|
||||
//! @author Pierre Hubert
|
||||
|
||||
use crate::data::error::{ExecError, ResultBoxError};
|
||||
|
||||
/// Check out whether a PDF is a valid PDF or not, by trying to open it
|
||||
pub fn is_valid_pdf(file: &bytes::Bytes) -> ResultBoxError<bool> {
|
||||
let pdf = pdf::file::File::new(Vec::from(file.as_ref()));
|
||||
|
||||
match pdf.get_num_pages() {
|
||||
Ok(num) if num < 1 =>
|
||||
Err(ExecError::boxed_string(format!("Detected a PDF with {} pages!", num))),
|
||||
|
||||
Ok(_) => Ok(true),
|
||||
|
||||
Err(e) => {
|
||||
println!("Error while parsing PDF: {}", e);
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user