expo-duooomi-app/utils/uploadFile.ts

98 lines
2.5 KiB
TypeScript

import { root } from '@repo/core'
import { type ConvertToWebpRequest as SDKConvertToWebpRequest, FileController } from '@repo/sdk'
import { Platform } from 'react-native'
import { Toast } from '@/@share/components'
import { handleError } from '@/hooks/data/use-error'
// ✅ 本地扩展类型,添加 width 和 height
interface ConvertToWebpRequest extends SDKConvertToWebpRequest {
width?: number
height?: number
}
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) {
Toast.show({ title: '上传失败' })
throw new Error(error?.message || '上传失败')
}
return data.data
}
export async function uploadAndConvertToAniUrl(params: {
url: string
mimeType?: string
fileName?: string
width?: number
height?: number
fps?: string
}): Promise<string> {
const { url, width = 360, height = 360, fps = '24' } = params
const body = {
videoUrl: url,
width,
height,
fps,
}
const fileController = root.get(FileController)
const { data, error } = await handleError(async () => await fileController.convertToAni(body))
if (error || !data?.data) {
Toast.show({ title: 'ANI 转换失败' })
throw new Error(error?.message || 'ANI 转换失败')
}
return data.data
}
// ✅ 使用本地扩展的类型
export async function mp4ToWebpUrl(params: ConvertToWebpRequest): Promise<string> {
const {
videoUrl,
width = 256,
height = 256,
compressionLevel = 3,
quality = 70,
loop = true,
mode = 'crop',
fps = 24,
} = params || {}
const body: SDKConvertToWebpRequest = {
videoUrl,
compressionLevel,
quality,
loop,
resolution: `${width}x${height}`,
fps,
mode,
}
const fileController = root.get(FileController)
const { data, error } = await handleError(async () => await fileController.convertToWebp(body))
// console.log('mp4ToWebpUrl data,error-----------', data, error)
if (error || !data?.data) {
// Toast.show({ title: 'WebP 转换失败' })
throw new Error(error?.message || 'WebP 转换失败')
}
return data.data
}