Skip to content

Commit 4b19632

Browse files
committed
🐛 address extension change in the code and in the docs
Fix #1479
1 parent 4f1c098 commit 4b19632

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

UPGRADE.md

+11
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,14 @@
1+
# Upgrading from v2.4 to v2.5
2+
3+
* To address the question raised in the previous version, now the original extension '.csv' is retained
4+
even if the mime type is guessed as 'text/plain'.
5+
6+
# Upgrading from v2.3 to v2.4
7+
8+
* To address a security question, the original extension of the uploaded file is not preserved anymore.
9+
Instead, it is replaced by the extension of the matching mime type. This could cause a different
10+
behaviour only if you use some non-standard extension, otherwise it should not change anything.
11+
112
# Upgrading from v2.1 to v2.2
213

314
* The signature of `StorageInterface::resolveStream` method was changed. The $fieldName parameter is now nullable.

src/Naming/Polyfill/FileExtensionTrait.php

+12
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@
88

99
trait FileExtensionTrait
1010
{
11+
// extensions safe to keep
12+
private static array $keep = [
13+
'txt' => 'csv',
14+
];
15+
1116
/**
1217
* Guess the extension of the given file.
1318
*/
@@ -18,6 +23,13 @@ private function getExtension(File $file): ?string
1823
}
1924

2025
if ('' !== ($extension = $file->guessExtension())) {
26+
if (isset(self::$keep[$extension])) {
27+
$originalExtension = \pathinfo($file->getClientOriginalName(), \PATHINFO_EXTENSION);
28+
if (self::$keep[$extension] === $originalExtension) {
29+
return $originalExtension;
30+
}
31+
}
32+
2133
return $extension;
2234
}
2335

tests/Naming/SmartUniqidNamerTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ public static function fileDataProvider(): array
2424
'double uppercase extension' => ['lala.JPEG.JPEG', 'jpg', '/lala-jpeg-[[:xdigit:]]{22}\.jpg/'],
2525
'dot in filename' => ['filename has . spaces (2).jpg', 'jpg', '/filename-has-spaces-2-[[:xdigit:]]{22}\.jpg/'],
2626
'file with no extension with null mimetype' => ['lala', null, '/lala-[[:xdigit:]]{22}$/'],
27+
'csv retains extension even if guessed as txt' => ['lala.csv', 'txt', '/lala-[[:xdigit:]]{22}\.csv/'],
2728
];
2829
}
2930

@@ -34,7 +35,6 @@ public function testNameReturnsAnUniqueName(string $originalName, ?string $guess
3435
{
3536
$file = $this->getUploadedFileMock();
3637
$file
37-
->expects(self::once())
3838
->method('getClientOriginalName')
3939
->willReturn($originalName)
4040
;

0 commit comments

Comments
 (0)