41 lines
1.4 KiB
TypeScript
41 lines
1.4 KiB
TypeScript
import { Alert } from 'react-native';
|
|
import pLimit from 'p-limit';
|
|
import { getMediaByDateRange } from './media';
|
|
import { processAndUploadMedia } from './uploader';
|
|
import { ExtendedAsset } from './types';
|
|
|
|
// 设置最大并发数
|
|
const CONCURRENCY_LIMIT = 10; // 同时最多上传10个文件
|
|
const limit = pLimit(CONCURRENCY_LIMIT);
|
|
|
|
// 手动触发上传
|
|
export const triggerManualUpload = async (startDate: Date, endDate: Date) => {
|
|
try {
|
|
const media = await getMediaByDateRange(startDate, endDate);
|
|
if (media.length === 0) {
|
|
Alert.alert('提示', '在指定时间范围内未找到媒体文件');
|
|
return [];
|
|
}
|
|
|
|
const uploadPromises = media.map((asset: ExtendedAsset) =>
|
|
limit(() => processAndUploadMedia(asset))
|
|
);
|
|
|
|
const results = await Promise.all(uploadPromises);
|
|
|
|
// 过滤掉因为已上传而返回 null 的结果
|
|
const finalResults = results.filter(result => result !== null);
|
|
|
|
console.log('Manual upload completed.', {
|
|
total: media.length,
|
|
uploaded: finalResults.length,
|
|
skipped: media.length - finalResults.length
|
|
});
|
|
|
|
return finalResults;
|
|
} catch (error) {
|
|
console.error('手动上传过程中出现错误:', error);
|
|
Alert.alert('错误', '上传过程中出现错误');
|
|
throw error;
|
|
}
|
|
}; |