mirror of
https://git.asonix.dog/asonix/pict-rs.git
synced 2024-11-24 18:41:06 +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;
|
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)
|
||||||
}
|
}
|
||||||
|
|
|
@ -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()))]
|
||||||
|
|
|
@ -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}")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue