use mime::Mime;
use uuid::Uuid;
use crate::utils::file::{get_file_name_ext, get_mime_ext, make_s3_safe};
pub mod delete_file;
pub mod generated;
pub mod index_file;
pub mod reprocess_octet_stream_files;
pub mod update_file;
pub mod upload_file;
pub mod upload_file_presigned;
pub fn create_file_key(document_box: &str, name: &str, mime: &Mime, file_key: Uuid) -> String {
let file_ext = get_file_name_ext(name)
.or_else(|| get_mime_ext(mime).map(|value| value.to_string()))
.unwrap_or_else(|| "bin".to_string());
let file_name = name.strip_suffix(&file_ext).unwrap_or(name);
let clean_file_name = make_s3_safe(file_name);
let file_key = format!("{file_key}_{clean_file_name}.{file_ext}");
format!("{document_box}/{file_key}")
}
pub fn create_generated_file_key(base_file_key: &str, mime: &Mime) -> String {
let file_ext = get_mime_ext(mime).unwrap_or("bin");
let file_key = Uuid::new_v4().to_string();
format!("{base_file_key}_{file_key}.generated.{file_ext}")
}
#[cfg(test)]
mod test {
use crate::files::create_file_key;
use mime::Mime;
use uuid::Uuid;
#[test]
fn test_create_file_key_ext_from_mime() {
let scope = "scope";
let mime: Mime = "image/png".parse().unwrap();
let file_key = Uuid::new_v4();
let key = create_file_key(scope, "photo", &mime, file_key);
assert_eq!(key, format!("scope/{file_key}_photo.png"));
}
#[test]
fn test_create_file_key_fallback_bin() {
let scope = "scope";
let mime: Mime = "unknown/unknown".parse().unwrap();
let file_key = Uuid::new_v4();
let key = create_file_key(scope, "file", &mime, file_key);
assert_eq!(key, format!("scope/{file_key}_file.bin"));
}
#[test]
fn test_create_file_key_strips_special_chars() {
let scope = "scope";
let mime: Mime = "text/plain".parse().unwrap();
let file_key = Uuid::new_v4();
let key = create_file_key(scope, "some file$name.txt", &mime, file_key);
assert_eq!(key, format!("scope/{file_key}_some_filename.txt"));
}
}