Skip to content

Commit

Permalink
Merge pull request #1484 from garak/fix-extension
Browse files Browse the repository at this point in the history
🐛 address extension change in the code and in the docs
  • Loading branch information
garak authored Dec 26, 2024
2 parents 4f1c098 + 4b19632 commit 99b1aad
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
11 changes: 11 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# Upgrading from v2.4 to v2.5

* To address the question raised in the previous version, now the original extension '.csv' is retained
even if the mime type is guessed as 'text/plain'.

# Upgrading from v2.3 to v2.4

* To address a security question, the original extension of the uploaded file is not preserved anymore.
Instead, it is replaced by the extension of the matching mime type. This could cause a different
behaviour only if you use some non-standard extension, otherwise it should not change anything.

# Upgrading from v2.1 to v2.2

* The signature of `StorageInterface::resolveStream` method was changed. The $fieldName parameter is now nullable.
Expand Down
12 changes: 12 additions & 0 deletions src/Naming/Polyfill/FileExtensionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

trait FileExtensionTrait
{
// extensions safe to keep
private static array $keep = [
'txt' => 'csv',
];

/**
* Guess the extension of the given file.
*/
Expand All @@ -18,6 +23,13 @@ private function getExtension(File $file): ?string
}

if ('' !== ($extension = $file->guessExtension())) {
if (isset(self::$keep[$extension])) {
$originalExtension = \pathinfo($file->getClientOriginalName(), \PATHINFO_EXTENSION);
if (self::$keep[$extension] === $originalExtension) {
return $originalExtension;
}
}

return $extension;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Naming/SmartUniqidNamerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function fileDataProvider(): array
'double uppercase extension' => ['lala.JPEG.JPEG', 'jpg', '/lala-jpeg-[[:xdigit:]]{22}\.jpg/'],
'dot in filename' => ['filename has . spaces (2).jpg', 'jpg', '/filename-has-spaces-2-[[:xdigit:]]{22}\.jpg/'],
'file with no extension with null mimetype' => ['lala', null, '/lala-[[:xdigit:]]{22}$/'],
'csv retains extension even if guessed as txt' => ['lala.csv', 'txt', '/lala-[[:xdigit:]]{22}\.csv/'],
];
}

Expand All @@ -34,7 +35,6 @@ public function testNameReturnsAnUniqueName(string $originalName, ?string $guess
{
$file = $this->getUploadedFileMock();
$file
->expects(self::once())
->method('getClientOriginalName')
->willReturn($originalName)
;
Expand Down

0 comments on commit 99b1aad

Please sign in to comment.