Use same UUID for directory and filename

This commit is contained in:
asonix 2024-03-02 12:20:50 -06:00
parent 25ef3861f1
commit 348f4ce0a3
3 changed files with 27 additions and 24 deletions

View file

@ -2,17 +2,36 @@ use std::path::PathBuf;
use uuid::Uuid; use uuid::Uuid;
pub(crate) fn generate_disk(mut path: PathBuf) -> PathBuf { fn add_extension(filename: String, extension: Option<&str>) -> String {
path.extend(generate()); 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 path
} }
pub(crate) fn generate_object() -> String { pub(crate) fn generate_object(extension: Option<&str>) -> String {
generate().join("/") 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(); 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)
} }

View file

@ -172,15 +172,7 @@ impl FileStore {
} }
fn next_file(&self, extension: Option<&str>) -> PathBuf { fn next_file(&self, extension: Option<&str>) -> PathBuf {
let target_path = crate::file_path::generate_disk(self.root_dir.clone()); crate::file_path::generate_disk(self.root_dir.clone(), extension)
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)
} }
#[tracing::instrument(level = "debug", skip(self, path), fields(path = ?path.as_ref()))] #[tracing::instrument(level = "debug", skip(self, path), fields(path = ?path.as_ref()))]

View file

@ -778,15 +778,7 @@ impl ObjectStore {
} }
fn next_file(&self, extension: Option<&str>) -> String { fn next_file(&self, extension: Option<&str>) -> String {
let path = crate::file_path::generate_object(); crate::file_path::generate_object(extension)
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}")
} }
} }