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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /**
  2. * @author Roeland Jago Douma <roeland@famdouma.nl>
  3. *
  4. * @copyright Copyright (c) 2015, ownCloud, Inc.
  5. * @license AGPL-3.0
  6. *
  7. * This code is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License, version 3,
  9. * as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Affero General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Affero General Public License, version 3,
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>
  18. *
  19. */
  20. /**
  21. * Namespace to hold functions related to convert mimetype to icons
  22. *
  23. * @namespace
  24. */
  25. OC.MimeType = {
  26. /**
  27. * Cache that maps mimeTypes to icon urls
  28. */
  29. _mimeTypeIcons: {},
  30. /**
  31. * Return the file icon we want to use for the given mimeType.
  32. * The file needs to be present in the supplied file list
  33. *
  34. * @param {string} mimeType The mimeType we want an icon for
  35. * @param {array} files The available icons in this theme
  36. * @return {string} The icon to use or null if there is no match
  37. */
  38. _getFile: function(mimeType, files) {
  39. var icon = mimeType.replace(new RegExp('/', 'g'), '-');
  40. // Generate path
  41. if (mimeType === 'dir' && $.inArray('folder', files) !== -1) {
  42. return 'folder';
  43. } else if (mimeType === 'dir-encrypted' && $.inArray('folder-encrypted', files) !== -1) {
  44. return 'folder-encrypted';
  45. } else if (mimeType === 'dir-shared' && $.inArray('folder-shared', files) !== -1) {
  46. return 'folder-shared';
  47. } else if (mimeType === 'dir-public' && $.inArray('folder-public', files) !== -1) {
  48. return 'folder-public';
  49. } else if (mimeType === 'dir-external' && $.inArray('folder-external', files) !== -1) {
  50. return 'folder-external';
  51. } else if ($.inArray(icon, files) !== -1) {
  52. return icon;
  53. } else if ($.inArray(icon.split('-')[0], files) !== -1) {
  54. return icon.split('-')[0];
  55. } else if ($.inArray('file', files) !== -1) {
  56. return 'file';
  57. }
  58. return null;
  59. },
  60. /**
  61. * Return the url to icon of the given mimeType
  62. *
  63. * @param {string} mimeType The mimeType to get the icon for
  64. * @return {string} Url to the icon for mimeType
  65. */
  66. getIconUrl: function(mimeType) {
  67. if (_.isUndefined(mimeType)) {
  68. return undefined;
  69. }
  70. while (mimeType in OC.MimeTypeList.aliases) {
  71. mimeType = OC.MimeTypeList.aliases[mimeType];
  72. }
  73. if (mimeType in OC.MimeType._mimeTypeIcons) {
  74. return OC.MimeType._mimeTypeIcons[mimeType];
  75. }
  76. // First try to get the correct icon from the current theme
  77. var gotIcon = null;
  78. var path = '';
  79. if (OC.theme.folder !== '' && $.isArray(OC.MimeTypeList.themes[OC.theme.folder])) {
  80. path = OC.getRootPath() + '/themes/' + OC.theme.folder + '/core/img/filetypes/';
  81. var icon = OC.MimeType._getFile(mimeType, OC.MimeTypeList.themes[OC.theme.folder]);
  82. if (icon !== null) {
  83. gotIcon = true;
  84. path += icon;
  85. }
  86. }
  87. if(OCA.Theming && gotIcon === null) {
  88. path = OC.generateUrl('/apps/theming/img/core/filetypes/');
  89. path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files);
  90. gotIcon = true;
  91. }
  92. // If we do not yet have an icon fall back to the default
  93. if (gotIcon === null) {
  94. path = OC.getRootPath() + '/core/img/filetypes/';
  95. path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files);
  96. }
  97. path += '.svg';
  98. if(OCA.Theming) {
  99. path += "?v=" + OCA.Theming.cacheBuster;
  100. }
  101. // Cache the result
  102. OC.MimeType._mimeTypeIcons[mimeType] = path;
  103. return path;
  104. }
  105. };