chore: update resource type checks (#2081)

This commit is contained in:
boojack
2023-08-05 20:17:33 +08:00
committed by GitHub
parent cc400da44e
commit 270a529948
4 changed files with 39 additions and 36 deletions

View File

@ -5,3 +5,34 @@ export const getResourceUrl = (resource: Resource, withOrigin = true) => {
return `${withOrigin ? window.location.origin : ""}/o/r/${resource.id}`;
};
export const getResourceType = (resource: Resource) => {
if (resource.type.startsWith("image") && isImage(resource.type)) {
return "image/*";
} else if (resource.type.startsWith("video")) {
return "video/*";
} else if (resource.type.startsWith("audio")) {
return "audio/*";
} else if (resource.type.startsWith("text")) {
return "text/*";
} else if (resource.type.startsWith("application/epub+zip")) {
return "application/epub+zip";
} else if (resource.type.startsWith("application/pdf")) {
return "application/pdf";
} else if (resource.type.includes("word")) {
return "application/msword";
} else if (resource.type.includes("excel")) {
return "application/msexcel";
} else if (resource.type.startsWith("application/zip")) {
return "application/zip";
} else if (resource.type.startsWith("application/x-java-archive")) {
return "application/x-java-archive";
} else {
return "application/octet-stream";
}
};
// isImage returns true if the given mime type is an image.
export const isImage = (t: string) => {
return t === "image/jpeg" || t === "image/png" || t === "image/gif" || t === "image/svg+xml" || t === "image/webp";
};