From 368a5127228ebc4cd26d17dc337c5015b5dade6d Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Mon, 22 Jun 2020 08:24:12 +0200 Subject: [PATCH] Create destination folder --- src/data/http_request_handler.rs | 5 ++++- src/utils/user_data_utils.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/data/http_request_handler.rs b/src/data/http_request_handler.rs index b7b6bba..d21bdb0 100644 --- a/src/data/http_request_handler.rs +++ b/src/data/http_request_handler.rs @@ -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 { 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 diff --git a/src/utils/user_data_utils.rs b/src/utils/user_data_utils.rs index a262d4a..9b1d854 100644 --- a/src/utils/user_data_utils.rs +++ b/src/utils/user_data_utils.rs @@ -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 { + 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"), "No listing.")?; + } + + Ok(subfolder) } \ No newline at end of file