-
-
Notifications
You must be signed in to change notification settings - Fork 4.7k
refactor: move OC.MimeType to src and add vitest unit tests
#55524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,4 @@ | ||
| [ | ||
| "mimetype.js", | ||
| "mimetypelist.js", | ||
| "select2-toggleselect.js" | ||
| ] |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| } | ||
|
|
||
| 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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)
But
generateUrlcannot be used here./apps/theming/img/- is a PHP route managed by a controllerIn the first case we need to include
index.phpwhen no rewrite. But in the second addingindex.phpbreaks the path to the icon./index.php/core/img/filetypes/file.svgis not correct.It must be either
imagePathorgenerateFilePath, orgetRootPathlike in the original version. But it cannot begenerateUrlThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @susnux
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in #55916