Move mimetypedetection to files/type/detection

This commit is contained in:
Roeland Jago Douma 2015-07-14 08:43:14 +02:00
parent 6db6689740
commit 141a0f0f47
4 changed files with 64 additions and 172 deletions

View File

@ -27,6 +27,7 @@
namespace OC\Files\Type;
use OCP\Files\IMimeTypeDetector;
use OCP\IURLGenerator;
/**
* Class Detection
@ -41,78 +42,17 @@ class Detection implements IMimeTypeDetector {
protected $mimetypeIcons = [];
/** @var string[] */
protected $mimeTypeAlias = [
'application/octet-stream' => 'file', // use file icon as fallback
protected $mimeTypeAlias = [];
'application/illustrator' => 'image/vector',
'application/postscript' => 'image/vector',
'image/svg+xml' => 'image/vector',
'application/coreldraw' => 'image',
'application/x-gimp' => 'image',
'application/x-photoshop' => 'image',
'application/x-dcraw' => 'image',
'application/font-sfnt' => 'font',
'application/x-font' => 'font',
'application/font-woff' => 'font',
'application/vnd.ms-fontobject' => 'font',
'application/json' => 'text/code',
'application/x-perl' => 'text/code',
'application/x-php' => 'text/code',
'text/x-shellscript' => 'text/code',
'application/yaml' => 'text/code',
'application/xml' => 'text/html',
'text/css' => 'text/code',
'application/x-tex' => 'text',
'application/x-compressed' => 'package/x-generic',
'application/x-7z-compressed' => 'package/x-generic',
'application/x-deb' => 'package/x-generic',
'application/x-gzip' => 'package/x-generic',
'application/x-rar-compressed' => 'package/x-generic',
'application/x-tar' => 'package/x-generic',
'application/vnd.android.package-archive' => 'package/x-generic',
'application/zip' => 'package/x-generic',
'application/msword' => 'x-office/document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.document' => 'x-office/document',
'application/vnd.openxmlformats-officedocument.wordprocessingml.template' => 'x-office/document',
'application/vnd.ms-word.document.macroEnabled.12' => 'x-office/document',
'application/vnd.ms-word.template.macroEnabled.12' => 'x-office/document',
'application/vnd.oasis.opendocument.text' => 'x-office/document',
'application/vnd.oasis.opendocument.text-template' => 'x-office/document',
'application/vnd.oasis.opendocument.text-web' => 'x-office/document',
'application/vnd.oasis.opendocument.text-master' => 'x-office/document',
'application/mspowerpoint' => 'x-office/presentation',
'application/vnd.ms-powerpoint' => 'x-office/presentation',
'application/vnd.openxmlformats-officedocument.presentationml.presentation' => 'x-office/presentation',
'application/vnd.openxmlformats-officedocument.presentationml.template' => 'x-office/presentation',
'application/vnd.openxmlformats-officedocument.presentationml.slideshow' => 'x-office/presentation',
'application/vnd.ms-powerpoint.addin.macroEnabled.12' => 'x-office/presentation',
'application/vnd.ms-powerpoint.presentation.macroEnabled.12' => 'x-office/presentation',
'application/vnd.ms-powerpoint.template.macroEnabled.12' => 'x-office/presentation',
'application/vnd.ms-powerpoint.slideshow.macroEnabled.12' => 'x-office/presentation',
'application/vnd.oasis.opendocument.presentation' => 'x-office/presentation',
'application/vnd.oasis.opendocument.presentation-template' => 'x-office/presentation',
'application/msexcel' => 'x-office/spreadsheet',
'application/vnd.ms-excel' => 'x-office/spreadsheet',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' => 'x-office/spreadsheet',
'application/vnd.openxmlformats-officedocument.spreadsheetml.template' => 'x-office/spreadsheet',
'application/vnd.ms-excel.sheet.macroEnabled.12' => 'x-office/spreadsheet',
'application/vnd.ms-excel.template.macroEnabled.12' => 'x-office/spreadsheet',
'application/vnd.ms-excel.addin.macroEnabled.12' => 'x-office/spreadsheet',
'application/vnd.ms-excel.sheet.binary.macroEnabled.12' => 'x-office/spreadsheet',
'application/vnd.oasis.opendocument.spreadsheet' => 'x-office/spreadsheet',
'application/vnd.oasis.opendocument.spreadsheet-template' => 'x-office/spreadsheet',
'text/csv' => 'x-office/spreadsheet',
'application/msaccess' => 'database',
];
/** @var IURLGenerator */
private $urlGenerator;
/**
* @param IURLGenerator $urlGenerator
*/
public function __construct(IURLGenerator $urlGenerator) {
$this->urlGenerator = $urlGenerator;
}
/**
* Add an extension -> mimetype mapping
@ -255,44 +195,61 @@ 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);
}
}
if (isset($this->mimeTypeAlias[$mimetype])) {
$mimetype = $this->mimeTypeAlias[$mimetype];
}
if (isset($this->mimetypeIcons[$mimetype])) {
return $this->mimetypeIcons[$mimetype];
}
// Replace slash and backslash with a minus
$icon = str_replace('/', '-', $mimetype);
$icon = str_replace('\\', '-', $icon);
// Is it a dir?
if ($mimetype === 'dir') {
$this->mimetypeIcons[$mimetype] = OC::$WEBROOT . '/core/img/filetypes/folder.png';
return OC::$WEBROOT . '/core/img/filetypes/folder.png';
$this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder.png');
return $this->mimetypeIcons[$mimetype];
}
if ($mimetype === 'dir-shared') {
$this->mimetypeIcons[$mimetype] = OC::$WEBROOT . '/core/img/filetypes/folder-shared.png';
return OC::$WEBROOT . '/core/img/filetypes/folder-shared.png';
$this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.png');
return $this->mimetypeIcons[$mimetype];
}
if ($mimetype === 'dir-external') {
$this->mimetypeIcons[$mimetype] = OC::$WEBROOT . '/core/img/filetypes/folder-external.png';
return OC::$WEBROOT . '/core/img/filetypes/folder-external.png';
$this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.
png');
return $this->mimetypeIcons[$mimetype];
}
// Icon exists?
if (file_exists(OC::$SERVERROOT . '/core/img/filetypes/' . $icon . '.png')) {
$this->mimetypeIcons[$mimetype] = OC::$WEBROOT . '/core/img/filetypes/' . $icon . '.png';
return OC::$WEBROOT . '/core/img/filetypes/' . $icon . '.png';
try {
$this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $icon . '.png');
return $this->mimetypeIcons[$mimetype];
} catch (\RuntimeException $e) {
// Specified image not found
}
// Try only the first part of the filetype
$mimePart = substr($icon, 0, strpos($icon, '-'));
if (file_exists(OC::$SERVERROOT . '/core/img/filetypes/' . $mimePart . '.png')) {
$this->mimetypeIcons[$mimetype] = OC::$WEBROOT . '/core/img/filetypes/' . $mimePart . '.png';
return OC::$WEBROOT . '/core/img/filetypes/' . $mimePart . '.png';
} else {
$this->mimetypeIcons[$mimetype] = OC::$WEBROOT . '/core/img/filetypes/file.png';
return OC::$WEBROOT . '/core/img/filetypes/file.png';
try {
$this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/' . $mimePart . '.png');
return $this->mimetypeIcons[$mimetype];
} catch (\RuntimeException $e) {
// Image for the first part of the mimetype not found
}
$this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/file.png');
return $this->mimetypeIcons[$mimetype];
}
}

View File

@ -50,11 +50,7 @@ use Symfony\Component\Process\ExecutableFinder;
* Collection of useful functions
*/
class OC_Helper {
private static $mimetypeIcons = array();
private static $mimetypeDetector;
private static $templateManager;
/** @var string[] */
private static $mimeTypeAlias = [];
/**
* Creates an url using a defined route
@ -183,65 +179,10 @@ class OC_Helper {
* @return string the url
*
* Returns the path to the image of this file type.
* @deprecated Use \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype)
* @deprecated 8.2.0 Use \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype)
*/
public static function mimetypeIcon($mimetype) {
// On first access load the list of mimetype aliases
if (empty(self::$mimeTypeAlias)) {
$file = file_get_contents(OC::$SERVERROOT . '/config/mimetypealiases.dist.json');
self::$mimeTypeAlias = get_object_vars(json_decode($file));
if (file_exists(\OC::$SERVERROOT . '/config/mimetypealiases.json')) {
$custom = get_object_vars(json_decode(file_get_contents(\OC::$SERVERROOT . '/config/mimetypealiases.json')));
self::$mimeTypeAlias = array_merge(self::$mimeTypeAlias, $custom);
}
}
if (isset(self::$mimeTypeAlias[$mimetype])) {
$mimetype = self::$mimeTypeAlias[$mimetype];
}
if (isset(self::$mimetypeIcons[$mimetype])) {
return self::$mimetypeIcons[$mimetype];
}
// Replace slash and backslash with a minus
$icon = str_replace('/', '-', $mimetype);
$icon = str_replace('\\', '-', $icon);
// Is it a dir?
if ($mimetype === 'dir') {
self::$mimetypeIcons[$mimetype] = \OC::$server->getURLGenerator()->imagePath('core', 'filetypes/folder.png');
return self::$mimetypeIcons[$mimetype];
}
if ($mimetype === 'dir-shared') {
self::$mimetypeIcons[$mimetype] = \OC::$server->getURLGenerator()->imagePath('core', 'filetypes/folder-shared.png');
return self::$mimetypeIcons[$mimetype];
}
if ($mimetype === 'dir-external') {
self::$mimetypeIcons[$mimetype] = \OC::$server->getURLGenerator()->imagePath('core', 'filetypes/folder-external.png');
return self::$mimetypeIcons[$mimetype];
}
// Icon exists?
try {
self::$mimetypeIcons[$mimetype] = \OC::$server->getURLGenerator()->imagePath('core', 'filetypes/' . $icon . '.png');
return self::$mimetypeIcons[$mimetype];
} catch (\RuntimeException $e) {
// Specified image not found
}
// Try only the first part of the filetype
$mimePart = substr($icon, 0, strpos($icon, '-'));
try {
self::$mimetypeIcons[$mimetype] = \OC::$server->getURLGenerator()->imagePath('core', 'filetypes/' . $mimePart . '.png');
return self::$mimetypeIcons[$mimetype];
} catch (\RuntimeException $e) {
// Image for the first part of the mimetype not found
}
self::$mimetypeIcons[$mimetype] = \OC::$server->getURLGenerator()->imagePath('core', 'filetypes/file.png');
return self::$mimetypeIcons[$mimetype];
return \OC::$server->getMimeTypeDetector()->mimeTypeIcon($mimetype);
}
/**
@ -432,23 +373,10 @@ class OC_Helper {
/**
* @return \OC\Files\Type\Detection
* @deprecated 8.2.0 use \OC::$server->getMimeTypeDetector()
*/
static public function getMimetypeDetector() {
if (!self::$mimetypeDetector) {
$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);
}
self::$mimetypeDetector = new \OC\Files\Type\Detection();
self::$mimetypeDetector->registerTypeArray($mimetypemapping);
}
return self::$mimetypeDetector;
return \OC::$server->getMimeTypeDetector();
}
/**
@ -469,7 +397,7 @@ class OC_Helper {
* @deprecated Use \OC::$server->getMimeTypeDetector()->detectPath($path)
*/
static public function getFileNameMimeType($path) {
return self::getMimetypeDetector()->detectPath($path);
return \OC::$server->getMimeTypeDetector()->detectPath($path);
}
/**
@ -481,7 +409,7 @@ class OC_Helper {
* @deprecated Use \OC::$server->getMimeTypeDetector()->detect($path)
*/
static function getMimeType($path) {
return self::getMimetypeDetector()->detect($path);
return \OC::$server->getMimeTypeDetector()->detect($path);
}
/**
@ -492,7 +420,7 @@ class OC_Helper {
* @deprecated Use \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType)
*/
static function getSecureMimeType($mimeType) {
return self::getMimetypeDetector()->getSecureMimeType($mimeType);
return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType);
}
/**
@ -500,10 +428,10 @@ class OC_Helper {
*
* @param string $data
* @return string
* @deprecated Use \OC::$server->getMimeTypeDetector->detectString($data)
* @deprecated Use \OC::$server->getMimeTypeDetector()->detectString($data)
*/
static function getStringMimeType($data) {
return self::getMimetypeDetector()->detectString($data);
return \OC::$server->getMimeTypeDetector()->detectString($data);
}
/**

View File

@ -59,7 +59,6 @@ use OC\Security\SecureRandom;
use OC\Security\TrustedDomainHelper;
use OC\Tagging\TagMapper;
use OCP\IServerContainer;
use OC\Files\Type\Detection;
/**
* Class Server
@ -446,9 +445,17 @@ class Server extends SimpleContainer implements IServerContainer {
return new \OC\Files\Mount\Manager();
});
$this->registerService('MimeTypeDetector', function(Server $c) {
$mimeTypeDetector = new Detection();
$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;
});

View File

@ -26,7 +26,7 @@ use \OC\Files\Type\Detection;
class DetectionTest extends \Test\TestCase {
public function testDetect() {
$detection = new Detection();
$detection = new Detection(\OC::$server->getURLGenerator());
$dir = \OC::$SERVERROOT.'/tests/data';
$result = $detection->detect($dir."/");
@ -51,7 +51,7 @@ class DetectionTest extends \Test\TestCase {
}
public function testGetSecureMimeType() {
$detection = new Detection();
$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);
@ -66,7 +66,7 @@ class DetectionTest extends \Test\TestCase {
}
public function testDetectPath() {
$detection = new Detection();
$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);
@ -84,7 +84,7 @@ class DetectionTest extends \Test\TestCase {
$this->markTestSkipped('[Windows] Strings have mimetype application/octet-stream on Windows');
}
$detection = new Detection();
$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);