aboutsummaryrefslogtreecommitdiffstats
path: root/lib/private/Preview/MimeIconProvider.php
blob: 80545bd40636876225f1850b32ebfd9f3c3ebee0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
<?php
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
namespace OC\Preview;

use OCA\Theming\ThemingDefaults;
use OCP\App\IAppManager;
use OCP\Files\IMimeTypeDetector;
use OCP\IConfig;
use OCP\IURLGenerator;
use OCP\Preview\IMimeIconProvider;

class MimeIconProvider implements IMimeIconProvider {
	public function __construct(
		protected IMimeTypeDetector $mimetypeDetector,
		protected IConfig $config,
		protected IURLGenerator $urlGenerator,
		protected IAppManager $appManager,
		protected ThemingDefaults $themingDefaults,
	) {
	}

	public function getMimeIconUrl(string $mime): ?string {
		if (!$mime) {
			return null;
		}

		// Fetch all the aliases
		$aliases = $this->mimetypeDetector->getAllAliases();

		// Remove comments
		$aliases = array_filter($aliases, static function (string $key) {
			return !($key === '' || $key[0] === '_');
		}, ARRAY_FILTER_USE_KEY);

		// Map all the aliases recursively
		foreach ($aliases as $alias => $value) {
			if ($alias === $mime) {
				$mime = $value;
			}
		}

		$fileName = str_replace('/', '-', $mime);
		if ($url = $this->searchfileName($fileName)) {
			return $url;
		}

		$mimeType = explode('/', $mime)[0];
		if ($url = $this->searchfileName($mimeType)) {
			return $url;
		}

		return null;
	}
	
	private function searchfileName(string $fileName): ?string {
		// If the file exists in the current enabled legacy
		// custom theme, let's return it
		$theme = $this->config->getSystemValue('theme', '');
		if (!empty($theme)) {
			$path = "/themes/$theme/core/img/filetypes/$fileName.svg";
			if (file_exists(\OC::$SERVERROOT . $path)) {
				return $this->urlGenerator->getAbsoluteURL($path);
			}
		}
		
		// Previously, we used to pass this through Theming
		// But it was only used to colour icons containing
		// 0082c9. Since with vue we moved to inline svg icons,
		// we can just use the default core icons.

		// Finally, if the file exists in core, let's return it
		$path = "/core/img/filetypes/$fileName.svg";
		if (file_exists(\OC::$SERVERROOT . $path)) {
			return $this->urlGenerator->getAbsoluteURL($path);
		}

		return null;
	}
}