37 lines
837 B
TypeScript
37 lines
837 B
TypeScript
import type { IResponse } from '.'
|
|
|
|
const putFile = (file: File, url: string): Promise<string> => {
|
|
return new Promise((resolve, reject) => {
|
|
$fetch(url, {
|
|
method: 'PUT',
|
|
body: file,
|
|
headers: {
|
|
'Content-Type': file.type,
|
|
},
|
|
})
|
|
.then(() => {
|
|
resolve(url.split('?')[0])
|
|
})
|
|
.catch(() => {
|
|
reject(new Error('File upload failed'))
|
|
})
|
|
})
|
|
}
|
|
|
|
export const uploadFile = async (file: File, type: 'resource' | 'temp') => {
|
|
const signedUrl = await http<IResponse<{ data: string }>>(
|
|
`/common/oss/getSignUrl`,
|
|
{
|
|
method: 'POST',
|
|
query: {
|
|
fileName: encodeURI(file.name),
|
|
fileType: type,
|
|
fileSize: file.size,
|
|
fileMime: file.type,
|
|
},
|
|
},
|
|
)
|
|
const url = signedUrl.data
|
|
return await putFile(file, url)
|
|
}
|