Skip to content

Commit

Permalink
refactor: preview media
Browse files Browse the repository at this point in the history
  • Loading branch information
Arman19941113 committed May 23, 2024
1 parent 851922d commit c58e54c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"clsx": "^2.1.1",
"framer-motion": "^11.1.8",
"i18next": "^23.11.3",
"mime": "^4.0.3",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-i18next": "^14.1.1",
Expand Down
10 changes: 10 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 9 additions & 4 deletions src/components/FileView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { MingcuteFileMoreLine } from '@/components/icon'
import { FileInfo } from '@/util'
import { convertFileSrc } from '@tauri-apps/api/core'

import { clsx } from 'clsx'
import { useState } from 'react'
import { useTranslation } from 'react-i18next'

Expand All @@ -9,9 +11,12 @@ function FileView({ fileInfo }: { fileInfo: FileInfo }) {
const { t } = useTranslation()

/* image source */
const imageSrc = convertFileSrc(fileInfo.pathname)
const imageSrc = fileInfo.preview ? convertFileSrc(fileInfo.pathname) : ''
const [isImgLoad, setIsImgLoad] = useState(false)
const [isImgError, setIsImgError] = useState(false)
const onImgLoad = () => setIsImgLoad(true)
const onImgError = () => setIsImgError(true)
const showFileIcon = !imageSrc || isImgError

/* exif data */
let focalLength = fileInfo.exifData?.FocalLength
Expand All @@ -32,11 +37,11 @@ function FileView({ fileInfo }: { fileInfo: FileInfo }) {
return (
<div className="ml-4 w-[248px] shrink-0 overflow-auto">
<div className="flex-center h-[143px]">
{isImgError ? (
{showFileIcon ? (
<MingcuteFileMoreLine className="text-5xl text-default-500" />
) : (
<div className="p-1 pt-0 shadow-md">
<img src={imageSrc} alt="" className="h-[135px] object-contain" onError={onImgError} />
<div className={clsx('p-1 pt-0', isImgLoad && 'shadow-md')}>
<img src={imageSrc} alt="" className="h-[135px] object-contain" onLoad={onImgLoad} onError={onImgError} />
</div>
)}
</div>
Expand Down
13 changes: 13 additions & 0 deletions src/util/file-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ExifStatus, FormatVar } from '@/const'
import mime from 'mime/lite'
import { formatDate, formatFileSize, getDirFromFilePath, getValidPath } from './'

export interface FileInfo {
Expand All @@ -8,6 +9,7 @@ export interface FileInfo {
filename: string
newFilename: string
size: string
preview: boolean
exifStatus: ExifStatus
exifMsg: string
exifData: IpcFiles[number]['exifData']
Expand Down Expand Up @@ -35,6 +37,7 @@ export function transformIpcFiles({ ipcFiles, format, t }: { ipcFiles: IpcFiles;
filename: item.filename,
newFilename: '',
size: formatFileSize(item.size),
preview: checkPreview(item.filename, item.size),
exifStatus,
exifMsg,
exifData,
Expand Down Expand Up @@ -120,3 +123,13 @@ function generateFilename({
return format
}
}

export function checkPreview(filename: string, size: number) {
const maxSize = 50_000_000
if (size > maxSize) return false

const mimeType = mime.getType(filename)
if (!mimeType) return false

return mimeType.startsWith('image') || mimeType.startsWith('video')
}

0 comments on commit c58e54c

Please sign in to comment.