Skip to content

Commit

Permalink
chore: update resource type checks (usememos#2081)
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack authored and qazxcdswe123 committed Aug 9, 2023
1 parent 76a3c65 commit 8934845
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 36 deletions.
12 changes: 5 additions & 7 deletions web/src/components/MemoResourceListView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ImageList, ImageListItem, useMediaQuery } from "@mui/material";
import { absolutifyLink } from "@/helpers/utils";
import { getResourceUrl } from "@/utils/resource";
import { getResourceType, getResourceUrl } from "@/utils/resource";
import SquareDiv from "./kit/SquareDiv";
import MemoResource from "./MemoResource";
import showPreviewImageDialog from "./PreviewImageDialog";
Expand All @@ -24,17 +24,15 @@ const MemoResourceListView: React.FC<Props> = (props: Props) => {
...props,
};
const matches = useMediaQuery("(min-width:640px)");
const imageResourceList = resourceList.filter((resource) => resource.type.startsWith("image"));
const imageResourceList = resourceList.filter((resource) => getResourceType(resource).startsWith("image"));
const videoResourceList = resourceList.filter((resource) => resource.type.startsWith("video"));
const otherResourceList = resourceList.filter(
(resource) => !imageResourceList.includes(resource) && !videoResourceList.includes(resource)
);

const imgUrls = imageResourceList
.filter((resource) => resource.type.startsWith("image"))
.map((resource) => {
return getResourceUrl(resource);
});
const imgUrls = imageResourceList.map((resource) => {
return getResourceUrl(resource);
});

const handleImageClick = (imgUrl: string) => {
const index = imgUrls.findIndex((url) => url === imgUrl);
Expand Down
28 changes: 1 addition & 27 deletions web/src/components/ResourceCover.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { getResourceUrl } from "@/utils/resource";
import { getResourceType, getResourceUrl } from "@/utils/resource";
import Icon from "./Icon";
import SquareDiv from "./kit/SquareDiv";
import showPreviewImageDialog from "./PreviewImageDialog";
Expand All @@ -9,32 +9,6 @@ interface ResourceCoverProps {
resource: Resource;
}

const getResourceType = (resource: Resource) => {
if (resource.type.startsWith("image")) {
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";
}
};

const ResourceCover = ({ resource }: ResourceCoverProps) => {
const resourceType = getResourceType(resource);
const resourceUrl = getResourceUrl(resource);
Expand Down
4 changes: 2 additions & 2 deletions web/src/components/ResourceItemDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React from "react";
import toast from "react-hot-toast";
import { useResourceStore } from "@/store/module";
import { useTranslate } from "@/utils/i18n";
import { getResourceUrl } from "@/utils/resource";
import { getResourceType, getResourceUrl } from "@/utils/resource";
import showChangeResourceFilenameDialog from "./ChangeResourceFilenameDialog";
import { showCommonDialog } from "./Dialog/CommonDialog";
import Icon from "./Icon";
Expand All @@ -20,7 +20,7 @@ const ResourceItemDropdown = ({ resource }: Props) => {

const handlePreviewBtnClick = (resource: Resource) => {
const resourceUrl = getResourceUrl(resource);
if (resource.type.startsWith("image")) {
if (getResourceType(resource).startsWith("image")) {
showPreviewImageDialog([getResourceUrl(resource)], 0);
} else {
window.open(resourceUrl);
Expand Down
31 changes: 31 additions & 0 deletions web/src/utils/resource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
};

0 comments on commit 8934845

Please sign in to comment.