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

Generate new file name

This commit is contained in:
Pierre HUBERT 2020-06-22 08:42:51 +02:00
parent 368a512722
commit e547219824
2 changed files with 26 additions and 5 deletions

View File

@ -15,7 +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;
use crate::utils::user_data_utils::{prepare_file_creation, generate_new_user_data_file_name};
/// Http request handler
///
@ -280,8 +280,9 @@ impl HttpRequestHandler {
let file = self.post_file(name)?;
let target_user_data_folder = prepare_file_creation(self.user_id()?, folder)?;
let target_file_path = generate_new_user_data_file_name(target_user_data_folder.as_path(), "png")?;
Ok(target_user_data_folder.to_str().unwrap().to_string())
Ok(target_file_path.to_str().unwrap().to_string())
}
/// Get an integer included in the POST request

View File

@ -2,12 +2,15 @@
//!
//! @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;
use crate::data::config::conf;
use crate::data::error::{ExecError, ResultBoxError};
use crate::data::user::UserID;
use crate::utils::crypt_utils::rand_str;
/// Get the full URL to a user data file
///
/// `uri` should contain the path to the target resource
@ -41,4 +44,21 @@ pub fn prepare_file_creation(user_id: UserID, folder: &str) -> ResultBoxError<Pa
}
Ok(subfolder)
}
/// Generate a new file name to store user data
pub fn generate_new_user_data_file_name(dir: &Path, ext: &str) -> ResultBoxError<PathBuf> {
let sys_dir = user_data_path(&dir);
if !sys_dir.exists() {
return Err(ExecError::boxed_string(format!("Directory {:?} does not exists!", dir)));
}
loop {
let file = format!("{}.{}", rand_str(30), ext);
if !sys_dir.join(&file).exists() {
return Ok(dir.join(&file));
}
}
}