From 58da7988c370ea0dbccfdd8d48b4594c2e3a205f Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sun, 9 Feb 2020 11:43:11 +1000 Subject: [PATCH] catch trailing space/period as well --- rslib/src/media/files.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/rslib/src/media/files.rs b/rslib/src/media/files.rs index 1f9cd00a3..e1885d1d4 100644 --- a/rslib/src/media/files.rs +++ b/rslib/src/media/files.rs @@ -37,6 +37,16 @@ lazy_static! { "# ) .unwrap(); + static ref WINDOWS_TRAILING_CHAR: Regex = Regex::new( + r#"(?x) + # filenames can't end with a space or period + ( + \x20 | \. + ) + $ + "# + ) + .unwrap(); pub(super) static ref NONSYNCABLE_FILENAME: Regex = Regex::new( r#"(?xi) ^ @@ -84,6 +94,10 @@ fn normalize_nfc_filename(mut fname: Cow) -> Cow { fname = o.into(); } + if WINDOWS_TRAILING_CHAR.is_match(fname.as_ref()) { + fname = format!("{}_", fname.as_ref()).into(); + } + if let Cow::Owned(o) = truncate_filename(fname.as_ref(), MAX_FILENAME_LENGTH) { fname = o.into(); } @@ -393,6 +407,9 @@ mod test { "con_.jpg" ); + assert_eq!(normalize_filename("test.").as_ref(), "test._"); + assert_eq!(normalize_filename("test ").as_ref(), "test _"); + let expected_stem_len = MAX_FILENAME_LENGTH - ".jpg".len(); assert_eq!( normalize_filename(&format!("{}.jpg", "x".repeat(MAX_FILENAME_LENGTH * 2))),