Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 address extension change in the code and in the docs #1484

Merged
merged 1 commit into from
Dec 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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