fix: upload remote file

This commit is contained in:
StyleZhang 2024-10-31 14:13:04 +08:00
parent f4c8657f63
commit 54110336b3
6 changed files with 34 additions and 24 deletions

View File

@ -1,6 +1,5 @@
import { import {
memo, memo,
useMemo,
} from 'react' } from 'react'
import { import {
RiDeleteBinLine, RiDeleteBinLine,
@ -35,17 +34,9 @@ const FileInAttachmentItem = ({
onRemove, onRemove,
onReUpload, onReUpload,
}: FileInAttachmentItemProps) => { }: FileInAttachmentItemProps) => {
const { id, name, type, progress, supportFileType, base64Url, url } = file const { id, name, type, progress, supportFileType, base64Url, url, isRemote } = file
const ext = getFileExtension(name, type) const ext = getFileExtension(name, type, isRemote)
const isImageFile = supportFileType === SupportUploadFileTypes.image const isImageFile = supportFileType === SupportUploadFileTypes.image
const nameArr = useMemo(() => {
const nameMatch = name.match(/(.+)\.([^.]+)$/)
if (nameMatch)
return [nameMatch[1], nameMatch[2]]
return [name, '']
}, [name])
return ( return (
<div className={cn( <div className={cn(
@ -75,12 +66,7 @@ const FileInAttachmentItem = ({
className='flex items-center mb-0.5 system-xs-medium text-text-secondary truncate' className='flex items-center mb-0.5 system-xs-medium text-text-secondary truncate'
title={file.name} title={file.name}
> >
<div className='truncate'>{nameArr[0]}</div> <div className='truncate'>{name}</div>
{
nameArr[1] && (
<span>.{nameArr[1]}</span>
)
}
</div> </div>
<div className='flex items-center system-2xs-medium-uppercase text-text-tertiary'> <div className='flex items-center system-2xs-medium-uppercase text-text-tertiary'>
{ {
@ -93,7 +79,11 @@ const FileInAttachmentItem = ({
<span className='mx-1 system-2xs-medium'></span> <span className='mx-1 system-2xs-medium'></span>
) )
} }
<span>{formatFileSize(file.size || 0)}</span> {
!!file.size && (
<span>{formatFileSize(file.size)}</span>
)
}
</div> </div>
</div> </div>
<div className='shrink-0 flex items-center'> <div className='shrink-0 flex items-center'>

View File

@ -31,8 +31,8 @@ const FileItem = ({
onRemove, onRemove,
onReUpload, onReUpload,
}: FileItemProps) => { }: FileItemProps) => {
const { id, name, type, progress, url } = file const { id, name, type, progress, url, isRemote } = file
const ext = getFileExtension(name, type) const ext = getFileExtension(name, type, isRemote)
const uploadError = progress === -1 const uploadError = progress === -1
return ( return (
@ -75,7 +75,9 @@ const FileItem = ({
</> </>
) )
} }
{formatFileSize(file.size || 0)} {
!!file.size && formatFileSize(file.size)
}
</div> </div>
{ {
showDownloadAction && ( showDownloadAction && (

View File

@ -188,6 +188,17 @@ export const useFile = (fileConfig: FileUpload) => {
} }
}, [fileStore, notify, t, handleUpdateFile, params]) }, [fileStore, notify, t, handleUpdateFile, params])
const startProgressTimer = useCallback((fileId: string) => {
const timer = setInterval(() => {
const files = fileStore.getState().files
const file = files.find(file => file.id === fileId)
if (file && file.progress < 80 && file.progress >= 0)
handleUpdateFile({ ...file, progress: file.progress + 20 })
else
clearTimeout(timer)
}, 200)
}, [fileStore, handleUpdateFile])
const handleLoadFileFromLink = useCallback((url: string) => { const handleLoadFileFromLink = useCallback((url: string) => {
const allowedFileTypes = fileConfig.allowed_file_types const allowedFileTypes = fileConfig.allowed_file_types
@ -197,11 +208,13 @@ export const useFile = (fileConfig: FileUpload) => {
type: '', type: '',
size: 0, size: 0,
progress: 0, progress: 0,
transferMethod: TransferMethod.remote_url, transferMethod: TransferMethod.local_file,
supportFileType: '', supportFileType: '',
url, url,
isRemote: true,
} }
handleAddFile(uploadingFile) handleAddFile(uploadingFile)
startProgressTimer(uploadingFile.id)
uploadRemoteFileInfo(url).then((res) => { uploadRemoteFileInfo(url).then((res) => {
const newFile = { const newFile = {
@ -221,7 +234,7 @@ export const useFile = (fileConfig: FileUpload) => {
notify({ type: 'error', message: t('common.fileUploader.pasteFileLinkInvalid') }) notify({ type: 'error', message: t('common.fileUploader.pasteFileLinkInvalid') })
handleRemoveFile(uploadingFile.id) handleRemoveFile(uploadingFile.id)
}) })
}, [checkSizeLimit, handleAddFile, handleUpdateFile, notify, t, handleRemoveFile, fileConfig?.allowed_file_types]) }, [checkSizeLimit, handleAddFile, handleUpdateFile, notify, t, handleRemoveFile, fileConfig?.allowed_file_types, startProgressTimer])
const handleLoadFileFromLinkSuccess = useCallback(() => { }, []) const handleLoadFileFromLinkSuccess = useCallback(() => { }, [])

View File

@ -29,4 +29,5 @@ export type FileEntity = {
uploadedId?: string uploadedId?: string
base64Url?: string base64Url?: string
url?: string url?: string
isRemote?: boolean
} }

View File

@ -43,10 +43,13 @@ export const fileUpload: FileUpload = ({
}) })
} }
export const getFileExtension = (fileName: string, fileMimetype: string) => { export const getFileExtension = (fileName: string, fileMimetype: string, isRemote?: boolean) => {
if (fileMimetype) if (fileMimetype)
return mime.getExtension(fileMimetype) || '' return mime.getExtension(fileMimetype) || ''
if (isRemote)
return ''
if (fileName) { if (fileName) {
const fileNamePair = fileName.split('.') const fileNamePair = fileName.split('.')
const fileNamePairLength = fileNamePair.length const fileNamePairLength = fileNamePair.length

View File

@ -133,6 +133,7 @@ const ImageList: FC<ImageListProps> = ({
<ImagePreview <ImagePreview
url={imagePreviewUrl} url={imagePreviewUrl}
onCancel={() => setImagePreviewUrl('')} onCancel={() => setImagePreviewUrl('')}
title=''
/> />
)} )}
</div> </div>