Skip to content

Commit

Permalink
Fix filename cleaning
Browse files Browse the repository at this point in the history
  • Loading branch information
lennyrouanet committed Dec 4, 2023
1 parent f6940da commit 7467fef
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
3 changes: 3 additions & 0 deletions component/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion component/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Zip extends File
{
public function unarchive(?string $unarchiveDirectory = null): ?array
{
if (! $this->exist()) {
if (!$this->exist()) {
return null;
}

Expand Down
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions test/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function setUp(): void

public function tearDown(): void
{
if (! file_exists($this->filePath)) {
if (!file_exists($this->filePath)) {
return;
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7467fef

Please sign in to comment.