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