feat: 自动上传组件 Co-authored-by: Junhui Chen <chenjunhui@fairclip.cn> Co-committed-by: Junhui Chen <chenjunhui@fairclip.cn>
52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
import * as ImageManipulator from 'expo-image-manipulator';
|
|
import { confirmUpload, getUploadUrl } from '../background-uploader/api';
|
|
import { ExtendedAsset } from '../background-uploader/types';
|
|
import { uploadFile } from '../background-uploader/uploader';
|
|
import { compressImage } from '../image-process/imageCompress';
|
|
|
|
/**
|
|
* @description 从视频资源创建缩略图文件
|
|
* @param asset 媒体资源
|
|
* @param width 缩略图宽度,默认为 300
|
|
* @returns 返回一个包含缩略图的 File 对象
|
|
*/
|
|
export const createVideoThumbnailFile = async (asset: { uri: string }, width: number = 300): Promise<File> => {
|
|
const thumbnailResult = await ImageManipulator.manipulateAsync(
|
|
asset.uri,
|
|
[{ resize: { width } }],
|
|
{ compress: 0.7, format: ImageManipulator.SaveFormat.WEBP }
|
|
);
|
|
|
|
const response = await fetch(thumbnailResult.uri);
|
|
const blob = await response.blob();
|
|
|
|
return new File([blob], `thumb_${Date.now()}.webp`, { type: 'image/webp' });
|
|
};
|
|
|
|
// 提取视频的首帧进行压缩并上传
|
|
export const uploadVideoThumbnail = async (asset: ExtendedAsset) => {
|
|
try {
|
|
const manipResult = await compressImage(asset.uri);
|
|
const response = await fetch(manipResult.uri);
|
|
const blob = await response.blob();
|
|
const filename = asset.filename ?
|
|
`compressed_${asset.filename}` :
|
|
`image_${Date.now()}_compressed.jpg`;
|
|
const compressedFile = new File([blob], filename, { type: 'image/jpeg' });
|
|
|
|
const { upload_url, file_id } = await getUploadUrl(compressedFile, {
|
|
originalUri: asset.uri,
|
|
creationTime: asset.creationTime,
|
|
mediaType: 'image',
|
|
isCompressed: true
|
|
});
|
|
|
|
await uploadFile(compressedFile, upload_url);
|
|
await confirmUpload(file_id);
|
|
|
|
return { success: true, file_id };
|
|
} catch (error) {
|
|
return { success: false, error };
|
|
}
|
|
};
|