expo-popcore-app/lib/uploadFile.ts

26 lines
821 B
TypeScript

import { root } from '@repo/core'
import { FileController } from '@repo/sdk'
import { Platform } from 'react-native'
import { handleError } from '@/hooks/use-error'
export async function uploadFile(params: { uri: string; mimeType?: string; fileName?: string }): Promise<string> {
const { uri, mimeType, fileName } = params
const file = {
name: fileName || 'uploaded_file.jpg',
type: mimeType || 'image/jpeg',
uri: Platform.OS === 'android' ? uri : uri.replace('file://', ''),
}
const formData = new FormData()
formData.append('file', file as any)
const fileController = root.get(FileController)
const { data, error } = await handleError(async () => await fileController.uploadS3(formData))
if (error || !data?.data) {
throw error || new Error('上传失败')
}
return data.data
}