56 lines
1.6 KiB
TypeScript
56 lines
1.6 KiB
TypeScript
import { apiClient } from './client'
|
|
import type { ApiResponse } from '@/shared/types/types'
|
|
import { optimizeImageForUpload } from '@/shared/utils/imageUpload'
|
|
|
|
export interface UploadedFile {
|
|
object_key: string
|
|
url: string
|
|
thumbnail_url?: string
|
|
medium_url?: string
|
|
filename: string
|
|
content_type: string
|
|
size: number
|
|
}
|
|
|
|
export async function uploadFile(file: File, scene: string) {
|
|
const uploadTarget = await optimizeImageForUpload(file, scene)
|
|
const form = new FormData()
|
|
form.append('file', uploadTarget)
|
|
form.append('scene', scene)
|
|
const { data } = await apiClient.post<ApiResponse<UploadedFile>>('/files/upload', form, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
})
|
|
return data.data
|
|
}
|
|
|
|
export async function uploadAdminFile(
|
|
file: File,
|
|
scene: string,
|
|
options: { optimize?: boolean } = {}
|
|
) {
|
|
const uploadTarget = options.optimize === false ? file : await optimizeImageForUpload(file, scene)
|
|
const form = new FormData()
|
|
form.append('file', uploadTarget)
|
|
form.append('scene', scene)
|
|
const { data } = await apiClient.post<ApiResponse<UploadedFile>>('/admin/files/upload', form, {
|
|
headers: { 'Content-Type': 'multipart/form-data' },
|
|
})
|
|
return data.data
|
|
}
|
|
|
|
export async function fetchAdminFileBlob(key: string) {
|
|
const { data } = await apiClient.get<Blob>('/admin/files/object', {
|
|
params: { key },
|
|
responseType: 'blob',
|
|
})
|
|
return data
|
|
}
|
|
|
|
export async function fetchFileBlobByURL(fileURL: string) {
|
|
const apiPath = fileURL.startsWith('/api/') ? fileURL.slice(4) : fileURL
|
|
const { data } = await apiClient.get<Blob>(apiPath, {
|
|
responseType: 'blob',
|
|
})
|
|
return data
|
|
}
|