83 lines
3.2 KiB
TypeScript
83 lines
3.2 KiB
TypeScript
import pLimit from 'p-limit';
|
|
import { PermissionService } from '../PermissionService';
|
|
import i18n from '@/i18n';
|
|
import { getUploadTaskStatus, insertUploadTask } from '../db';
|
|
import { getMediaByDateRange } from './media';
|
|
import { ExtendedAsset } from './types';
|
|
import { processAndUploadMedia } from './uploader';
|
|
|
|
// 设置最大并发数
|
|
const CONCURRENCY_LIMIT = 1; // 同时最多上传10个文件
|
|
const limit = pLimit(CONCURRENCY_LIMIT);
|
|
|
|
// 手动触发上传
|
|
export const triggerManualUpload = async (
|
|
startDate: Date,
|
|
endDate: Date,
|
|
onProgress?: (progress: {
|
|
totalCount: number;
|
|
uploadedCount: number;
|
|
totalBytes: number; // Overall total size
|
|
uploadedBytes: number; // Overall uploaded size
|
|
currentAsset: ExtendedAsset; // To show a thumbnail
|
|
}) => void
|
|
) => {
|
|
try {
|
|
const media = await getMediaByDateRange(startDate, endDate);
|
|
if (media.length === 0) {
|
|
PermissionService.show({ title: i18n.t('permission:title.getMediaFailed'), message: i18n.t('permission:message.noMediaFound') });
|
|
return [];
|
|
}
|
|
|
|
const progressMap = new Map<string, { loaded: number; total: number }>();
|
|
|
|
const results = [];
|
|
let uploadedCount = 0;
|
|
|
|
for (const asset of media) {
|
|
const existingTask = await getUploadTaskStatus(asset.uri);
|
|
if (!existingTask) {
|
|
await insertUploadTask({ uri: asset.uri, filename: asset.filename, status: 'pending', progress: 0 });
|
|
} else if (existingTask.status === 'success' || existingTask.status === 'skipped') {
|
|
console.log(`File ${asset.uri} already ${existingTask.status}, skipping processing.`);
|
|
uploadedCount++; // Also count skipped files as 'processed'
|
|
continue;
|
|
}
|
|
|
|
const result = await limit(() => processAndUploadMedia(asset, (fileProgress) => {
|
|
progressMap.set(asset.uri, fileProgress);
|
|
const uploadedBytes = Array.from(progressMap.values()).reduce((sum, p) => sum + p.loaded, 0);
|
|
const totalBytes = Array.from(progressMap.values()).reduce((sum, p) => sum + p.total, 0);
|
|
onProgress?.({
|
|
totalCount: media.length,
|
|
uploadedCount,
|
|
totalBytes,
|
|
uploadedBytes,
|
|
currentAsset: asset,
|
|
});
|
|
}));
|
|
|
|
if (result) {
|
|
results.push(result);
|
|
if (result.originalSuccess) {
|
|
uploadedCount++;
|
|
}
|
|
}
|
|
}
|
|
|
|
// 过滤掉因为已上传而返回 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);
|
|
PermissionService.show({ title: i18n.t('permission:title.error'), message: i18n.t('permission:message.uploadError') });
|
|
throw error;
|
|
}
|
|
}; |