From 9cdd637050a3a60ff58f78fff84088adce37051e Mon Sep 17 00:00:00 2001 From: Roeland Jago Douma Date: Wed, 15 Jul 2015 10:04:34 +0200 Subject: [PATCH] Loading of mapping/aliases is done in class --- lib/private/files/type/detection.php | 59 ++++++++++++++++++++++------ lib/private/server.php | 14 +------ tests/lib/files/type/detection.php | 9 ----- 3 files changed, 48 insertions(+), 34 deletions(-) diff --git a/lib/private/files/type/detection.php b/lib/private/files/type/detection.php index 35348c41a6..d69b52bba3 100644 --- a/lib/private/files/type/detection.php +++ b/lib/private/files/type/detection.php @@ -37,8 +37,8 @@ use OCP\IURLGenerator; * @package OC\Files\Type */ class Detection implements IMimeTypeDetector { - protected $mimetypes = array(); - protected $secureMimeTypes = array(); + protected $mimetypes = []; + protected $secureMimeTypes = []; protected $mimetypeIcons = []; /** @var string[] */ @@ -88,6 +88,44 @@ class Detection implements IMimeTypeDetector { } } + /** + * Add the mimetype aliases if they are not yet present + */ + private function loadAliases() { + if (!empty($this->mimeTypeAlias)) { + return; + } + + $file = file_get_contents(\OC::$configDir . '/mimetypealiases.dist.json'); + $this->mimeTypeAlias = get_object_vars(json_decode($file)); + + if (file_exists(\OC::$configDir . '/mimetypealiases.json')) { + $custom = get_object_vars(json_decode(file_get_contents(\OC::$configDir . '/mimetypealiases.json'))); + $this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom); + } + } + + /** + * Add mimetype mappings if they are not yet present + */ + private function loadMappings() { + if (!empty($this->mimetypes)) { + return; + } + + $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); + $mimetypemapping = get_object_vars(json_decode($dist)); + + //Check if need to load custom mappings + if (file_exists(\OC::$configDir . '/mimetypemapping.json')) { + $custom = file_get_contents(\OC::$configDir . '/mimetypemapping.json'); + $custom_mapping = get_object_vars(json_decode($custom)); + $mimetypemapping = array_merge($mimetypemapping, $custom_mapping); + } + + $this->registerTypeArray($mimetypemapping); + } + /** * detect mimetype only based on filename, content of file is not used * @@ -95,6 +133,8 @@ class Detection implements IMimeTypeDetector { * @return string */ public function detectPath($path) { + $this->loadMappings(); + if (strpos($path, '.')) { //try to guess the type by the file extension $extension = strtolower(strrchr(basename($path), ".")); @@ -114,6 +154,8 @@ class Detection implements IMimeTypeDetector { * @return string */ public function detect($path) { + $this->loadMappings(); + if (@is_dir($path)) { // directories are easy return "httpd/unix-directory"; @@ -184,6 +226,8 @@ class Detection implements IMimeTypeDetector { * @return string */ public function getSecureMimeType($mimeType) { + $this->loadMappings(); + return isset($this->secureMimeTypes[$mimeType]) ? $this->secureMimeTypes[$mimeType] : 'application/octet-stream'; @@ -195,16 +239,7 @@ class Detection implements IMimeTypeDetector { * @return string the url */ public function mimeTypeIcon($mimetype) { - // On first access load the list of mimetype aliases - if (empty($this->mimeTypeAlias)) { - $file = file_get_contents(\OC::$configDir . '/mimetypealiases.dist.json'); - $this->mimeTypeAlias = get_object_vars(json_decode($file)); - - if (file_exists(\OC::$configDir . '/mimetypealiases.json')) { - $custom = get_object_vars(json_decode(file_get_contents(\OC::$configDir . '/mimetypealiases.json'))); - $this->mimeTypeAlias = array_merge($this->mimeTypeAlias, $custom); - } - } + $this->loadAliases(); if (isset($this->mimeTypeAlias[$mimetype])) { $mimetype = $this->mimeTypeAlias[$mimetype]; diff --git a/lib/private/server.php b/lib/private/server.php index b835fbd4cb..82b2d6765d 100644 --- a/lib/private/server.php +++ b/lib/private/server.php @@ -445,19 +445,7 @@ class Server extends SimpleContainer implements IServerContainer { return new \OC\Files\Mount\Manager(); }); $this->registerService('MimeTypeDetector', function(Server $c) { - $mimeTypeDetector = new \OC\Files\Type\Detection($c->getURLGenerator()); - $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); - $mimetypemapping = get_object_vars(json_decode($dist)); - - //Check if need to load custom mappings - if (file_exists(\OC::$configDir . '/mimetypemapping.json')) { - $custom = file_get_contents(\OC::$configDir . '/mimetypemapping.json'); - $custom_mapping = get_object_vars(json_decode($custom)); - $mimetypemapping = array_merge($mimetypemapping, $custom_mapping); - } - - $mimeTypeDetector->registerTypeArray($mimetypemapping); - return $mimeTypeDetector; + return new \OC\Files\Type\Detection($c->getURLGenerator()); }); } diff --git a/tests/lib/files/type/detection.php b/tests/lib/files/type/detection.php index ce4c437c9d..7bd4c43054 100644 --- a/tests/lib/files/type/detection.php +++ b/tests/lib/files/type/detection.php @@ -52,9 +52,6 @@ class DetectionTest extends \Test\TestCase { public function testGetSecureMimeType() { $detection = new Detection(\OC::$server->getURLGenerator()); - $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); - $mimetypemapping = get_object_vars(json_decode($dist)); - $detection->registerTypeArray($mimetypemapping); $result = $detection->getSecureMimeType('image/svg+xml'); $expected = 'text/plain'; @@ -67,9 +64,6 @@ class DetectionTest extends \Test\TestCase { public function testDetectPath() { $detection = new Detection(\OC::$server->getURLGenerator()); - $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); - $mimetypemapping = get_object_vars(json_decode($dist)); - $detection->registerTypeArray($mimetypemapping); $this->assertEquals('text/plain', $detection->detectPath('foo.txt')); $this->assertEquals('image/png', $detection->detectPath('foo.png')); @@ -85,9 +79,6 @@ class DetectionTest extends \Test\TestCase { } $detection = new Detection(\OC::$server->getURLGenerator()); - $dist = file_get_contents(\OC::$configDir . '/mimetypemapping.dist.json'); - $mimetypemapping = get_object_vars(json_decode($dist)); - $detection->registerTypeArray($mimetypemapping); $result = $detection->detectString("/data/data.tar.gz"); $expected = 'text/plain; charset=us-ascii';