let uploader set a collection name for a multiple-file upload

This commit is contained in:
xenofem 2022-05-26 15:42:11 -04:00
parent 3125e1f4e7
commit 97f58bbbe3
5 changed files with 28 additions and 4 deletions

View file

@ -20,6 +20,10 @@ const MAX_FILES: usize = 256;
const FILENAME_DATE_FORMAT: &[time::format_description::FormatItem] =
time::macros::format_description!("[year]-[month]-[day]-[hour][minute][second]");
fn sanitise(name: &str) -> String {
sanitise_file_name::sanitise(&name.nfd().collect::<String>())
}
#[derive(thiserror::Error, Debug)]
enum Error {
#[error("Failed to parse file metadata")]
@ -104,7 +108,7 @@ pub struct UploadedFile {
impl UploadedFile {
fn new(name: &str, size: u64, modtime: OffsetDateTime) -> Self {
Self {
name: sanitise_file_name::sanitise(&name.nfd().collect::<String>()),
name: sanitise(name),
size,
modtime,
}
@ -134,6 +138,7 @@ struct UploadManifest {
files: Vec<RawUploadedFile>,
lifetime: u16,
password: String,
collection_name: Option<String>,
}
#[derive(Serialize)]
@ -229,6 +234,7 @@ impl Uploader {
files: raw_files,
lifetime,
password,
collection_name,
} = serde_json::from_slice(text.as_bytes())?;
if std::env::var("TRANSBEAM_UPLOAD_PASSWORD") != Ok(password) {
return Err(Error::IncorrectPassword);
@ -277,9 +283,13 @@ impl Uploader {
let now = OffsetDateTime::now_utc();
let zip_writer = super::zip::ZipGenerator::new(files.clone(), writer);
let size = zip_writer.total_size();
let download_filename = super::APP_NAME.to_owned()
+ &now.format(FILENAME_DATE_FORMAT).unwrap()
+ ".zip";
let download_filename = collection_name
.map(|f| sanitise(&(f + ".zip")))
.unwrap_or_else(|| {
super::APP_NAME.to_owned()
+ &now.format(FILENAME_DATE_FORMAT).unwrap()
+ ".zip"
});
(Box::new(zip_writer), download_filename, size, now)
} else {
(