mirror of
https://git.asonix.dog/asonix/pict-rs
synced 2024-12-22 03:11:24 +00:00
Use same UUID for directory and filename
This commit is contained in:
parent
25ef3861f1
commit
348f4ce0a3
3 changed files with 27 additions and 24 deletions
|
@ -2,17 +2,36 @@ use std::path::PathBuf;
|
|||
|
||||
use uuid::Uuid;
|
||||
|
||||
pub(crate) fn generate_disk(mut path: PathBuf) -> PathBuf {
|
||||
path.extend(generate());
|
||||
fn add_extension(filename: String, extension: Option<&str>) -> String {
|
||||
if let Some(extension) = extension {
|
||||
filename + extension
|
||||
} else {
|
||||
filename
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) fn generate_disk(mut path: PathBuf, extension: Option<&str>) -> PathBuf {
|
||||
let (directories, filename) = generate();
|
||||
path.extend(directories);
|
||||
path.push(add_extension(filename, extension));
|
||||
path
|
||||
}
|
||||
|
||||
pub(crate) fn generate_object() -> String {
|
||||
generate().join("/")
|
||||
pub(crate) fn generate_object(extension: Option<&str>) -> String {
|
||||
let (directories, filename) = generate();
|
||||
|
||||
format!(
|
||||
"{}/{}",
|
||||
directories.join("/"),
|
||||
add_extension(filename, extension)
|
||||
)
|
||||
}
|
||||
|
||||
fn generate() -> Vec<String> {
|
||||
fn generate() -> (Vec<String>, String) {
|
||||
let s = Uuid::now_v7().simple().to_string();
|
||||
|
||||
(0..10).map(|i| s[i * 2..i * 2 + 2].to_string()).collect()
|
||||
let directories = (0..10).map(|i| s[i * 2..i * 2 + 2].to_string()).collect();
|
||||
let filename = s[20..].to_string();
|
||||
|
||||
(directories, filename)
|
||||
}
|
||||
|
|
|
@ -172,15 +172,7 @@ impl FileStore {
|
|||
}
|
||||
|
||||
fn next_file(&self, extension: Option<&str>) -> PathBuf {
|
||||
let target_path = crate::file_path::generate_disk(self.root_dir.clone());
|
||||
let file_id = uuid::Uuid::new_v4().to_string();
|
||||
let filename = if let Some(ext) = extension {
|
||||
file_id + ext
|
||||
} else {
|
||||
file_id
|
||||
};
|
||||
|
||||
target_path.join(filename)
|
||||
crate::file_path::generate_disk(self.root_dir.clone(), extension)
|
||||
}
|
||||
|
||||
#[tracing::instrument(level = "debug", skip(self, path), fields(path = ?path.as_ref()))]
|
||||
|
|
|
@ -778,15 +778,7 @@ impl ObjectStore {
|
|||
}
|
||||
|
||||
fn next_file(&self, extension: Option<&str>) -> String {
|
||||
let path = crate::file_path::generate_object();
|
||||
let file_id = uuid::Uuid::new_v4().to_string();
|
||||
let filename = if let Some(ext) = extension {
|
||||
file_id + ext
|
||||
} else {
|
||||
file_id
|
||||
};
|
||||
|
||||
format!("{path}/{filename}")
|
||||
crate::file_path::generate_object(extension)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue