50 lines
1.2 KiB
TypeScript
50 lines
1.2 KiB
TypeScript
export interface AdminUploadResponseData {
|
|
id?: string | number
|
|
fileId?: string | number
|
|
}
|
|
|
|
export interface AdminUploadResponse {
|
|
id?: string | number
|
|
fileId?: string | number
|
|
data?: AdminUploadResponseData
|
|
}
|
|
|
|
export interface AdminUploadFileLike {
|
|
uid?: string | number
|
|
url?: string
|
|
thumbUrl?: string
|
|
fileId?: string | number
|
|
response?: AdminUploadResponse
|
|
}
|
|
|
|
export interface AdminUploadTaskFile extends File {
|
|
uid: string
|
|
}
|
|
|
|
export const getUploadResponse = (file: AdminUploadFileLike | null | undefined): AdminUploadResponse | null => {
|
|
if (!file || typeof file !== 'object') {
|
|
return null
|
|
}
|
|
return file.response || null
|
|
}
|
|
|
|
export const resolveUploadedFileId = (file: AdminUploadFileLike | null | undefined): string => {
|
|
const response = getUploadResponse(file)
|
|
const responseId = response?.id ?? response?.data?.id ?? response?.fileId ?? response?.data?.fileId ?? file?.fileId
|
|
if (responseId !== undefined && responseId !== null && responseId !== '') {
|
|
return String(responseId)
|
|
}
|
|
|
|
const fileUid = String(file?.uid || '')
|
|
if (/^\d+$/.test(fileUid)) {
|
|
return fileUid
|
|
}
|
|
|
|
const fileUrl = String(file?.url || '')
|
|
if (/^\d+$/.test(fileUrl)) {
|
|
return fileUrl
|
|
}
|
|
|
|
return ''
|
|
}
|