mirror of
https://github.com/chatmail/core.git
synced 2026-04-18 14:06:29 +03:00
Do not break double file extensions
Double extensions are sometimes used to identify files correctly, e.g. .tar.gz or .html.zip. Breaking those extensions is not very nice. This fixes #865.
This commit is contained in:
committed by
holger krekel
parent
ff8e282c43
commit
f505ff03e4
30
src/blob.rs
30
src/blob.rs
@@ -283,13 +283,13 @@ impl<'a> BlobObject<'a> {
|
||||
};
|
||||
|
||||
let clean = sanitize_filename::sanitize_with_options(name, opts);
|
||||
let mut iter = clean.rsplitn(2, '.');
|
||||
let mut ext = iter.next().unwrap_or_default().to_string();
|
||||
let mut iter = clean.splitn(2, '.');
|
||||
let mut stem = iter.next().unwrap_or_default().to_string();
|
||||
ext.truncate(32);
|
||||
let mut ext = iter.next().unwrap_or_default().to_string();
|
||||
stem.truncate(64);
|
||||
match stem.len() {
|
||||
0 => (ext, "".to_string()),
|
||||
ext.truncate(32);
|
||||
match ext.len() {
|
||||
0 => (stem, "".to_string()),
|
||||
_ => (stem, format!(".{}", ext).to_lowercase()),
|
||||
}
|
||||
}
|
||||
@@ -610,6 +610,26 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_double_ext_preserved() {
|
||||
let t = dummy_context();
|
||||
BlobObject::create(&t.ctx, "foo.tar.gz", b"hello").unwrap();
|
||||
let foo = t.ctx.get_blobdir().join("foo.tar.gz");
|
||||
assert!(foo.exists());
|
||||
BlobObject::create(&t.ctx, "foo.tar.gz", b"world").unwrap();
|
||||
for dirent in fs::read_dir(t.ctx.get_blobdir()).unwrap() {
|
||||
let fname = dirent.unwrap().file_name();
|
||||
if fname == foo.file_name().unwrap() {
|
||||
assert_eq!(fs::read(&foo).unwrap(), b"hello");
|
||||
} else {
|
||||
let name = fname.to_str().unwrap();
|
||||
println!("{}", name);
|
||||
assert!(name.starts_with("foo"));
|
||||
assert!(name.ends_with(".tar.gz"));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_create_long_names() {
|
||||
let t = dummy_context();
|
||||
|
||||
Reference in New Issue
Block a user