]> source.dussan.org Git - nextcloud-server.git/commitdiff
Improve mimetype detection for object storages 7081/head
authorMorris Jobke <hey@morrisjobke.de>
Wed, 25 Oct 2017 15:57:21 +0000 (17:57 +0200)
committerMorris Jobke <hey@morrisjobke.de>
Mon, 6 Nov 2017 14:23:35 +0000 (15:23 +0100)
Object storage instances always fall back to the content based mimetype detection, because the file name for object storage was always random due to the fact that it was temporarily storage in a generated temp file. This patch adds a check before that to make sure to use the original file name for this purpose and also remove possible other extensions like the versioning or part file extension.

Signed-off-by: Morris Jobke <hey@morrisjobke.de>
lib/private/Files/ObjectStore/ObjectStoreStorage.php
lib/private/Files/Type/Detection.php
tests/lib/Files/Type/DetectionTest.php

index ded69e8079bd1266dc384dd7bc83d6b4a4a3f608..15df808684b04b596a81c1c667094027f1da0307 100644 (file)
@@ -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);
index cd4ddc2f06745313dce2c6360697410599d4ca7e..928c68251cfe148c444147da50423b704a680c9c 100644 (file)
@@ -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 .
index 5c1f48a806e03e15dff6768cf3460664862a4245..81c3bfe876e8ebb7d380fa88802768113de4f3cf 100644 (file)
@@ -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() {