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
*/
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];

View File

@ -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());
});
}

View File

@ -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';