58 lines
1.4 KiB
TypeScript
58 lines
1.4 KiB
TypeScript
import { http, type JavaApiResponse } from './http.ts'
|
|
import { JAVA_API_PREFIX } from './url.ts'
|
|
|
|
export interface UploadedFileRef {
|
|
fileKey: string;
|
|
originalFilename?: string;
|
|
relativePath?: string;
|
|
}
|
|
|
|
export interface UploadFileVo {
|
|
fileKey: string;
|
|
originalFilename: string;
|
|
localPath: string;
|
|
size: number;
|
|
relativePath?: string;
|
|
objectKey?: string;
|
|
url?: string;
|
|
mediaType?: "image" | "video" | "audio" | "file" | string;
|
|
}
|
|
|
|
export interface UploadTempFileOptions {
|
|
relativePath?: string;
|
|
uploadToOss?: boolean;
|
|
moduleType?: string;
|
|
}
|
|
|
|
export async function uploadTempFileToJava(
|
|
file: File,
|
|
relativePathOrOptions?: string | UploadTempFileOptions,
|
|
) {
|
|
const options =
|
|
typeof relativePathOrOptions === 'string'
|
|
? { relativePath: relativePathOrOptions }
|
|
: relativePathOrOptions || {}
|
|
const formData = new FormData()
|
|
formData.append('file', file)
|
|
if (options.relativePath) {
|
|
formData.append('relativePath', options.relativePath)
|
|
}
|
|
if (options.uploadToOss) {
|
|
formData.append('uploadToOss', 'true')
|
|
}
|
|
if (options.moduleType) {
|
|
formData.append('moduleType', options.moduleType)
|
|
}
|
|
const response = await http.post<JavaApiResponse<UploadFileVo>>(
|
|
`${JAVA_API_PREFIX}/files/upload`,
|
|
formData,
|
|
{
|
|
headers: {
|
|
'Content-Type': 'multipart/form-data',
|
|
},
|
|
timeout: 120000,
|
|
},
|
|
)
|
|
return response.data
|
|
}
|