fix: upload remote image preview

This commit is contained in:
StyleZhang 2024-10-28 16:15:55 +08:00
parent aafa4a3c8b
commit f4c8657f63
2 changed files with 12 additions and 9 deletions

View File

@ -25,7 +25,7 @@ import { TransferMethod } from '@/types/app'
import { SupportUploadFileTypes } from '@/app/components/workflow/types' import { SupportUploadFileTypes } from '@/app/components/workflow/types'
import type { FileUpload } from '@/app/components/base/features/types' import type { FileUpload } from '@/app/components/base/features/types'
import { formatFileSize } from '@/utils/format' import { formatFileSize } from '@/utils/format'
import { fetchRemoteFileInfo } from '@/service/common' import { uploadRemoteFileInfo } from '@/service/common'
import type { FileUploadConfigResponse } from '@/models/common' import type { FileUploadConfigResponse } from '@/models/common'
export const useFileSizeLimit = (fileUploadConfig?: FileUploadConfigResponse) => { export const useFileSizeLimit = (fileUploadConfig?: FileUploadConfigResponse) => {
@ -49,7 +49,7 @@ export const useFile = (fileConfig: FileUpload) => {
const params = useParams() const params = useParams()
const { imgSizeLimit, docSizeLimit, audioSizeLimit, videoSizeLimit } = useFileSizeLimit(fileConfig.fileUploadConfig) const { imgSizeLimit, docSizeLimit, audioSizeLimit, videoSizeLimit } = useFileSizeLimit(fileConfig.fileUploadConfig)
const checkSizeLimit = (fileType: string, fileSize: number) => { const checkSizeLimit = useCallback((fileType: string, fileSize: number) => {
switch (fileType) { switch (fileType) {
case SupportUploadFileTypes.image: { case SupportUploadFileTypes.image: {
if (fileSize > imgSizeLimit) { if (fileSize > imgSizeLimit) {
@ -120,7 +120,7 @@ export const useFile = (fileConfig: FileUpload) => {
return true return true
} }
} }
} }, [audioSizeLimit, docSizeLimit, imgSizeLimit, notify, t, videoSizeLimit])
const handleAddFile = useCallback((newFile: FileEntity) => { const handleAddFile = useCallback((newFile: FileEntity) => {
const { const {
@ -203,13 +203,15 @@ export const useFile = (fileConfig: FileUpload) => {
} }
handleAddFile(uploadingFile) handleAddFile(uploadingFile)
fetchRemoteFileInfo(url).then((res) => { uploadRemoteFileInfo(url).then((res) => {
const newFile = { const newFile = {
...uploadingFile, ...uploadingFile,
type: res.file_type, type: res.mime_type,
size: res.file_length, size: res.size,
progress: 100, progress: 100,
supportFileType: getSupportFileType(url, res.file_type, allowedFileTypes?.includes(SupportUploadFileTypes.custom)), supportFileType: getSupportFileType(res.name, res.mime_type, allowedFileTypes?.includes(SupportUploadFileTypes.custom)),
uploadedId: res.id,
url: res.url,
} }
if (!checkSizeLimit(newFile.supportFileType, newFile.size)) if (!checkSizeLimit(newFile.supportFileType, newFile.size))
handleRemoveFile(uploadingFile.id) handleRemoveFile(uploadingFile.id)

View File

@ -324,9 +324,10 @@ export const verifyForgotPasswordToken: Fetcher<CommonResponse & { is_valid: boo
export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) => export const changePasswordWithToken: Fetcher<CommonResponse, { url: string; body: { token: string; new_password: string; password_confirm: string } }> = ({ url, body }) =>
post<CommonResponse>(url, { body }) post<CommonResponse>(url, { body })
export const fetchRemoteFileInfo = (url: string) => { export const uploadRemoteFileInfo = (url: string) => {
return get<{ file_type: string; file_length: number }>(`/remote-files/${url}`) return post<{ id: string; name: string; size: number; mime_type: string; url: string }>('/remote-files/upload', { body: { url } })
} }
export const sendEMailLoginCode = (email: string, language = 'en-US') => export const sendEMailLoginCode = (email: string, language = 'en-US') =>
post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } }) post<CommonResponse & { data: string }>('/email-code-login', { body: { email, language } })