Browse Source

Merge pull request #6958 from nextcloud/improved-mimetype-detection-for-object-storage

Improve mimetype detection for object storages
tags/v13.0.0beta1
Joas Schilling 6 years ago
parent
commit
3e0789aa94
No account linked to committer's email address

+ 9
- 1
lib/private/Files/ObjectStore/ObjectStoreStorage.php View 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);

+ 4
- 0
lib/private/Files/Type/Detection.php View 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 .

+ 2
- 0
tests/lib/Files/Type/DetectionTest.php View File

@@ -84,6 +84,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() {

Loading…
Cancel
Save