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 通过
This commit is contained in:
1 parent
3759422222
commit
58424fe55d
7 files changed
+107
-34
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import type { H3Event } from 'h3'
|
||||
import { readFormData, type H3Event } from 'h3'
|
||||
import { requireAdminAuth } from '../../../utils/access'
|
||||
import { createApiError } from '../../../utils/api-error'
|
||||
import { serializeBackupJob } from '../../../utils/backup/api'
|
||||
@@ -14,6 +14,7 @@ import {
|
||||
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
|
||||
@@ -75,21 +76,22 @@ async function resolveRestoreInput(event: H3Event, userId: number): Promise<{
|
||||
}
|
||||
|
||||
if (contentType.includes('multipart/form-data')) {
|
||||
const form = await readMultipartFormData(event)
|
||||
const filePart = form?.find(part => part.name === 'file' && part.data?.length)
|
||||
if (!filePart?.data) {
|
||||
const form = await readFormData(event)
|
||||
const filePart = readFormDataFiles(form, ['file'])[0]
|
||||
if (!filePart) {
|
||||
createApiError(event, 'INVALID_REQUEST', '请上传备份文件', 400)
|
||||
}
|
||||
|
||||
const conflictField = form?.find(part => part.name === 'conflictMode')?.data?.toString('utf8')
|
||||
const dryRunField = form?.find(part => part.name === 'dryRun')?.data?.toString('utf8')
|
||||
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, filePart.data)
|
||||
await fs.writeFile(archivePath, fileBytes)
|
||||
return {
|
||||
archivePath,
|
||||
body: {
|
||||
@@ -105,7 +107,7 @@ async function resolveRestoreInput(event: H3Event, userId: number): Promise<{
|
||||
await fs.mkdir(getBackupsDir(), { recursive: true })
|
||||
const safeName = `upload-${Date.now()}${BACKUP_ARCHIVE_EXT}`
|
||||
const archivePath = join(getBackupsDir(), safeName)
|
||||
await fs.writeFile(archivePath, filePart.data)
|
||||
await fs.writeFile(archivePath, fileBytes)
|
||||
return {
|
||||
archivePath,
|
||||
body: {
|
||||
|
||||
Reference in new issue
Block a user