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 { readMultipartFormData } from 'h3'
|
||||
import { getHeader, readFormData } from 'h3'
|
||||
import { requireSiteSyncAuth } from '../../utils/access'
|
||||
import { createApiError } from '../../utils/api-error'
|
||||
import { MAX_VIDEO_FILE_SIZE } from '../../utils/constants'
|
||||
@@ -6,23 +6,27 @@ import { findUserByUsername } from '../../utils/db'
|
||||
import { validateStorageKey } from '../../utils/image-key'
|
||||
import { getActiveBackend } from '../../utils/storage/resolver'
|
||||
import { recordSiteSyncArtifact } from '../../utils/site-sync/artifacts'
|
||||
import { readFormDataFiles } from '../../utils/multipart-form'
|
||||
|
||||
function field(form: Awaited<ReturnType<typeof readMultipartFormData>>, name: string): string {
|
||||
const part = form?.find(item => item.name === name)
|
||||
return part?.data?.length ? new TextDecoder().decode(part.data).trim() : ''
|
||||
function field(form: FormData, name: string): string {
|
||||
const value = form.get(name)
|
||||
return typeof value === 'string' ? value.trim() : ''
|
||||
}
|
||||
|
||||
export default defineEventHandler(async (event) => {
|
||||
await requireSiteSyncAuth(event)
|
||||
const form = await readMultipartFormData(event)
|
||||
if (!form?.length) createApiError(event, 'INVALID_REQUEST', '未收到同步数据', 400)
|
||||
const contentLength = Number(getHeader(event, 'content-length') || 0)
|
||||
if (contentLength > MAX_VIDEO_FILE_SIZE + 1024 * 1024) {
|
||||
createApiError(event, 'FILE_TOO_LARGE', '同步文件超过 500 MB', 413)
|
||||
}
|
||||
const form = await readFormData(event)
|
||||
|
||||
const key = field(form, 'key')
|
||||
const file = form.find(part => part.name === 'file' && part.data?.length)
|
||||
if (!validateStorageKey(key) || !file?.data) {
|
||||
const file = readFormDataFiles(form, ['file'])[0]
|
||||
if (!validateStorageKey(key) || !file) {
|
||||
createApiError(event, 'INVALID_REQUEST', '同步文件参数无效', 400)
|
||||
}
|
||||
if (file.data.length > MAX_VIDEO_FILE_SIZE) {
|
||||
if (file.size > MAX_VIDEO_FILE_SIZE) {
|
||||
createApiError(event, 'FILE_TOO_LARGE', '同步文件超过 500 MB', 413)
|
||||
}
|
||||
|
||||
@@ -51,11 +55,11 @@ export default defineEventHandler(async (event) => {
|
||||
})
|
||||
}
|
||||
|
||||
await backend.put(key, new Uint8Array(file.data), {
|
||||
originalName: field(form, 'originalName') || file.filename || key.split('/').pop() || 'file',
|
||||
await backend.put(key, new Uint8Array(await file.arrayBuffer()), {
|
||||
originalName: field(form, 'originalName') || file.name || key.split('/').pop() || 'file',
|
||||
uploadedAt: field(form, 'uploadedAt') || new Date().toISOString(),
|
||||
contentType: file.type || field(form, 'contentType') || 'application/octet-stream',
|
||||
size: file.data.length,
|
||||
size: file.size,
|
||||
userId: owner?.id ?? null,
|
||||
mediaType: key.startsWith('videos/')
|
||||
? 'video'
|
||||
|
||||
Reference in new issue
Block a user