Loading of mapping/aliases is done in class

This commit is contained in:
Roeland Jago Douma 2015-07-15 10:04:34 +02:00
parent 141a0f0f47
commit 9cdd637050
3 changed files with 48 additions and 34 deletions

View File

@ -37,8 +37,8 @@ use OCP\IURLGenerator;
* @package OC\Files\Type * @package OC\Files\Type
*/ */
class Detection implements IMimeTypeDetector { class Detection implements IMimeTypeDetector {
protected $mimetypes = array(); protected $mimetypes = [];
protected $secureMimeTypes = array(); protected $secureMimeTypes = [];
protected $mimetypeIcons = []; protected $mimetypeIcons = [];
/** @var string[] */ /** @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 * detect mimetype only based on filename, content of file is not used
* *
@ -95,6 +133,8 @@ class Detection implements IMimeTypeDetector {
* @return string * @return string
*/ */
public function detectPath($path) { public function detectPath($path) {
$this->loadMappings();
if (strpos($path, '.')) { if (strpos($path, '.')) {
//try to guess the type by the file extension //try to guess the type by the file extension
$extension = strtolower(strrchr(basename($path), ".")); $extension = strtolower(strrchr(basename($path), "."));
@ -114,6 +154,8 @@ class Detection implements IMimeTypeDetector {
* @return string * @return string
*/ */
public function detect($path) { public function detect($path) {
$this->loadMappings();
if (@is_dir($path)) { if (@is_dir($path)) {
// directories are easy // directories are easy
return "httpd/unix-directory"; return "httpd/unix-directory";
@ -184,6 +226,8 @@ class Detection implements IMimeTypeDetector {
* @return string * @return string
*/ */
public function getSecureMimeType($mimeType) { public function getSecureMimeType($mimeType) {
$this->loadMappings();
return isset($this->secureMimeTypes[$mimeType]) return isset($this->secureMimeTypes[$mimeType])
? $this->secureMimeTypes[$mimeType] ? $this->secureMimeTypes[$mimeType]
: 'application/octet-stream'; : 'application/octet-stream';
@ -195,16 +239,7 @@ class Detection implements IMimeTypeDetector {
* @return string the url * @return string the url
*/ */
public function mimeTypeIcon($mimetype) { public function mimeTypeIcon($mimetype) {
// On first access load the list of mimetype aliases $this->loadAliases();
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);
}
}
if (isset($this->mimeTypeAlias[$mimetype])) { if (isset($this->mimeTypeAlias[$mimetype])) {
$mimetype = $this->mimeTypeAlias[$mimetype]; $mimetype = $this->mimeTypeAlias[$mimetype];

View File

@ -445,19 +445,7 @@ class Server extends SimpleContainer implements IServerContainer {
return new \OC\Files\Mount\Manager(); return new \OC\Files\Mount\Manager();
}); });
$this->registerService('MimeTypeDetector', function(Server $c) { $this->registerService('MimeTypeDetector', function(Server $c) {
$mimeTypeDetector = new \OC\Files\Type\Detection($c->getURLGenerator()); return 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;
}); });
} }

View File

@ -52,9 +52,6 @@ class DetectionTest extends \Test\TestCase {
public function testGetSecureMimeType() { public function testGetSecureMimeType() {
$detection = new Detection(\OC::$server->getURLGenerator()); $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'); $result = $detection->getSecureMimeType('image/svg+xml');
$expected = 'text/plain'; $expected = 'text/plain';
@ -67,9 +64,6 @@ class DetectionTest extends \Test\TestCase {
public function testDetectPath() { public function testDetectPath() {
$detection = new Detection(\OC::$server->getURLGenerator()); $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('text/plain', $detection->detectPath('foo.txt'));
$this->assertEquals('image/png', $detection->detectPath('foo.png')); $this->assertEquals('image/png', $detection->detectPath('foo.png'));
@ -85,9 +79,6 @@ class DetectionTest extends \Test\TestCase {
} }
$detection = new Detection(\OC::$server->getURLGenerator()); $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"); $result = $detection->detectString("/data/data.tar.gz");
$expected = 'text/plain; charset=us-ascii'; $expected = 'text/plain; charset=us-ascii';