-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
fix: zip Icon missing #2198
fix: zip Icon missing #2198
Conversation
Adding the "do-not-merge/release-note-label-needed" label because no release-note block was detected, please follow our release note process to remove it. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. |
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: The full list of commands accepted by this bot can be found here.
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
@@ -49,7 +49,7 @@ export function getImgUrl(name: string) { | |||
const list = Object.values(typeList).flat() | |||
|
|||
const type = list.includes(fileType(name).toLowerCase()) ? fileType(name).toLowerCase() : 'unknow' | |||
return new URL(`../assets/${type}-icon.svg`, import.meta.url).href | |||
return new URL(`../assets/fileType/${type}-icon.svg`, import.meta.url).href | |||
} | |||
// 是否是白名单后缀 | |||
export function isRightType(name: string, type: string) { |
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.
The provided code looks generally clean, but here are some points to consider for improvement:
-
File Type Check: The current implementation checks if
fileType(name)
(presumably a helper function that returns the file extension of a name) is included in thelist
array. However, this won't directly match against known image types like.jpg
,.png
, etc. -
URL Construction: The URL construction is correct, but it assumes that all valid image formats have corresponding icon files named
<ext>-icon.svg
. -
Optimization Suggestion: Consider adding a mapping from supported image extensions to their corresponding icons to reduce redundancy and improve readability when setting up the
typeList
object. -
Error Handling: Add basic error handling to manage cases where
fileType(name)
might not return an expected value.
Here's an updated version with these considerations:
export function getImgUrl(name: string): string | null {
const list = Object.values(typeList).flat();
// Get the file extension in lowercase without dot
const fileType = () => name.split('.').pop()?.toLowerCase();
const typeLowerCase = fileType() || '';
// Find a matching icon type in the typeList
const availableTypes = [...new Set(list)];
const iconName = availableTypes.find(t =>
t === typeLowerCase ||
`${t.charAt(0).toUpperCase()}${typeLowerCase.slice(1)}-img`.includes(typeLowerCase)
);
if (!iconName) {
console.error(`No icon found for type ${typeLowerCase}`);
return null; // or handle it gracefully based on your needs
}
// Construct and return the URL
return new URL(`../assets/${iconName.toLowerCase().replace('-', '')}.svg`, import.meta.url).href;
}
// Whether it's a whitelist after suffixes
export function isRightType(name: string, type: string): boolean {
// Simplified example assuming 'type' is already sanitized or correctly passed
return typeList[type] && Object.values(typeList[type]).some(ext => ext === name);
}
Explanation:
- Available Types: A set of available types (derived from
typeList
) ensures uniqueness and simplifies finding the right icon name. - Icon Name Calculation: The logic tries to find a match by looking for both exact matches and variations with capitalization adjustments.
- Null Return: If no icon is found, a message is logged, and
null
is returned to indicate the absence of a suitable icon. - Sanitation Concerns: Ensure
typeName()
is properly sanitizing the input before searching withintypeList
.
This approach should make the code more robust and flexible while maintaining clarity.
fix: zip Icon missing