Files
FileHost/server/api/admin/backup/restore.post.ts
T
tianmo 58424fe55d fix: 支持备份恢复与站点同步的大文件上传 版本更新到1.5.4
- 备份恢复的 multipart 分支改用 Web FormData,移除旧解析器约 128 MB 的限制
- 初始化恢复新增 application/octet-stream 流式写盘,前端改为直接上传文件流,避免整包进入内存
- 站点同步改用 Web FormData,并在读取请求体前拒绝超过 500 MB 的文件
- 新增 multipart 字段、重复字段和文件读取回归测试

验证:
- 2 GB 测试备份包流式恢复预览成功,服务内存保持低位
- 130 MB 站点同步上传成功
- 原有备份预览及图片、视频、文件上传正常
- npm test 234 项通过;npm run typecheck 与 eslint 通过
2026-10-19 15:26:13 +08:00

172 lines
5.5 KiB
TypeScript

import { readFormData, type H3Event } from 'h3'
import { requireAdminAuth } from '../../../utils/access'
import { createApiError } from '../../../utils/api-error'
import { serializeBackupJob } from '../../../utils/backup/api'
import { previewFullRestore } from '../../../utils/backup/full-restore'
import { startFullRestoreJob } from '../../../utils/backup/runner'
import { listAllBackupJobsForUser } from '../../../utils/backup/jobs'
import {
BACKUP_ARCHIVE_EXT,
getBackupsDir,
getBackupStagingDir,
isSafeBackupFilename
} from '../../../utils/backup/paths'
import { createWriteStream, promises as fs } from 'node:fs'
import { basename, dirname, join } from 'node:path'
import { pipeline } from 'node:stream/promises'
import { readFormDataFiles } from '../../../utils/multipart-form'
interface RestoreBody {
archiveName?: string
conflictMode?: 'skip' | 'overwrite'
dryRun?: boolean
}
async function streamUploadToFile(event: H3Event, archivePath: string): Promise<void> {
await fs.mkdir(dirname(archivePath), { recursive: true })
try {
await pipeline(event.node.req, createWriteStream(archivePath))
const stat = await fs.stat(archivePath)
if (stat.size === 0) {
throw new Error('备份文件为空')
}
} catch (error) {
await fs.rm(archivePath, { force: true }).catch(() => {})
throw error
}
}
async function resolveRestoreInput(event: H3Event, userId: number): Promise<{
archivePath: string
body: RestoreBody
cleanup?: () => Promise<void>
}> {
const contentType = getHeader(event, 'content-type') ?? ''
if (contentType.includes('application/octet-stream')) {
const dryRun = getHeader(event, 'x-pichost-dry-run') === 'true'
const conflictMode = getHeader(event, 'x-pichost-conflict-mode') === 'skip'
? 'skip'
: 'overwrite'
if (dryRun) {
const tempDir = getBackupStagingDir(`upload-preview-${Date.now()}`)
await fs.mkdir(tempDir, { recursive: true })
const archivePath = join(tempDir, 'package.phost.tar.gz')
await streamUploadToFile(event, archivePath)
return {
archivePath,
body: { conflictMode, dryRun: true },
cleanup: async () => {
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {})
}
}
}
await fs.mkdir(getBackupsDir(), { recursive: true })
const archivePath = join(
getBackupsDir(),
`upload-${Date.now()}${BACKUP_ARCHIVE_EXT}`
)
await streamUploadToFile(event, archivePath)
return {
archivePath,
body: { conflictMode, dryRun: false }
}
}
if (contentType.includes('multipart/form-data')) {
const form = await readFormData(event)
const filePart = readFormDataFiles(form, ['file'])[0]
if (!filePart) {
createApiError(event, 'INVALID_REQUEST', '请上传备份文件', 400)
}
const conflictField = form.get('conflictMode')
const dryRunField = form.get('dryRun')
const dryRun = dryRunField === 'true'
const fileBytes = new Uint8Array(await filePart.arrayBuffer())
if (dryRun) {
const tempDir = getBackupStagingDir(`upload-preview-${Date.now()}`)
await fs.mkdir(tempDir, { recursive: true })
const archivePath = join(tempDir, 'package.phost.tar.gz')
await fs.writeFile(archivePath, fileBytes)
return {
archivePath,
body: {
conflictMode: conflictField === 'skip' ? 'skip' : 'overwrite',
dryRun: true
},
cleanup: async () => {
await fs.rm(tempDir, { recursive: true, force: true }).catch(() => {})
}
}
}
await fs.mkdir(getBackupsDir(), { recursive: true })
const safeName = `upload-${Date.now()}${BACKUP_ARCHIVE_EXT}`
const archivePath = join(getBackupsDir(), safeName)
await fs.writeFile(archivePath, fileBytes)
return {
archivePath,
body: {
conflictMode: conflictField === 'skip' ? 'skip' : 'overwrite',
dryRun: false
}
}
}
const body = await readBody<RestoreBody>(event)
if (!body?.archiveName || !isSafeBackupFilename(body.archiveName)) {
createApiError(event, 'INVALID_REQUEST', '请选择有效的备份文件', 400)
}
const allowedArchives = new Set(
listAllBackupJobsForUser(userId).flatMap((job) => {
const names: string[] = []
if (job.outputPath) names.push(basename(job.outputPath))
if (typeof job.payload.archivePath === 'string') {
names.push(basename(job.payload.archivePath))
}
return names
})
)
if (!allowedArchives.has(body.archiveName)) {
createApiError(event, 'INVALID_REQUEST', '备份文件不存在', 404)
}
const archivePath = join(getBackupsDir(), body.archiveName)
try {
await fs.access(archivePath)
} catch {
createApiError(event, 'INVALID_REQUEST', '备份文件不存在', 400)
}
return { archivePath, body }
}
export default defineEventHandler(async (event) => {
const user = await requireAdminAuth(event)
const { archivePath, body, cleanup } = await resolveRestoreInput(event, user.id)
const conflictMode = body.conflictMode === 'skip' ? 'skip' : 'overwrite'
if (body.dryRun) {
try {
const preview = await previewFullRestore(archivePath)
return { preview }
} finally {
await cleanup?.()
}
}
try {
const job = startFullRestoreJob(archivePath, conflictMode, user.id)
return { job: serializeBackupJob(job) }
} catch (error) {
if (error instanceof Error && error.message === 'BACKUP_JOB_RUNNING') {
createApiError(event, 'CONFLICT', '已有备份或迁移任务正在运行', 409)
}
throw error
}
})