You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mimetype.js 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /**
  2. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  3. * SPDX-FileCopyrightText: 2015 ownCloud, Inc.
  4. * SPDX-License-Identifier: AGPL-3.0-only
  5. */
  6. /**
  7. * Namespace to hold functions related to convert mimetype to icons
  8. *
  9. * @namespace
  10. */
  11. OC.MimeType = {
  12. /**
  13. * Cache that maps mimeTypes to icon urls
  14. */
  15. _mimeTypeIcons: {},
  16. /**
  17. * Return the file icon we want to use for the given mimeType.
  18. * The file needs to be present in the supplied file list
  19. *
  20. * @param {string} mimeType The mimeType we want an icon for
  21. * @param {array} files The available icons in this theme
  22. * @return {string} The icon to use or null if there is no match
  23. */
  24. _getFile: function(mimeType, files) {
  25. var icon = mimeType.replace(new RegExp('/', 'g'), '-');
  26. // Generate path
  27. if (mimeType === 'dir' && files.includes('folder')) {
  28. return 'folder';
  29. } else if (mimeType === 'dir-encrypted' && files.includes('folder-encrypted')) {
  30. return 'folder-encrypted';
  31. } else if (mimeType === 'dir-shared' && files.includes('folder-shared')) {
  32. return 'folder-shared';
  33. } else if (mimeType === 'dir-public' && files.includes('folder-public')) {
  34. return 'folder-public';
  35. } else if ((mimeType === 'dir-external' || mimeType === 'dir-external-root') && files.includes('folder-external')) {
  36. return 'folder-external';
  37. } else if (files.includes(icon)) {
  38. return icon;
  39. } else if (files.includes(icon.split('-')[0])) {
  40. return icon.split('-')[0];
  41. } else if (files.includes('file')) {
  42. return 'file';
  43. }
  44. return null;
  45. },
  46. /**
  47. * Return the url to icon of the given mimeType
  48. *
  49. * @param {string} mimeType The mimeType to get the icon for
  50. * @return {string} Url to the icon for mimeType
  51. */
  52. getIconUrl: function(mimeType) {
  53. if (typeof mimeType === 'undefined') {
  54. return undefined;
  55. }
  56. while (mimeType in OC.MimeTypeList.aliases) {
  57. mimeType = OC.MimeTypeList.aliases[mimeType];
  58. }
  59. if (mimeType in OC.MimeType._mimeTypeIcons) {
  60. return OC.MimeType._mimeTypeIcons[mimeType];
  61. }
  62. // First try to get the correct icon from the current theme
  63. var gotIcon = null;
  64. var path = '';
  65. if (OC.theme.folder !== '' && Array.isArray(OC.MimeTypeList.themes[OC.theme.folder])) {
  66. path = OC.getRootPath() + '/themes/' + OC.theme.folder + '/core/img/filetypes/';
  67. var icon = OC.MimeType._getFile(mimeType, OC.MimeTypeList.themes[OC.theme.folder]);
  68. if (icon !== null) {
  69. gotIcon = true;
  70. path += icon;
  71. }
  72. }
  73. if(OCA.Theming && gotIcon === null) {
  74. path = OC.generateUrl('/apps/theming/img/core/filetypes/');
  75. path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files);
  76. gotIcon = true;
  77. }
  78. // If we do not yet have an icon fall back to the default
  79. if (gotIcon === null) {
  80. path = OC.getRootPath() + '/core/img/filetypes/';
  81. path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files);
  82. }
  83. path += '.svg';
  84. if(OCA.Theming) {
  85. path += "?v=" + OCA.Theming.cacheBuster;
  86. }
  87. // Cache the result
  88. OC.MimeType._mimeTypeIcons[mimeType] = path;
  89. return path;
  90. }
  91. };