From 348f4ce0a3afdd7c35510ae2b90529617eafa3d5 Mon Sep 17 00:00:00 2001 From: asonix Date: Sat, 2 Mar 2024 12:20:50 -0600 Subject: [PATCH] Use same UUID for directory and filename --- src/file_path.rs | 31 +++++++++++++++++++++++++------ src/store/file_store.rs | 10 +--------- src/store/object_store.rs | 10 +--------- 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/src/file_path.rs b/src/file_path.rs index 6c6dec6..fc56524 100644 --- a/src/file_path.rs +++ b/src/file_path.rs @@ -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 { +fn generate() -> (Vec, 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) } diff --git a/src/store/file_store.rs b/src/store/file_store.rs index 8084642..77143ce 100644 --- a/src/store/file_store.rs +++ b/src/store/file_store.rs @@ -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()))] diff --git a/src/store/object_store.rs b/src/store/object_store.rs index ce76ea7..3d6ed83 100644 --- a/src/store/object_store.rs +++ b/src/store/object_store.rs @@ -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) } }