1
0

MimeIconProvider.php 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace OC\Preview;
  7. use OCA\Theming\ThemingDefaults;
  8. use OCP\App\IAppManager;
  9. use OCP\Files\IMimeTypeDetector;
  10. use OCP\IConfig;
  11. use OCP\IURLGenerator;
  12. use OCP\Preview\IMimeIconProvider;
  13. class MimeIconProvider implements IMimeIconProvider {
  14. public function __construct(
  15. protected IMimeTypeDetector $mimetypeDetector,
  16. protected IConfig $config,
  17. protected IURLGenerator $urlGenerator,
  18. protected IAppManager $appManager,
  19. protected ThemingDefaults $themingDefaults,
  20. ) {
  21. }
  22. public function getMimeIconUrl(string $mime): null|string {
  23. if (!$mime) {
  24. return null;
  25. }
  26. // Fetch all the aliases
  27. $aliases = $this->mimetypeDetector->getAllAliases();
  28. // Remove comments
  29. $aliases = array_filter($aliases, static function (string $key) {
  30. return !($key === '' || $key[0] === '_');
  31. }, ARRAY_FILTER_USE_KEY);
  32. // Map all the aliases recursively
  33. foreach ($aliases as $alias => $value) {
  34. if ($alias === $mime) {
  35. $mime = $value;
  36. }
  37. }
  38. $fileName = str_replace('/', '-', $mime);
  39. if ($url = $this->searchfileName($fileName)) {
  40. return $url;
  41. }
  42. $mimeType = explode('/', $mime)[0];
  43. if ($url = $this->searchfileName($mimeType)) {
  44. return $url;
  45. }
  46. return null;
  47. }
  48. private function searchfileName(string $fileName): null|string {
  49. // If the file exists in the current enabled legacy
  50. // custom theme, let's return it
  51. $theme = $this->config->getSystemValue('theme', '');
  52. if (!empty($theme)) {
  53. $path = "/themes/$theme/core/img/filetypes/$fileName.svg";
  54. if (file_exists(\OC::$SERVERROOT . $path)) {
  55. return $this->urlGenerator->getAbsoluteURL($path);
  56. }
  57. }
  58. // Previously, we used to pass this through Theming
  59. // But it was only used to colour icons containing
  60. // 0082c9. Since with vue we moved to inline svg icons,
  61. // we can just use the default core icons.
  62. // Finally, if the file exists in core, let's return it
  63. $path = "/core/img/filetypes/$fileName.svg";
  64. if (file_exists(\OC::$SERVERROOT . $path)) {
  65. return $this->urlGenerator->getAbsoluteURL($path);
  66. }
  67. return null;
  68. }
  69. }