summaryrefslogtreecommitdiffstats
path: root/lib/private
diff options
context:
space:
mode:
authorMorris Jobke <hey@morrisjobke.de>2015-07-28 13:35:26 +0200
committerMorris Jobke <hey@morrisjobke.de>2015-07-28 13:35:26 +0200
commitc34e63bb1f9752892d3c36b50e461b284f822a36 (patch)
tree760bce9904c5c05d6edfc32b3193fa95191ed800 /lib/private
parent73169b0edbc1086d2a287c26640e2a2428449971 (diff)
parentd276aebf4099acd54dff7e727e25d28debf441b8 (diff)
downloadnextcloud-server-c34e63bb1f9752892d3c36b50e461b284f822a36.tar.gz
nextcloud-server-c34e63bb1f9752892d3c36b50e461b284f822a36.zip
Merge pull request #15543 from rullzer/mimetypedetector
Mimetypedetector
Diffstat (limited to 'lib/private')
-rw-r--r--lib/private/files/type/detection.php127
-rw-r--r--lib/private/helper.php99
-rw-r--r--lib/private/server.php15
3 files changed, 151 insertions, 90 deletions
diff --git a/lib/private/files/type/detection.php b/lib/private/files/type/detection.php
index 3287375bc79..ba286637df3 100644
--- a/lib/private/files/type/detection.php
+++ b/lib/private/files/type/detection.php
@@ -4,6 +4,7 @@
* @author Jens-Christian Fischer <jens-christian.fischer@switch.ch>
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Thomas Tanghus <thomas@tanghus.net>
*
* @copyright Copyright (c) 2015, ownCloud, Inc.
@@ -25,6 +26,9 @@
namespace OC\Files\Type;
+use OCP\Files\IMimeTypeDetector;
+use OCP\IURLGenerator;
+
/**
* Class Detection
*
@@ -32,9 +36,28 @@ namespace OC\Files\Type;
*
* @package OC\Files\Type
*/
-class Detection {
- protected $mimetypes = array();
- protected $secureMimeTypes = array();
+class Detection implements IMimeTypeDetector {
+ protected $mimetypes = [];
+ protected $secureMimeTypes = [];
+
+ protected $mimetypeIcons = [];
+ /** @var string[] */
+ protected $mimeTypeAlias = [];
+
+ /** @var IURLGenerator */
+ private $urlGenerator;
+
+ /** @var string */
+ private $configDir;
+
+ /**
+ * @param IURLGenerator $urlGenerator
+ * @param string $configDir
+ */
+ public function __construct(IURLGenerator $urlGenerator, $configDir) {
+ $this->urlGenerator = $urlGenerator;
+ $this->configDir = $configDir;
+ }
/**
* Add an extension -> mimetype mapping
@@ -71,12 +94,52 @@ class Detection {
}
/**
+ * Add the mimetype aliases if they are not yet present
+ */
+ private function loadAliases() {
+ if (!empty($this->mimeTypeAlias)) {
+ return;
+ }
+
+ $file = file_get_contents($this->configDir . '/mimetypealiases.dist.json');
+ $this->mimeTypeAlias = get_object_vars(json_decode($file));
+
+ if (file_exists($this->configDir . '/mimetypealiases.json')) {
+ $custom = get_object_vars(json_decode(file_get_contents($this->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($this->configDir . '/mimetypemapping.dist.json');
+ $mimetypemapping = get_object_vars(json_decode($dist));
+
+ //Check if need to load custom mappings
+ if (file_exists($this->configDir . '/mimetypemapping.json')) {
+ $custom = file_get_contents($this->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), "."));
@@ -96,6 +159,8 @@ class Detection {
* @return string
*/
public function detect($path) {
+ $this->loadMappings();
+
if (@is_dir($path)) {
// directories are easy
return "httpd/unix-directory";
@@ -166,8 +231,64 @@ class Detection {
* @return string
*/
public function getSecureMimeType($mimeType) {
+ $this->loadMappings();
+
return isset($this->secureMimeTypes[$mimeType])
? $this->secureMimeTypes[$mimeType]
: 'application/octet-stream';
}
+
+ /**
+ * Get path to the icon of a file type
+ * @param string $mimeType the MIME type
+ * @return string the url
+ */
+ public function mimeTypeIcon($mimetype) {
+ $this->loadAliases();
+
+ 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] = $this->urlGenerator->imagePath('core', 'filetypes/folder.png');
+ return $this->mimetypeIcons[$mimetype];
+ }
+ if ($mimetype === 'dir-shared') {
+ $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-shared.png');
+ return $this->mimetypeIcons[$mimetype];
+ }
+ if ($mimetype === 'dir-external') {
+ $this->mimetypeIcons[$mimetype] = $this->urlGenerator->imagePath('core', 'filetypes/folder-external.png');
+ return $this->mimetypeIcons[$mimetype];
+ }
+
+ // Icon exists?
+ 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, '-'));
+ 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];
+ }
}
diff --git a/lib/private/helper.php b/lib/private/helper.php
index ed954f87630..b8e4b451835 100644
--- a/lib/private/helper.php
+++ b/lib/private/helper.php
@@ -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,64 +179,10 @@ class OC_Helper {
* @return string the url
*
* Returns the path to the image of this file type.
+ * @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);
}
/**
@@ -431,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();
}
/**
@@ -465,9 +394,10 @@ class OC_Helper {
*
* @param string $path
* @return string
+ * @deprecated 8.2.0 Use \OC::$server->getMimeTypeDetector()->detectPath($path)
*/
static public function getFileNameMimeType($path) {
- return self::getMimetypeDetector()->detectPath($path);
+ return \OC::$server->getMimeTypeDetector()->detectPath($path);
}
/**
@@ -476,9 +406,10 @@ class OC_Helper {
* @param string $path
* @return string
* does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
+ * @deprecated 8.2.0 Use \OC::$server->getMimeTypeDetector()->detect($path)
*/
static function getMimeType($path) {
- return self::getMimetypeDetector()->detect($path);
+ return \OC::$server->getMimeTypeDetector()->detect($path);
}
/**
@@ -486,9 +417,10 @@ class OC_Helper {
*
* @param string $mimeType
* @return string
+ * @deprecated 8.2.0 Use \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType)
*/
static function getSecureMimeType($mimeType) {
- return self::getMimetypeDetector()->getSecureMimeType($mimeType);
+ return \OC::$server->getMimeTypeDetector()->getSecureMimeType($mimeType);
}
/**
@@ -496,20 +428,13 @@ class OC_Helper {
*
* @param string $data
* @return string
+ * @deprecated 8.2.0 Use \OC::$server->getMimeTypeDetector()->detectString($data)
*/
static function getStringMimeType($data) {
- return self::getMimetypeDetector()->detectString($data);
+ return \OC::$server->getMimeTypeDetector()->detectString($data);
}
/**
- * Checks $_REQUEST contains a var for the $s key. If so, returns the html-escaped value of this var; otherwise returns the default value provided by $d.
- * @param string $s name of the var to escape, if set.
- * @param string $d default value.
- * @return string the print-safe value.
- *
- */
-
- /**
* detect if a given program is found in the search PATH
*
* @param string $name
diff --git a/lib/private/server.php b/lib/private/server.php
index 53949b53df7..12981fe7f19 100644
--- a/lib/private/server.php
+++ b/lib/private/server.php
@@ -12,6 +12,7 @@
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <icewind@owncloud.com>
* @author Robin McCorkell <rmccorkell@karoshi.org.uk>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
* @author Sander <brantje@gmail.com>
* @author Thomas Müller <thomas.mueller@tmit.eu>
* @author Thomas Tanghus <thomas@tanghus.net>
@@ -443,6 +444,11 @@ class Server extends SimpleContainer implements IServerContainer {
$this->registerService('MountManager', function () {
return new \OC\Files\Mount\Manager();
});
+ $this->registerService('MimeTypeDetector', function(Server $c) {
+ return new \OC\Files\Type\Detection(
+ $c->getURLGenerator(),
+ \OC::$configDir);
+ });
}
/**
@@ -930,4 +936,13 @@ class Server extends SimpleContainer implements IServerContainer {
function getMountManager() {
return $this->query('MountManager');
}
+
+ /*
+ * Get the MimeTypeDetector
+ *
+ * @return \OCP\Files\IMimeTypeDetector
+ */
+ public function getMimeTypeDetector() {
+ return $this->query('MimeTypeDetector');
+ }
}