diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2015-07-15 10:04:34 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2015-07-27 14:59:49 +0200 |
commit | 9cdd637050a3a60ff58f78fff84088adce37051e (patch) | |
tree | 686ef9cd3fb1b68268294bc6906685cb5863e4c4 /lib | |
parent | 141a0f0f476a6675c0db9044afc7447342197f45 (diff) | |
download | nextcloud-server-9cdd637050a3a60ff58f78fff84088adce37051e.tar.gz nextcloud-server-9cdd637050a3a60ff58f78fff84088adce37051e.zip |
Loading of mapping/aliases is done in class
Diffstat (limited to 'lib')
-rw-r--r-- | lib/private/files/type/detection.php | 59 | ||||
-rw-r--r-- | lib/private/server.php | 14 |
2 files changed, 48 insertions, 25 deletions
diff --git a/lib/private/files/type/detection.php b/lib/private/files/type/detection.php index 35348c41a65..d69b52bba3d 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[] */ @@ -89,12 +89,52 @@ 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 * * @param string $path * @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 b835fbd4cbf..82b2d6765dc 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()); }); } |