Skip to content

Commit 678a8a7

Browse files
authored
Merge pull request #55524 from nextcloud/chore/karma-mimetype
refactor: move `OC.MimeType` to `src` and add `vitest` unit tests
2 parents 057c0dc + 7bda1cd commit 678a8a7

File tree

10 files changed

+220
-248
lines changed

10 files changed

+220
-248
lines changed
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
[
2-
"mimetype.js",
32
"mimetypelist.js",
43
"select2-toggleselect.js"
54
]

core/js/mimetype.js

Lines changed: 0 additions & 106 deletions
This file was deleted.

core/js/tests/specs/mimeTypeSpec.js

Lines changed: 0 additions & 135 deletions
This file was deleted.

core/src/OC/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ import {
6363
showMenu,
6464
unregisterMenu,
6565
} from './menu.js'
66+
import * as MimeType from './mimeType.js'
6667
import msg from './msg.js'
6768
import { redirect, reload } from './navigation.js'
6869
import Notification from './notification.js'
@@ -127,6 +128,7 @@ export default {
127128
currentUser,
128129
dialogs: Dialogs,
129130
EventSource,
131+
MimeType,
130132
/**
131133
* Returns the currently logged in user or null if there is no logged in
132134
* user (public page mode)

core/src/OC/mimeType.js

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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

Comments
 (0)