同步修改

This commit is contained in:
tianmo committed 2026-10-17 17:52:08 +08:00
1 parent d647453085
commit 9939943e31
6 files changed
+60 -27

No files matched your search

+31 -17
View File
@@ -25,29 +25,41 @@ export async function abandonSiteSyncJob(
const targetBaseUrl = typeof job.payload.targetBaseUrl === 'string'
? job.payload.targetBaseUrl.replace(/\/+$/, '')
: ''
let cleanup: CleanupResult
let cleanup: CleanupResult | null = null
let cleanupError: string | null = null
if (direction === 'push') {
if (!targetBaseUrl || !token) {
throw new Error('放弃推送任务并清理目标站残留时,需要目标站 Token')
}
cleanup = await fetchSiteSyncJson<CleanupResult>(
`${targetBaseUrl}/api/site-sync/cleanup`,
token,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ jobId })
if (targetBaseUrl && token) {
try {
cleanup = await fetchSiteSyncJson<CleanupResult>(
`${targetBaseUrl}/api/site-sync/cleanup`,
token,
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ jobId })
}
)
} catch (error) {
cleanupError = error instanceof Error ? error.message : '目标站残留清理失败'
}
)
}
} else {
cleanup = await cleanupSiteSyncJobArtifacts(jobId)
try {
cleanup = await cleanupSiteSyncJobArtifacts(jobId)
} catch (error) {
cleanupError = error instanceof Error ? error.message : '本地残留清理失败'
}
}
if (cleanup.failed > 0) {
throw new Error(`同步残留清理失败:${cleanup.failed} 个对象`)
}
const cleanupPending = Boolean(
cleanupError
|| !cleanup
|| cleanup.failed > 0
)
const message = `已放弃同步 · 清理 ${cleanup.deleted} 个残留对象`
const message = cleanupPending
? `已放弃同步 · 残留待清理${cleanupError ? `:${cleanupError}` : ''}`
: `已放弃同步 · 清理 ${cleanup!.deleted} 个残留对象`
updateBackupJob(jobId, {
status: 'failed',
message,
@@ -58,6 +70,8 @@ export async function abandonSiteSyncJob(
resumable: false,
abandoned: true,
cancelled: false,
cleanupPending,
cleanupError,
failedKeys: [],
cleanup
}
+9 -4
View File
@@ -1,6 +1,8 @@
import { basename } from 'node:path'
import { File as BufferFile } from 'node:buffer'
import {
Agent,
FormData as UndiciFormData,
fetch as undiciFetch
} from 'undici'
import type { Readable } from 'node:stream'
@@ -399,7 +401,7 @@ async function uploadObject(
getJobAbortSignal(jobId)
)
throwIfJobCancellationRequested(jobId)
const form = new FormData()
const form = new UndiciFormData()
const isPrimary = key === row.key
form.append('jobId', jobId)
form.append('mediaKey', row.key)
@@ -412,8 +414,11 @@ async function uploadObject(
form.append('contentType', contentType)
form.append(
'file',
new Blob([new Uint8Array(bytes)], { type: contentType }),
basename(key)
new BufferFile(
[new Uint8Array(bytes)],
basename(key),
{ type: contentType }
)
)
const response = await undiciFetch(
@@ -424,7 +429,7 @@ async function uploadObject(
headers: { 'Auth-Token': token },
signal: getJobAbortSignal(jobId),
dispatcher: agents.upload
} as Parameters<typeof undiciFetch>[1]
}
)
if (!response.ok) throw await responseError(response)
await response.json().catch(() => null)