- 备份恢复的 multipart 分支改用 Web FormData,移除旧解析器约 128 MB 的限制 - 初始化恢复新增 application/octet-stream 流式写盘,前端改为直接上传文件流,避免整包进入内存 - 站点同步改用 Web FormData,并在读取请求体前拒绝超过 500 MB 的文件 - 新增 multipart 字段、重复字段和文件读取回归测试 验证: - 2 GB 测试备份包流式恢复预览成功,服务内存保持低位 - 130 MB 站点同步上传成功 - 原有备份预览及图片、视频、文件上传正常 - npm test 234 项通过;npm run typecheck 与 eslint 通过
57 lines
2.2 KiB
TypeScript
57 lines
2.2 KiB
TypeScript
import { getHeader, readFormData, type H3Event } from 'h3'
|
|
import { createApiError } from '../../utils/api-error'
|
|
import { isInitialized } from '../../utils/auth'
|
|
import { serializeBackupJob } from '../../utils/backup/api'
|
|
import { startFullRestoreJob } from '../../utils/backup/runner'
|
|
import { BACKUP_ARCHIVE_EXT, getBackupsDir } from '../../utils/backup/paths'
|
|
import { readFormDataFiles } from '../../utils/multipart-form'
|
|
import { join } from 'node:path'
|
|
import { createWriteStream, promises as fs } from 'node:fs'
|
|
import { pipeline } from 'node:stream/promises'
|
|
|
|
async function saveRestoreUpload(event: H3Event, archivePath: string): Promise<void> {
|
|
const contentType = getHeader(event, 'content-type') ?? ''
|
|
try {
|
|
if (contentType.includes('application/octet-stream')) {
|
|
await pipeline(event.node.req, createWriteStream(archivePath))
|
|
} else {
|
|
const form = await readFormData(event)
|
|
const file = readFormDataFiles(form, ['file'])[0]
|
|
if (!file) {
|
|
createApiError(event, 'INVALID_REQUEST', '请上传备份文件', 400)
|
|
}
|
|
await fs.writeFile(archivePath, new Uint8Array(await file.arrayBuffer()))
|
|
}
|
|
const stat = await fs.stat(archivePath)
|
|
if (stat.size === 0) {
|
|
createApiError(event, 'INVALID_REQUEST', '备份文件为空', 400)
|
|
}
|
|
} catch (error) {
|
|
await fs.rm(archivePath, { force: true }).catch(() => {})
|
|
throw error
|
|
}
|
|
}
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
if (isInitialized()) {
|
|
createApiError(event, 'FORBIDDEN', '系统已初始化,请在存储页恢复备份', 403)
|
|
}
|
|
|
|
await fs.mkdir(getBackupsDir(), { recursive: true })
|
|
const archivePath = join(getBackupsDir(), `setup-restore-${Date.now()}${BACKUP_ARCHIVE_EXT}`)
|
|
await saveRestoreUpload(event, archivePath)
|
|
|
|
try {
|
|
const job = startFullRestoreJob(archivePath, 'overwrite')
|
|
return {
|
|
job: serializeBackupJob(job),
|
|
message: '恢复任务已开始,完成后请使用备份中的管理员账号登录'
|
|
}
|
|
} catch (error) {
|
|
if (error instanceof Error && error.message === 'BACKUP_JOB_RUNNING') {
|
|
createApiError(event, 'CONFLICT', '已有备份或迁移任务正在运行', 409)
|
|
}
|
|
throw error
|
|
}
|
|
})
|