mirror of
https://gitlab.com/comunic/comunicapiv3
synced 2025-06-21 17:05:16 +00:00
Create destination folder
This commit is contained in:
@ -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)
|
||||
}
|
Reference in New Issue
Block a user