1
0
mirror of https://gitlab.com/comunic/comunicapiv3 synced 2024-12-28 14:38:52 +00:00

Create destination folder

This commit is contained in:
Pierre HUBERT 2020-06-22 08:24:12 +02:00
parent 23da151635
commit 368a512722
2 changed files with 36 additions and 1 deletions

View File

@ -15,6 +15,7 @@ use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::helpers::{account_helper, api_helper, conversations_helper, user_helper};
use crate::utils::virtual_directories_utils::check_virtual_directory;
use crate::utils::user_data_utils::prepare_file_creation;
/// Http request handler
///
@ -278,7 +279,9 @@ impl HttpRequestHandler {
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()))
let target_user_data_folder = prepare_file_creation(self.user_id()?, folder)?;
Ok(target_user_data_folder.to_str().unwrap().to_string())
}
/// Get an integer included in the POST request

View File

@ -3,10 +3,42 @@
//! @author Pierre Hubert
use crate::data::config::conf;
use crate::data::error::ResultBoxError;
use crate::data::user::UserID;
use std::path::{Path, PathBuf};
use serde_json::to_string;
/// Get the full URL to a user data file
///
/// `uri` should contain the path to the target resource
pub fn user_data_url(uri: &str) -> String {
format!("{}{}", conf().storage_url, uri)
}
/// Get the system path to a user data file
pub fn user_data_path(uri: &Path) -> PathBuf {
Path::new(conf().storage_path.as_str()).join(uri)
}
/// Prepare the creation of a file (user data file)
///
/// This function returns the relative folder path in user data directory where the file can be
/// created
pub fn prepare_file_creation(user_id: UserID, folder: &str) -> ResultBoxError<PathBuf> {
let subfolder = match user_id {
0 => Path::new(folder).to_path_buf(),
id => Path::new(folder).join(to_string(&id)?)
};
let full_path = user_data_path(subfolder.as_path());
if !full_path.exists() {
// Create the directory
std::fs::create_dir_all(&full_path)?;
// Block directory listing
std::fs::write(&full_path.join("index.html"), "<b>No listing.</b>")?;
}
Ok(subfolder)
}