From 92aeab13fbe2dcc0cf9a3316c10863d815abda04 Mon Sep 17 00:00:00 2001 From: Damien Matabon Date: Mon, 7 May 2018 14:47:28 +0200 Subject: [PATCH 1/2] Fix ParseFile MimeType guessing with uppercase extensions --- src/Parse/ParseFile.php | 2 +- tests/Parse/ParseFileTest.php | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) mode change 100755 => 100644 src/Parse/ParseFile.php diff --git a/src/Parse/ParseFile.php b/src/Parse/ParseFile.php old mode 100755 new mode 100644 index 39409d04..d7d4ad9e --- a/src/Parse/ParseFile.php +++ b/src/Parse/ParseFile.php @@ -119,7 +119,7 @@ public function getMimeType() // return an inferred mime type instead $fileParts = explode('.', $this->getName()); $extension = array_pop($fileParts); - return $this->getMimeTypeForExtension($extension); + return $this->getMimeTypeForExtension(strtolower($extension)); } } diff --git a/tests/Parse/ParseFileTest.php b/tests/Parse/ParseFileTest.php index 987a6ffc..a3a74e39 100644 --- a/tests/Parse/ParseFileTest.php +++ b/tests/Parse/ParseFileTest.php @@ -128,22 +128,27 @@ public function testParseFileTypes() $file = ParseFile::createFromData($contents, 'noextension'); $file2 = ParseFile::createFromData($contents, 'photo.png', 'text/plain'); $file3 = ParseFile::createFromData($contents, 'photo.png'); + $file4 = ParseFile::createFromData($contents, 'photo.PNG'); $file->save(); $file2->save(); $file3->save(); + $file4->save(); // check initial mime types after creating from data $this->assertEquals('unknown/unknown', $file->getMimeType()); $this->assertEquals('text/plain', $file2->getMimeType()); $this->assertEquals('image/png', $file3->getMimeType()); + $this->assertEquals('image/png', $file4->getMimeType()); $fileAgain = ParseFile::_createFromServer($file->getName(), $file->getURL()); $file2Again = ParseFile::_createFromServer($file2->getName(), $file2->getURL()); $file3Again = ParseFile::_createFromServer($file3->getName(), $file3->getURL()); + $file4Again = ParseFile::_createFromServer($file4->getName(), $file4->getURL()); $this->assertEquals($contents, $fileAgain->getData()); $this->assertEquals($contents, $file2Again->getData()); $this->assertEquals($contents, $file3Again->getData()); + $this->assertEquals($contents, $file4Again->getData()); // check mime types after calling getData $mt = $fileAgain->getMimeType(); @@ -154,6 +159,7 @@ public function testParseFileTypes() ); $this->assertEquals('image/png', $file2Again->getMimeType()); $this->assertEquals('image/png', $file3Again->getMimeType()); + $this->assertEquals('image/png', $file4Again->getMimeType()); } public function testFileOnObject() From 30224949a8beceeb67b283525c9125f8bec56e08 Mon Sep 17 00:00:00 2001 From: Damien Matabon Date: Mon, 7 May 2018 14:54:39 +0200 Subject: [PATCH 2/2] Move strtolower in getMimeTypeForExtension --- src/Parse/ParseFile.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/Parse/ParseFile.php b/src/Parse/ParseFile.php index d7d4ad9e..2c528501 100644 --- a/src/Parse/ParseFile.php +++ b/src/Parse/ParseFile.php @@ -119,7 +119,7 @@ public function getMimeType() // return an inferred mime type instead $fileParts = explode('.', $this->getName()); $extension = array_pop($fileParts); - return $this->getMimeTypeForExtension(strtolower($extension)); + return $this->getMimeTypeForExtension($extension); } } @@ -269,6 +269,7 @@ private function download() */ private function getMimeTypeForExtension($extension) { + $extension = strtolower($extension); $knownTypes = [ 'ai' => 'application/postscript', 'aif' => 'audio/x-aiff',