use mimetype lib to get types

This commit is contained in:
SrGooglo 2023-03-06 02:08:52 +00:00
parent 974733bc5f
commit 0d1ecc5f03
2 changed files with 99 additions and 103 deletions

View File

@ -66,6 +66,7 @@
"localforage": "^1.10.0", "localforage": "^1.10.0",
"luxon": "^3.0.4", "luxon": "^3.0.4",
"match-sorter": "^6.3.1", "match-sorter": "^6.3.1",
"mime": "^3.0.0",
"moment": "2.29.4", "moment": "2.29.4",
"moment-timezone": "^0.5.37", "moment-timezone": "^0.5.37",
"mpegts.js": "^1.6.10", "mpegts.js": "^1.6.10",

View File

@ -3,42 +3,24 @@ import { Skeleton } from "antd"
import { Carousel } from "react-responsive-carousel" import { Carousel } from "react-responsive-carousel"
import { ImageViewer } from "components" import { ImageViewer } from "components"
import Plyr from "plyr-react" import Plyr from "plyr-react"
import mimetypes from "mime"
import ContentFailed from "../contentFailed" import ContentFailed from "../contentFailed"
const mediaTypes = {
"jpg": "image",
"jpeg": "image",
"png": "image",
"gif": "image",
"mp4": "video",
"webm": "video",
"ogv": "video",
"mov": "video",
"avi": "video",
"mkv": "video",
"ogg": "audio",
"mp3": "audio",
"wav": "audio",
"flac": "audio",
"aac": "audio",
"m4a": "audio",
}
import "react-responsive-carousel/lib/styles/carousel.min.css" import "react-responsive-carousel/lib/styles/carousel.min.css"
import "plyr-react/dist/plyr.css" import "plyr-react/dist/plyr.css"
import "./index.less" import "./index.less"
const Attachment = React.memo((props) => { const Attachment = React.memo((props) => {
const { url, id } = props.attachment
const [loaded, setLoaded] = React.useState(false) const [loaded, setLoaded] = React.useState(false)
const [mediaType, setMediaType] = React.useState(null)
const [mimeType, setMimeType] = React.useState(null) const [mimeType, setMimeType] = React.useState(null)
try {
const { url, id } = props.attachment
const onDoubleClickAttachment = (e) => { const onDoubleClickAttachment = (e) => {
if (mediaType.split("/")[0] === "image") { if (mimeType.split("/")[0] === "image") {
e.preventDefault() e.preventDefault()
e.stopPropagation() e.stopPropagation()
@ -50,10 +32,10 @@ const Attachment = React.memo((props) => {
let extension = null let extension = null
// get media type by parsing the url // get media type by parsing the url
const mediaTypeExt = /\.([a-zA-Z0-9]+)$/.exec(url) const mediaExtname = /\.([a-zA-Z0-9]+)$/.exec(url)
if (mediaTypeExt) { if (mediaExtname) {
extension = mediaTypeExt[1] extension = mediaExtname[1]
} else { } else {
// try to get media by creating requesting the url // try to get media by creating requesting the url
const response = await fetch(url, { const response = await fetch(url, {
@ -65,17 +47,27 @@ const Attachment = React.memo((props) => {
extension = extension.toLowerCase() extension = extension.toLowerCase()
const mediaType = mediaTypes[extension] if (!extension) {
const mimeType = `${mediaType}/${extension}` setLoaded(true)
console.error("Failed to get media type", url, extension)
return
}
const mimeType = mimetypes.getType(extension)
setMediaType(mediaType)
setMimeType(mimeType) setMimeType(mimeType)
setLoaded(true) setLoaded(true)
} }
const renderMedia = () => { const renderMedia = () => {
switch (mediaType.split("/")[0]) { if (!mimeType) {
return null
}
switch (mimeType.split("/")[0]) {
case "image": { case "image": {
return <ImageViewer src={url} /> return <ImageViewer src={url} />
} }
@ -99,7 +91,7 @@ const Attachment = React.memo((props) => {
} }
default: { default: {
return <h4> return <h4>
Unsupported media type [{mediaType}/{mediaTypeExt}] Unsupported media type [{mimeType}]
</h4> </h4>
} }
} }
@ -113,11 +105,10 @@ const Attachment = React.memo((props) => {
return <Skeleton active /> return <Skeleton active />
} }
if (loaded && !mediaType && !mimeType) { if (loaded && !mimeType) {
return <ContentFailed /> return <ContentFailed />
} }
try {
return <div return <div
key={props.index} key={props.index}
id={id} id={id}
@ -127,7 +118,7 @@ const Attachment = React.memo((props) => {
{renderMedia()} {renderMedia()}
</div> </div>
} catch (error) { } catch (error) {
console.error(`Failed to render attachment ${props.attachment.name} (${props.attachment.id})`, error) console.error(error)
return <ContentFailed /> return <ContentFailed />
} }
@ -179,6 +170,10 @@ export default (props) => {
> >
{ {
props.attachments?.length > 0 && props.attachments.map((attachment, index) => { props.attachments?.length > 0 && props.attachments.map((attachment, index) => {
if (typeof attachment !== "object") {
return null
}
return <Attachment index={index} attachment={attachment} /> return <Attachment index={index} attachment={attachment} />
}) })
} }