35 lines
1.2 KiB
TypeScript
35 lines
1.2 KiB
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 || {}
|
|
|
|
console.log('[uploadFile] 开始上传:', { uri, mimeType, fileName, platform: Platform.OS })
|
|
|
|
const formData = new FormData()
|
|
|
|
// React Native 中 FormData 需要使用特定格式
|
|
const fileObject = {
|
|
uri: Platform.OS === 'android' ? uri : uri.replace('file://', ''),
|
|
type: mimeType || 'image/jpeg',
|
|
name: fileName || 'uploaded_file.jpg',
|
|
}
|
|
|
|
console.log('[uploadFile] FormData file object:', fileObject)
|
|
formData.append('file', fileObject as any)
|
|
|
|
const fileController = root.get(FileController)
|
|
const { data, error } = await handleError(async () => await fileController.uploadS3(formData))
|
|
|
|
if (error || !data?.data) {
|
|
console.error('[uploadFile] 上传失败:', { error, data, uri, formData })
|
|
throw error || new Error('上传失败')
|
|
}
|
|
|
|
console.log('[uploadFile] 上传成功:', data.data)
|
|
return data.data
|
|
}
|