diff --git a/lib/private/files/type/detection.php b/lib/private/files/type/detection.php
index 3287375bc79a33e79906011f999e12678ff26855..ba286637df38ba8043f701d55366eaee85017d67 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
@@ -70,6 +93,44 @@ 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
 	 *
@@ -77,6 +138,8 @@ class Detection {
 	 * @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 ed954f876305c077e1b3c6e016c55edbc9ad36d5..b8e4b4518357bedd106df0af4fd0529fabcec5b0 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,19 +428,12 @@ 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
 	 *
diff --git a/lib/private/server.php b/lib/private/server.php
index 53949b53df70d8b93f495ab11478b81b19825c94..12981fe7f197e0402234232cdd25407f8d89c2d3 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');
+	}
 }
diff --git a/lib/public/files/imimetypedetector.php b/lib/public/files/imimetypedetector.php
new file mode 100644
index 0000000000000000000000000000000000000000..79ed8a4fac98dd04545a0001a02d3bf091d33edd
--- /dev/null
+++ b/lib/public/files/imimetypedetector.php
@@ -0,0 +1,78 @@
+<?php
+/**
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+// use OCP namespace for all classes that are considered public.
+// This means that they should be used by apps instead of the internal ownCloud classes
+namespace OCP\Files;
+
+
+/**
+ * Interface IMimeTypeDetector
+ * @package OCP\Files
+ * @since 8.2.0
+ *
+ * Interface to handle mimetypes (detection and icon retrieval)
+ **/
+interface IMimeTypeDetector {
+
+	/**
+	 * detect mimetype only based on filename, content of file is not used
+	 * @param string $path
+	 * @return string
+	 * @since 8.2.0
+	 **/
+	public function detectPath($path);
+
+	/**
+	 * detect mimetype based on both filename and content
+	 *
+	 * @param string $path
+	 * @return string
+	 * @since 8.2.0
+	 */
+	public function detect($path);
+
+	/**
+	 * Get a secure mimetype that won't expose potential XSS.
+	 *
+	 * @param string $mimeType
+	 * @return string
+	 * @since 8.2.0
+	 */
+	public function getSecureMimeType($mimeType);
+
+	/**
+	 * detect mimetype based on the content of a string
+	 *
+	 * @param string $data
+	 * @return string
+	 * @since 8.2.0
+	 */
+	public function detectString($data);
+
+	/**
+	 * Get path to the icon of a file type
+	 * @param string $mimeType the MIME type
+	 * @return string the url
+	 * @since 8.2.0
+	 */
+	public function mimeTypeIcon($mimeType);
+}
diff --git a/lib/public/iservercontainer.php b/lib/public/iservercontainer.php
index 95ee853d84c39f8225cc56cdd5b1cb87133af219..f3165db33da8b075515f2ab0c2cde2d80b69de45 100644
--- a/lib/public/iservercontainer.php
+++ b/lib/public/iservercontainer.php
@@ -9,6 +9,7 @@
  * @author Lukas Reschke <lukas@owncloud.com>
  * @author Morris Jobke <hey@morrisjobke.de>
  * @author Robin Appelman <icewind@owncloud.com>
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
  * @author Thomas Müller <thomas.mueller@tmit.eu>
  * @author Thomas Tanghus <thomas@tanghus.net>
  *
@@ -429,4 +430,12 @@ interface IServerContainer {
 	 * @since 8.2.0
 	 */
 	public function getMountManager();
+
+	/**
+	 * Get the MimeTypeDetector
+	 *
+	 * @return \OCP\Files\IMimeTypeDetector
+	 * @since 8.2.0
+	 */
+	public function getMimeTypeDetector();
 }
