fix: 修复watcher检查分片文件的部分问题

This commit is contained in:
Pan Qiancheng 2025-12-29 17:35:53 +08:00
parent 14d593cc89
commit fdf948d3ee
3 changed files with 87 additions and 30 deletions

View File

@ -87,7 +87,7 @@ const watchers = [
}
},
{
name: '确定uploading的文件状态',
name: '处理长时间未完成分片上传的文件',
entity: 'extraFile',
filter: async () => {
const now = Date.now();
@ -126,15 +126,32 @@ const watchers = [
successIds.push(d.id);
}
else {
// 这个文件不存在,此时去判断是否是上传完但是未合并的情况
const { parts } = await cos.listMultipartUploads(context.getApplication(), d, context);
assert(parts);
const isDone = parts.every(p => p.etag && p.etag.length > 0);
if (isDone) {
// 标记为成功
// 这个文件不存在,此时去尝试合并,如果合并失败,则标记为失败
try {
if (d.chunkInfo?.merged) {
// 已经合并过了,说明是合并后删除的
failedIds.push(d.id);
continue;
}
// 去合并分片
await cos.mergeChunkedUpload(context.getApplication(), d, context);
await context.operate('extraFile', {
id: await generateNewIdAsync(),
action: 'update',
data: {
chunkInfo: {
...d.chunkInfo,
merged: true,
},
},
filter: {
id: d.id,
},
}, {});
successIds.push(d.id);
}
else {
catch (err) {
console.log(`分片合并失败,文件: ${d.id} 标记为上传失败: `, err);
failedIds.push(d.id);
}
}

View File

@ -89,7 +89,7 @@ const watchers = [
}
},
{
name: '确定uploading的文件状态',
name: '处理长时间未完成分片上传的文件',
entity: 'extraFile',
filter: async () => {
const now = Date.now();
@ -128,15 +128,32 @@ const watchers = [
successIds.push(d.id);
}
else {
// 这个文件不存在,此时去判断是否是上传完但是未合并的情况
const { parts } = await cos.listMultipartUploads(context.getApplication(), d, context);
(0, assert_1.assert)(parts);
const isDone = parts.every(p => p.etag && p.etag.length > 0);
if (isDone) {
// 标记为成功
// 这个文件不存在,此时去尝试合并,如果合并失败,则标记为失败
try {
if (d.chunkInfo?.merged) {
// 已经合并过了,说明是合并后删除的
failedIds.push(d.id);
continue;
}
// 去合并分片
await cos.mergeChunkedUpload(context.getApplication(), d, context);
await context.operate('extraFile', {
id: await (0, uuid_1.generateNewIdAsync)(),
action: 'update',
data: {
chunkInfo: {
...d.chunkInfo,
merged: true,
},
},
filter: {
id: d.id,
},
}, {});
successIds.push(d.id);
}
else {
catch (err) {
console.log(`分片合并失败,文件: ${d.id} 标记为上传失败: `, err);
failedIds.push(d.id);
}
}

View File

@ -104,7 +104,7 @@ const watchers: Watcher<
}
},
{
name: '确定uploading的文件状态',
name: '处理长时间未完成分片上传的文件',
entity: 'extraFile',
filter: async () => {
const now = Date.now();
@ -148,19 +148,42 @@ const watchers: Watcher<
successIds.push(d.id!);
}
else {
// 这个文件不存在,此时去判断是否是上传完但是未合并的情况
const { parts } = await cos.listMultipartUploads(
context.getApplication() as EntityDict['application']['Schema'],
d as EntityDict['extraFile']['OpSchema'],
context
)
assert(parts);
const isDone = parts.every(p => p.etag && p.etag.length > 0);
if (isDone) {
// 标记为成功
successIds.push(d.id!);
}
else {
// 这个文件不存在,此时去尝试合并,如果合并失败,则标记为失败
try {
if (d.chunkInfo?.merged) {
// 已经合并过了,说明是合并后删除的
failedIds.push(d.id!);
continue;
}
// 去合并分片
await cos.mergeChunkedUpload(
context.getApplication() as EntityDict['application']['Schema'],
d as EntityDict['extraFile']['OpSchema'],
context
);
await context.operate(
'extraFile',
{
id: await generateNewIdAsync(),
action: 'update',
data: {
chunkInfo: {
...d.chunkInfo,
merged: true,
} as any,
},
filter: {
id: d.id!,
},
},
{}
);
successIds.push(d.id!);
} catch (err) {
console.log(`分片合并失败,文件: ${d.id} 标记为上传失败: `, err);
failedIds.push(d.id!);
}
}