mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: update resource type checks (#2081)
This commit is contained in:
@ -1,6 +1,6 @@
|
|||||||
import { ImageList, ImageListItem, useMediaQuery } from "@mui/material";
|
import { ImageList, ImageListItem, useMediaQuery } from "@mui/material";
|
||||||
import { absolutifyLink } from "@/helpers/utils";
|
import { absolutifyLink } from "@/helpers/utils";
|
||||||
import { getResourceUrl } from "@/utils/resource";
|
import { getResourceType, getResourceUrl } from "@/utils/resource";
|
||||||
import SquareDiv from "./kit/SquareDiv";
|
import SquareDiv from "./kit/SquareDiv";
|
||||||
import MemoResource from "./MemoResource";
|
import MemoResource from "./MemoResource";
|
||||||
import showPreviewImageDialog from "./PreviewImageDialog";
|
import showPreviewImageDialog from "./PreviewImageDialog";
|
||||||
@ -24,17 +24,15 @@ const MemoResourceListView: React.FC<Props> = (props: Props) => {
|
|||||||
...props,
|
...props,
|
||||||
};
|
};
|
||||||
const matches = useMediaQuery("(min-width:640px)");
|
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 videoResourceList = resourceList.filter((resource) => resource.type.startsWith("video"));
|
||||||
const otherResourceList = resourceList.filter(
|
const otherResourceList = resourceList.filter(
|
||||||
(resource) => !imageResourceList.includes(resource) && !videoResourceList.includes(resource)
|
(resource) => !imageResourceList.includes(resource) && !videoResourceList.includes(resource)
|
||||||
);
|
);
|
||||||
|
|
||||||
const imgUrls = imageResourceList
|
const imgUrls = imageResourceList.map((resource) => {
|
||||||
.filter((resource) => resource.type.startsWith("image"))
|
return getResourceUrl(resource);
|
||||||
.map((resource) => {
|
});
|
||||||
return getResourceUrl(resource);
|
|
||||||
});
|
|
||||||
|
|
||||||
const handleImageClick = (imgUrl: string) => {
|
const handleImageClick = (imgUrl: string) => {
|
||||||
const index = imgUrls.findIndex((url) => url === imgUrl);
|
const index = imgUrls.findIndex((url) => url === imgUrl);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import React from "react";
|
import React from "react";
|
||||||
import { getResourceUrl } from "@/utils/resource";
|
import { getResourceType, getResourceUrl } from "@/utils/resource";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
import SquareDiv from "./kit/SquareDiv";
|
import SquareDiv from "./kit/SquareDiv";
|
||||||
import showPreviewImageDialog from "./PreviewImageDialog";
|
import showPreviewImageDialog from "./PreviewImageDialog";
|
||||||
@ -9,32 +9,6 @@ interface ResourceCoverProps {
|
|||||||
resource: Resource;
|
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 ResourceCover = ({ resource }: ResourceCoverProps) => {
|
||||||
const resourceType = getResourceType(resource);
|
const resourceType = getResourceType(resource);
|
||||||
const resourceUrl = getResourceUrl(resource);
|
const resourceUrl = getResourceUrl(resource);
|
||||||
|
@ -3,7 +3,7 @@ import React from "react";
|
|||||||
import toast from "react-hot-toast";
|
import toast from "react-hot-toast";
|
||||||
import { useResourceStore } from "@/store/module";
|
import { useResourceStore } from "@/store/module";
|
||||||
import { useTranslate } from "@/utils/i18n";
|
import { useTranslate } from "@/utils/i18n";
|
||||||
import { getResourceUrl } from "@/utils/resource";
|
import { getResourceType, getResourceUrl } from "@/utils/resource";
|
||||||
import showChangeResourceFilenameDialog from "./ChangeResourceFilenameDialog";
|
import showChangeResourceFilenameDialog from "./ChangeResourceFilenameDialog";
|
||||||
import { showCommonDialog } from "./Dialog/CommonDialog";
|
import { showCommonDialog } from "./Dialog/CommonDialog";
|
||||||
import Icon from "./Icon";
|
import Icon from "./Icon";
|
||||||
@ -20,7 +20,7 @@ const ResourceItemDropdown = ({ resource }: Props) => {
|
|||||||
|
|
||||||
const handlePreviewBtnClick = (resource: Resource) => {
|
const handlePreviewBtnClick = (resource: Resource) => {
|
||||||
const resourceUrl = getResourceUrl(resource);
|
const resourceUrl = getResourceUrl(resource);
|
||||||
if (resource.type.startsWith("image")) {
|
if (getResourceType(resource).startsWith("image")) {
|
||||||
showPreviewImageDialog([getResourceUrl(resource)], 0);
|
showPreviewImageDialog([getResourceUrl(resource)], 0);
|
||||||
} else {
|
} else {
|
||||||
window.open(resourceUrl);
|
window.open(resourceUrl);
|
||||||
|
@ -5,3 +5,34 @@ export const getResourceUrl = (resource: Resource, withOrigin = true) => {
|
|||||||
|
|
||||||
return `${withOrigin ? window.location.origin : ""}/o/r/${resource.id}`;
|
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";
|
||||||
|
};
|
||||||
|
Reference in New Issue
Block a user