From 40cf0c63286926f7357eb8136c0d2325cf33ba29 Mon Sep 17 00:00:00 2001 From: Patrick Organ Date: Tue, 22 Nov 2022 12:02:33 -0500 Subject: [PATCH] Add Additional Filename Sanitization (#3105) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update FileAdder.php Update the default filename sanitizer to handle funky whitespace characters in files - I had a case where a file was being uploaded, and it kepts throwing Flysystems `CorruptedPathDetected` exception. Not sure if copy-pasting the filename to github keeps the characters there: `Scan-‎9‎.‎14‎.‎2022-‎7‎.‎23‎.‎28.pdf` * add unit tests for PR #3031 * Fix styling Co-authored-by: Tommi Carleman --- src/MediaCollections/FileAdder.php | 2 ++ tests/MediaCollections/FileAdderTest.php | 25 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+) create mode 100644 tests/MediaCollections/FileAdderTest.php diff --git a/src/MediaCollections/FileAdder.php b/src/MediaCollections/FileAdder.php index 8e348a7a3..bd9edf6ca 100644 --- a/src/MediaCollections/FileAdder.php +++ b/src/MediaCollections/FileAdder.php @@ -396,6 +396,8 @@ protected function ensureDiskExists(string $diskName) public function defaultSanitizer(string $fileName): string { + $fileName = preg_replace('#\p{C}+#u', '', $fileName); + return str_replace(['#', '/', '\\', ' '], '-', $fileName); } diff --git a/tests/MediaCollections/FileAdderTest.php b/tests/MediaCollections/FileAdderTest.php new file mode 100644 index 000000000..08ee8053b --- /dev/null +++ b/tests/MediaCollections/FileAdderTest.php @@ -0,0 +1,25 @@ +defaultSanitizer('valid-filename.jpg')) + ->toEqual('valid-filename.jpg'); + + expect($adder->defaultSanitizer('test one.pdf')) + ->toEqual('test-one.pdf'); + + expect($adder->defaultSanitizer('test#one.pdf')) + ->toEqual('test-one.pdf'); + + expect($adder->defaultSanitizer('some/test/file.pdf')) + ->toEqual('some-test-file.pdf'); + + expect($adder->defaultSanitizer('Scan-‎9‎.‎14‎.‎2022-‎7‎.‎23‎.‎28.pdf')) + ->toEqual('Scan-9.14.2022-7.23.28.pdf'); +});