MimeIconProvider.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2023 John Molakvoæ <skjnldsv@protonmail.com>
  4. *
  5. * @author John Molakvoæ <skjnldsv@protonmail.com>
  6. *
  7. * @license AGPL-3.0-or-later
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace OC\Preview;
  23. use OCA\Theming\ThemingDefaults;
  24. use OCP\App\IAppManager;
  25. use OCP\Files\IMimeTypeDetector;
  26. use OCP\IConfig;
  27. use OCP\IURLGenerator;
  28. use OCP\Preview\IMimeIconProvider;
  29. class MimeIconProvider implements IMimeIconProvider {
  30. public function __construct(
  31. protected IMimeTypeDetector $mimetypeDetector,
  32. protected IConfig $config,
  33. protected IURLGenerator $urlGenerator,
  34. protected IAppManager $appManager,
  35. protected ThemingDefaults $themingDefaults,
  36. ) {
  37. }
  38. public function getMimeIconUrl(string $mime): null|string {
  39. if (!$mime) {
  40. return null;
  41. }
  42. // Fetch all the aliases
  43. $aliases = $this->mimetypeDetector->getAllAliases();
  44. // Remove comments
  45. $aliases = array_filter($aliases, static function ($key) {
  46. return !($key === '' || $key[0] === '_');
  47. }, ARRAY_FILTER_USE_KEY);
  48. // Map all the aliases recursively
  49. foreach ($aliases as $alias => $value) {
  50. if ($alias === $mime) {
  51. $mime = $value;
  52. }
  53. }
  54. $fileName = str_replace('/', '-', $mime);
  55. if ($url = $this->searchfileName($fileName)) {
  56. return $url;
  57. }
  58. $mimeType = explode('/', $mime)[0];
  59. if ($url = $this->searchfileName($mimeType)) {
  60. return $url;
  61. }
  62. return null;
  63. }
  64. private function searchfileName(string $fileName): null|string {
  65. // If the file exists in the current enabled legacy
  66. // custom theme, let's return it
  67. $theme = $this->config->getSystemValue('theme', '');
  68. if (!empty($theme)) {
  69. $path = "/themes/$theme/core/img/filetypes/$fileName.svg";
  70. if (file_exists(\OC::$SERVERROOT . $path)) {
  71. return $this->urlGenerator->getAbsoluteURL($path);
  72. }
  73. }
  74. // Previously, we used to pass this through Theming
  75. // But it was only used to colour icons containing
  76. // 0082c9. Since with vue we moved to inline svg icons,
  77. // we can just use the default core icons.
  78. // Finally, if the file exists in core, let's return it
  79. $path = "/core/img/filetypes/$fileName.svg";
  80. if (file_exists(\OC::$SERVERROOT . $path)) {
  81. return $this->urlGenerator->getAbsoluteURL($path);
  82. }
  83. return null;
  84. }
  85. }