Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion core/js/merged-template-prepend.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
[
"mimetype.js",
"mimetypelist.js",
"select2-toggleselect.js"
]
106 changes: 0 additions & 106 deletions core/js/mimetype.js

This file was deleted.

135 changes: 0 additions & 135 deletions core/js/tests/specs/mimeTypeSpec.js

This file was deleted.

2 changes: 2 additions & 0 deletions core/src/OC/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ import {
showMenu,
unregisterMenu,
} from './menu.js'
import * as MimeType from './mimeType.js'
import msg from './msg.js'
import { redirect, reload } from './navigation.js'
import Notification from './notification.js'
Expand Down Expand Up @@ -127,6 +128,7 @@ export default {
currentUser,
dialogs: Dialogs,
EventSource,
MimeType,
/**
* Returns the currently logged in user or null if there is no logged in
* user (public page mode)
Expand Down
94 changes: 94 additions & 0 deletions core/src/OC/mimeType.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/**
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/

import { generateUrl } from '@nextcloud/router'

const iconCache = new Map()

/**
* Return the url to icon of the given mimeType
*
* @param {string} mimeType The mimeType to get the icon for
* @return {string} Url to the icon for mimeType
*/
export function getIconUrl(mimeType) {
if (typeof mimeType === 'undefined') {
return undefined
}

while (mimeType in window.OC.MimeTypeList.aliases) {
mimeType = window.OC.MimeTypeList.aliases[mimeType]
}

if (!iconCache.has(mimeType)) {
// First try to get the correct icon from the current theme
let gotIcon = null
let path = ''
if (OC.theme.folder !== '' && Array.isArray(OC.MimeTypeList.themes[OC.theme.folder])) {
path = generateUrl('/themes/' + window.OC.theme.folder + '/core/img/filetypes/')
const icon = getMimeTypeIcon(mimeType, window.OC.MimeTypeList.themes[OC.theme.folder])

if (icon !== null) {
gotIcon = true
path += icon
}
}
if (window.OCA.Theming && gotIcon === null) {
path = generateUrl('/apps/theming/img/core/filetypes/')
path += getMimeTypeIcon(mimeType, window.OC.MimeTypeList.files)
gotIcon = true
}

// If we do not yet have an icon fall back to the default
if (gotIcon === null) {
path = generateUrl('/core/img/filetypes/')
path += getMimeTypeIcon(mimeType, window.OC.MimeTypeList.files)
}
Comment on lines +38 to +48
Copy link
Contributor

@ShGKme ShGKme Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not only moving the function but also changing it
(using suggestion just to show a diff)

Suggested change
if (window.OCA.Theming && gotIcon === null) {
path = generateUrl('/apps/theming/img/core/filetypes/')
path += getMimeTypeIcon(mimeType, window.OC.MimeTypeList.files)
gotIcon = true
}
// If we do not yet have an icon fall back to the default
if (gotIcon === null) {
path = generateUrl('/core/img/filetypes/')
path += getMimeTypeIcon(mimeType, window.OC.MimeTypeList.files)
}
if (OCA.Theming && gotIcon === null) {
path = OC.generateUrl('/apps/theming/img/core/filetypes/')
path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files)
gotIcon = true
}
// If we do not yet have an icon fall back to the default
if (gotIcon === null) {
path = OC.getRootPath() + '/core/img/filetypes/'
path += OC.MimeType._getFile(mimeType, OC.MimeTypeList.files)
}

But generateUrl cannot be used here.

  1. /apps/theming/img/ - is a PHP route managed by a controller
  2. '/core/img/filetypes/' - is a static file path

In the first case we need to include index.php when no rewrite. But in the second adding index.php breaks the path to the icon.

/index.php/core/img/filetypes/file.svg is not correct.

It must be either imagePath or generateFilePath, or getRootPath like in the original version. But it cannot be generateUrl

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @susnux

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in #55916


path += '.svg'

if (window.OCA.Theming) {
path += '?v=' + window.OCA.Theming.cacheBuster
}

// Cache the result
iconCache.set(mimeType, path)
}

return iconCache.get(mimeType)
}

/**
* Return the file icon we want to use for the given mimeType.
* The file needs to be present in the supplied file list
*
* @param {string} mimeType The mimeType we want an icon for
* @param {string[]} files The available icons in this theme
* @return {string | null} The icon to use or null if there is no match
*/
function getMimeTypeIcon(mimeType, files) {
const icon = mimeType.replace(new RegExp('/', 'g'), '-')

// Generate path
if (mimeType === 'dir' && files.includes('folder')) {
return 'folder'
} else if (mimeType === 'dir-encrypted' && files.includes('folder-encrypted')) {
return 'folder-encrypted'
} else if (mimeType === 'dir-shared' && files.includes('folder-shared')) {
return 'folder-shared'
} else if (mimeType === 'dir-public' && files.includes('folder-public')) {
return 'folder-public'
} else if ((mimeType === 'dir-external' || mimeType === 'dir-external-root') && files.includes('folder-external')) {
return 'folder-external'
} else if (files.includes(icon)) {
return icon
} else if (files.includes(icon.split('-')[0])) {
return icon.split('-')[0]
} else if (files.includes('file')) {
return 'file'
}

return null
}
Loading
Loading