From 7467fef387dfe318bc4fa8a8175f0d011c8caa4c Mon Sep 17 00:00:00 2001 From: Lenny ROUANET Date: Mon, 4 Dec 2023 09:42:32 +0100 Subject: [PATCH] Fix filename cleaning --- component/File.php | 3 +++ component/Zip.php | 2 +- composer.json | 8 ++++---- test/FileTest.php | 8 ++++---- 4 files changed, 12 insertions(+), 9 deletions(-) diff --git a/component/File.php b/component/File.php index 04d0220..5974553 100644 --- a/component/File.php +++ b/component/File.php @@ -44,6 +44,9 @@ public static function cleanFilename(string $fileName): string ['Þ' => 'TH', 'þ' => 'th', 'Ð' => 'DH', 'ð' => 'dh', 'ß' => 'ss', 'Œ' => 'OE', 'œ' => 'oe', 'Æ' => 'AE', 'æ' => 'ae', 'µ' => 'u'] ); + $fileName = str_replace('\'', ' ', $fileName); + $fileName = preg_replace('/\s+/', ' ', $fileName); + $fileName = trim($fileName); $fileName = preg_replace(['/\s/', '/\.[\.]+/', '/[^\w_\.\-]/'], ['_', '.', ''], $fileName); $extension = strtolower($extension); diff --git a/component/Zip.php b/component/Zip.php index 24e9f3a..6552fee 100644 --- a/component/Zip.php +++ b/component/Zip.php @@ -11,7 +11,7 @@ class Zip extends File { public function unarchive(?string $unarchiveDirectory = null): ?array { - if (! $this->exist()) { + if (!$this->exist()) { return null; } diff --git a/composer.json b/composer.json index 8d03384..cb82354 100644 --- a/composer.json +++ b/composer.json @@ -10,12 +10,12 @@ } ], "require": { - "php": ">=8.1" + "php": ">=8.2" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.0", - "phpstan/phpstan": "^1.4", - "phpunit/phpunit": "^9.5" + "friendsofphp/php-cs-fixer": "3.*", + "phpstan/phpstan": "1.*", + "phpunit/phpunit": "9.*" }, "scripts": { "lint": "vendor/bin/php-cs-fixer fix ./ --rules=@PSR12", diff --git a/test/FileTest.php b/test/FileTest.php index 08812cb..16b919b 100644 --- a/test/FileTest.php +++ b/test/FileTest.php @@ -26,7 +26,7 @@ public function setUp(): void public function tearDown(): void { - if (! file_exists($this->filePath)) { + if (!file_exists($this->filePath)) { return; } @@ -58,15 +58,15 @@ public function testGetTemoraryDirectory(): void public function testCleanFilename(): void { - $result = File::cleanFilename(' µ û '); + $result = File::cleanFilename(' µ \' û '); $this->assertIsString($result); - $this->assertEquals('_u_u_', $result); + $this->assertEquals('u_u', $result); $result = File::cleanFilename(' µ û . Jpg'); $this->assertIsString($result); - $this->assertEquals('_u_u_.jpg', $result); + $this->assertEquals('u_u.jpg', $result); } public function testDownload(): void