diff --git a/tests/lib/files/type/detection.php b/tests/lib/files/type/detection.php
new file mode 100644
index 0000000000000000000000000000000000000000..1997fa4dfd6161225e774fb93af93c32411b86b5
--- /dev/null
+++ b/tests/lib/files/type/detection.php
@@ -0,0 +1,283 @@
+<?php
+/**
+ * @author Roeland Jago Douma <roeland@famdouma.nl>
+ *
+ * @copyright Copyright (c) 2015, ownCloud, Inc.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>
+ *
+ */
+
+namespace OC\Files\Type;
+
+use \OC\Files\Type\Detection;
+
+class DetectionTest extends \Test\TestCase {
+
+	public function testDetect() {
+		$detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
+		$dir = \OC::$SERVERROOT.'/tests/data';
+
+		$result = $detection->detect($dir."/");
+		$expected = 'httpd/unix-directory';
+		$this->assertEquals($expected, $result);
+
+		$result = $detection->detect($dir."/data.tar.gz");
+		$expected = 'application/x-gzip';
+		$this->assertEquals($expected, $result);
+
+		$result = $detection->detect($dir."/data.zip");
+		$expected = 'application/zip';
+		$this->assertEquals($expected, $result);
+
+		$result = $detection->detect($dir."/testimagelarge.svg");
+		$expected = 'image/svg+xml';
+		$this->assertEquals($expected, $result);
+
+		$result = $detection->detect($dir."/testimage.png");
+		$expected = 'image/png';
+		$this->assertEquals($expected, $result);
+	}
+
+	public function testGetSecureMimeType() {
+		$detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
+
+		$result = $detection->getSecureMimeType('image/svg+xml');
+		$expected = 'text/plain';
+		$this->assertEquals($expected, $result);
+
+		$result = $detection->getSecureMimeType('image/png');
+		$expected = 'image/png';
+		$this->assertEquals($expected, $result);
+	}
+
+	public function testDetectPath() {
+		$detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
+
+		$this->assertEquals('text/plain', $detection->detectPath('foo.txt'));
+		$this->assertEquals('image/png', $detection->detectPath('foo.png'));
+		$this->assertEquals('image/png', $detection->detectPath('foo.bar.png'));
+		$this->assertEquals('application/octet-stream', $detection->detectPath('.png'));
+		$this->assertEquals('application/octet-stream', $detection->detectPath('foo'));
+		$this->assertEquals('application/octet-stream', $detection->detectPath(''));
+	}
+
+	public function testDetectString() {
+		if (\OC_Util::runningOnWindows()) {
+			$this->markTestSkipped('[Windows] Strings have mimetype application/octet-stream on Windows');
+		}
+
+		$detection = new Detection(\OC::$server->getURLGenerator(), \OC::$configDir);
+
+		$result = $detection->detectString("/data/data.tar.gz");
+		$expected = 'text/plain; charset=us-ascii';
+		$this->assertEquals($expected, $result);
+	}
+
+	public function testMimeTypeIcon() {
+		if (!class_exists('org\\bovigo\\vfs\\vfsStream')) {
+			$this->markTestSkipped('Pacakge vfsStream not installed');
+		}
+		$confDir = \org\bovigo\vfs\vfsStream::setup();
+		$mimetypealiases_dist = \org\bovigo\vfs\vfsStream::newFile('mimetypealiases.dist.json')->at($confDir);
+
+		//Empty alias file
+		$mimetypealiases_dist->setContent(json_encode([], JSON_FORCE_OBJECT));
+
+
+		/*
+		 * Test dir mimetype
+		 */
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->once())
+			->method('imagePath')
+			->with($this->equalTo('core'), $this->equalTo('filetypes/folder.png'))
+			->willReturn('folder.svg');
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('dir');
+		$this->assertEquals('folder.svg', $mimeType);
+
+
+		/*
+		 * Test dir-shareed mimetype
+		 */
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->once())
+			->method('imagePath')
+			->with($this->equalTo('core'), $this->equalTo('filetypes/folder-shared.png'))
+			->willReturn('folder-shared.svg');
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('dir-shared');
+		$this->assertEquals('folder-shared.svg', $mimeType);
+
+
+		/*
+		 * Test dir external
+		 */
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->once())
+			->method('imagePath')
+			->with($this->equalTo('core'), $this->equalTo('filetypes/folder-external.png'))
+			->willReturn('folder-external.svg');
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('dir-external');
+		$this->assertEquals('folder-external.svg', $mimeType);
+
+
+		/*
+		 * Test complete mimetype
+		 */
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->once())
+			->method('imagePath')
+			->with($this->equalTo('core'), $this->equalTo('filetypes/my-type.png'))
+			->willReturn('my-type.svg');
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('my-type');
+		$this->assertEquals('my-type.svg', $mimeType);
+
+
+		/*
+		 * Test subtype
+		 */
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->exactly(2))
+			->method('imagePath')
+			->withConsecutive(
+				[$this->equalTo('core'), $this->equalTo('filetypes/my-type.png')],
+				[$this->equalTo('core'), $this->equalTo('filetypes/my.png')]
+			)
+			->will($this->returnCallback(
+				function($appName, $file) {
+					if ($file === 'filetypes/my.png') {
+						return 'my.svg';
+					}
+					throw new \RuntimeException();
+				}
+			));
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('my-type');
+		$this->assertEquals('my.svg', $mimeType);
+
+
+		/*
+		 * Test default mimetype
+		 */
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->exactly(3))
+			->method('imagePath')
+			->withConsecutive(
+				[$this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png')],
+				[$this->equalTo('core'), $this->equalTo('filetypes/foo.png')],
+				[$this->equalTo('core'), $this->equalTo('filetypes/file.png')]
+			)
+			->will($this->returnCallback(
+				function($appName, $file) {
+					if ($file === 'filetypes/file.png') {
+						return 'file.svg';
+					}
+					throw new \RuntimeException();
+				}
+			));
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('foo-bar');
+		$this->assertEquals('file.svg', $mimeType);
+
+		/*
+		 * Test chaching
+		 */
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->once())
+			->method('imagePath')
+			->with($this->equalTo('core'), $this->equalTo('filetypes/foo-bar.png'))
+			->willReturn('foo-bar.svg');
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('foo-bar');
+		$this->assertEquals('foo-bar.svg', $mimeType);
+		$mimeType = $detection->mimeTypeIcon('foo-bar');
+		$this->assertEquals('foo-bar.svg', $mimeType);
+
+
+
+		/*
+		 * Test aliases
+		 */
+
+		//Put alias
+		$mimetypealiases_dist->setContent(json_encode(['foo' => 'foobar/baz'], JSON_FORCE_OBJECT));
+
+		//Mock UrlGenerator
+		$urlGenerator = $this->getMockBuilder('\OCP\IURLGenerator')
+			->disableOriginalConstructor()
+			->getMock();
+
+		//Only call the url generator once
+		$urlGenerator->expects($this->once())
+			->method('imagePath')
+			->with($this->equalTo('core'), $this->equalTo('filetypes/foobar-baz.png'))
+			->willReturn('foobar-baz.svg');
+
+		$detection = new Detection($urlGenerator, $confDir->url());
+		$mimeType = $detection->mimeTypeIcon('foo');
+		$this->assertEquals('foobar-baz.svg', $mimeType);
+	}
+}