diff options
author | Morris Jobke <hey@morrisjobke.de> | 2017-11-06 18:45:32 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-11-06 18:45:32 +0100 |
commit | 8a8e76561ba64377ac11b5d6d5f7aa0968c2aa8c (patch) | |
tree | 1ba6ddb07e5e999a433c34a052af4afd1aa1239e | |
parent | f9e29d09b5dc9bbaed2ffa7835dd370207093d6e (diff) | |
parent | 21b8399e88e7f98193bae7e6208727d0d77fa0b0 (diff) | |
download | nextcloud-server-8a8e76561ba64377ac11b5d6d5f7aa0968c2aa8c.tar.gz nextcloud-server-8a8e76561ba64377ac11b5d6d5f7aa0968c2aa8c.zip |
Merge pull request #7081 from nextcloud/12-6958
[stable12] Improve mimetype detection for object storages
-rw-r--r-- | lib/private/Files/ObjectStore/ObjectStoreStorage.php | 10 | ||||
-rw-r--r-- | lib/private/Files/Type/Detection.php | 4 | ||||
-rw-r--r-- | tests/lib/Files/Type/DetectionTest.php | 2 |
3 files changed, 15 insertions, 1 deletions
diff --git a/lib/private/Files/ObjectStore/ObjectStoreStorage.php b/lib/private/Files/ObjectStore/ObjectStoreStorage.php index ded69e8079b..15df808684b 100644 --- a/lib/private/Files/ObjectStore/ObjectStoreStorage.php +++ b/lib/private/Files/ObjectStore/ObjectStoreStorage.php @@ -390,7 +390,15 @@ class ObjectStoreStorage extends \OC\Files\Storage\Common { $stat['size'] = filesize($tmpFile); $stat['mtime'] = $mTime; $stat['storage_mtime'] = $mTime; - $stat['mimetype'] = \OC::$server->getMimeTypeDetector()->detect($tmpFile); + + // run path based detection first, to use file extension because $tmpFile is only a random string + $mimetypeDetector = \OC::$server->getMimeTypeDetector(); + $mimetype = $mimetypeDetector->detectPath($path); + if ($mimetype === 'application/octet-stream') { + $mimetype = $mimetypeDetector->detect($tmpFile); + } + + $stat['mimetype'] = $mimetype; $stat['etag'] = $this->getETag($path); $fileId = $this->getCache()->put($path, $stat); diff --git a/lib/private/Files/Type/Detection.php b/lib/private/Files/Type/Detection.php index cd4ddc2f067..928c68251cf 100644 --- a/lib/private/Files/Type/Detection.php +++ b/lib/private/Files/Type/Detection.php @@ -173,6 +173,10 @@ class Detection implements IMimeTypeDetector { // note: leading dot doesn't qualify as extension if (strpos($fileName, '.') > 0) { + + // remove versioning extension: name.v1508946057 and transfer extension: name.ocTransferId2057600214.part + $fileName = preg_replace('!((\.v\d+)|((.ocTransferId\d+)?.part))$!', '', $fileName); + //try to guess the type by the file extension $extension = strtolower(strrchr($fileName, '.')); $extension = substr($extension, 1); //remove leading . diff --git a/tests/lib/Files/Type/DetectionTest.php b/tests/lib/Files/Type/DetectionTest.php index 5c1f48a806e..81c3bfe876e 100644 --- a/tests/lib/Files/Type/DetectionTest.php +++ b/tests/lib/Files/Type/DetectionTest.php @@ -83,6 +83,8 @@ class DetectionTest extends \Test\TestCase { $this->assertEquals('application/octet-stream', $this->detection->detectPath('..hidden')); $this->assertEquals('application/octet-stream', $this->detection->detectPath('foo')); $this->assertEquals('application/octet-stream', $this->detection->detectPath('')); + $this->assertEquals('image/png', $this->detection->detectPath('foo.png.ocTransferId123456789.part')); + $this->assertEquals('image/png', $this->detection->detectPath('foo.png.v1234567890')); } public function testDetectString() { |