Skip to content

Commit 3119fc8

Browse files
authored
Use getMimeType instead of getClientMimeType. (#4085)
* Use getMimeType instead of getClientMimeType. * Fix testExtensionGuessing. * Make mime test files OS-agnostic. * Fix mime test file sizes. * Add mime tests for non-text files. * Specifiy test cases in comments.
1 parent 9201f01 commit 3119fc8

File tree

7 files changed

+52
-8
lines changed

7 files changed

+52
-8
lines changed

system/HTTP/Files/UploadedFile.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ public function getExtension(): string
325325
*/
326326
public function guessExtension(): string
327327
{
328-
return Mimes::guessExtensionFromType($this->getClientMimeType(), $this->getClientExtension()) ?? $this->getClientExtension();
328+
return Mimes::guessExtensionFromType($this->getMimeType(), $this->getClientExtension()) ?? $this->getClientExtension();
329329
}
330330

331331
//--------------------------------------------------------------------
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
text
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1+
more text
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
separated;"text"
441 Bytes
Binary file not shown.
441 Bytes
Binary file not shown.

tests/system/HTTP/Files/FileCollectionTest.php

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,28 +143,71 @@ public function testExtensionGuessing()
143143
'userfile1' => [
144144
'name' => 'fileA.txt',
145145
'type' => 'text/plain',
146-
'size' => 124,
147-
'tmp_name' => '/fileA.txt',
146+
'size' => 4,
147+
'tmp_name' => SUPPORTPATH . 'HTTP/Files/tmp/fileA.txt',
148148
'error' => 0,
149149
],
150150
'userfile2' => [
151151
'name' => 'fileB.txt',
152152
'type' => 'text/csv',
153-
'size' => 248,
154-
'tmp_name' => '/fileB.txt',
153+
'size' => 9,
154+
'tmp_name' => SUPPORTPATH . 'HTTP/Files/tmp/fileB.txt',
155+
'error' => 0,
156+
],
157+
'userfile3' => [
158+
'name' => 'fileC.csv',
159+
'type' => 'text/csv',
160+
'size' => 16,
161+
'tmp_name' => SUPPORTPATH . 'HTTP/Files/tmp/fileC.csv',
162+
'error' => 0,
163+
],
164+
'userfile4' => [
165+
'name' => 'fileD.zip',
166+
'type' => 'application/zip',
167+
'size' => 441,
168+
'tmp_name' => SUPPORTPATH . 'HTTP/Files/tmp/fileD.zip',
169+
'error' => 0,
170+
],
171+
'userfile5' => [
172+
'name' => 'fileE.zip.rar',
173+
'type' => 'application/rar',
174+
'size' => 441,
175+
'tmp_name' => SUPPORTPATH . 'HTTP/Files/tmp/fileE.zip.rar',
155176
'error' => 0,
156177
],
157178
];
158179

159180
$collection = new FileCollection();
160181

182+
// proposed extension matches finfo_open mime type (text/plain)
161183
$file = $collection->getFile('userfile1');
162184
$this->assertInstanceOf(UploadedFile::class, $file);
163185
$this->assertEquals('txt', $file->getExtension());
164186

187+
// proposed extension matches finfo_open mime type (text/plain)
188+
// but not client mime type
165189
$file = $collection->getFile('userfile2');
166190
$this->assertInstanceOf(UploadedFile::class, $file);
167-
$this->assertEquals('csv', $file->guessExtension());
191+
$this->assertEquals('txt', $file->getExtension());
192+
$this->assertNotEquals('txt', \Config\Mimes::guessExtensionFromType($file->getClientMimeType(), $file->getClientExtension()) ?? $file->getClientExtension());
193+
194+
// proposed extension does not match finfo_open mime type (text/plain)
195+
// but can be resolved by reverse searching
196+
$file = $collection->getFile('userfile3');
197+
$this->assertInstanceOf(UploadedFile::class, $file);
198+
$this->assertEquals('csv', $file->getExtension());
199+
200+
// proposed extension matches finfo_open mime type (application/zip)
201+
$file = $collection->getFile('userfile4');
202+
$this->assertInstanceOf(UploadedFile::class, $file);
203+
$this->assertEquals('zip', $file->getExtension());
204+
205+
// proposed extension matches client mime type, but not finfo_open mime type (application/zip)
206+
// this is a zip file (userFile4) but hat been renamed to 'rar'
207+
$file = $collection->getFile('userfile5');
208+
$this->assertInstanceOf(UploadedFile::class, $file);
209+
$this->assertNotEquals('rar', $file->getExtension());
210+
$this->assertEquals('rar', \Config\Mimes::guessExtensionFromType($file->getClientMimeType(), $file->getClientExtension()) ?? $file->getClientExtension());
168211
}
169212

170213
//--------------------------------------------------------------------

0 commit comments

Comments
 (0)