🐛 fix: 上传前拦截空文件并明确提示
* 普通选择和拖拽均拒绝零字节文件,清空无效选择及文件哈希 * 提交时复核单文件和打包结果,阻止分片或预签名初始化空文件 * 补充空文件入口及非空上传回归,记录生产分片代理限制修复
This commit is contained in:
commit
6a872fa8b0
4 files changed
+69
-3
No files matched your search
@@ -87,6 +87,12 @@ export function useSendFlow() {
|
||||
}
|
||||
|
||||
const checkFileSize = (file: File) => {
|
||||
if (file.size === 0) {
|
||||
alertStore.showAlert(t('send.messages.emptyFileError'), 'error')
|
||||
selectedFile.value = null
|
||||
fileHash.value = ''
|
||||
return false
|
||||
}
|
||||
if (file.size > config.value.uploadSize) {
|
||||
alertStore.showAlert(
|
||||
t('send.messages.fileSizeExceeded', { size: getStorageUnit(config.value.uploadSize) }),
|
||||
|
||||
@@ -111,9 +111,6 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||
if (selectedFiles.length > 0) {
|
||||
options.notify('正在打包文件...', 'success')
|
||||
fileToUpload = await packFilesAsZip(selectedFiles)
|
||||
if (!validateFileSize(fileToUpload)) {
|
||||
return null
|
||||
}
|
||||
options.onHashCalculated(await calculateFileHash(fileToUpload))
|
||||
}
|
||||
|
||||
@@ -121,6 +118,8 @@ export function useSendSubmit(options: UseSendSubmitOptions) {
|
||||
throw new Error(options.translate('send.messages.selectFile'))
|
||||
}
|
||||
|
||||
if (!validateFileSize(fileToUpload)) return null
|
||||
|
||||
operation.check()
|
||||
options.onProgress({ loaded:0,total:fileToUpload.size,percentage:0 })
|
||||
const result = await (enableChunk
|
||||
|
||||
@@ -143,3 +143,10 @@ Playwright Page。Vite 固定运行于 `127.0.0.1:5180`;分别传入 `http`
|
||||
可使用 `FCB_TEST_TOKEN` 或 `FCB_TEST_PASSWORD`;可选本地前端产物路径。
|
||||
- 当前前端单元/集成 75 项通过,架构/类型/构建通过;Node 覆盖率不覆盖 SSR 动态 Vue 模块,
|
||||
交互另经真实浏览器验证。此前自动随列表刷新的行为已撤销。
|
||||
|
||||
### 空文件上传校验(2026-10-06)
|
||||
|
||||
- `emptyUpload.integration.test.mjs` 验证选文件、拖拽和提交复核;分片/预签名初始化前均拒绝零字节文件。
|
||||
- 非空文件仍携带真实大小初始化,多文件沿用原有 ZIP 打包行为。
|
||||
- 生产 90 MiB 分片需要反向代理请求体上限不小于分片大小;本次 413 来自 OpenResty 全局 50 MiB 限制。
|
||||
FileCodeBox 站点与测试站统一设置 128 MiB,关闭请求缓冲,读写超时 600 秒。
|
||||
@@ -0,0 +1,54 @@
|
||||
import { test, before, after } from 'node:test'
|
||||
import assert from 'node:assert/strict'
|
||||
import { createServer } from 'vite'
|
||||
import { fileURLToPath } from 'node:url'
|
||||
import { createSSRApp, h } from 'vue'
|
||||
import { renderToString } from 'vue/server-renderer'
|
||||
import { createPinia } from 'pinia'
|
||||
import { createI18n } from 'vue-i18n'
|
||||
let server, createFlow, service, presign, alerts, config, zh, oldStorage, oldWindow
|
||||
before(async () => {
|
||||
oldStorage=globalThis.localStorage;oldWindow=globalThis.window
|
||||
globalThis.localStorage={getItem:()=>null,setItem(){},removeItem(){}}
|
||||
globalThis.window={isSecureContext:true,location:{origin:'https://example.test'}}
|
||||
server=await createServer({resolve:{alias:{'file-saver':fileURLToPath(new URL('./fixtures/browser-save.stub.mjs',import.meta.url))}},server:{middlewareMode:true,watch:null},appType:'custom'})
|
||||
createFlow=(await server.ssrLoadModule('/src/composables/useSendFlow.ts')).useSendFlow
|
||||
const exports=await server.ssrLoadModule('/src/services/index.ts');service=exports.FileService;presign=exports.PresignUploadService
|
||||
alerts=(await server.ssrLoadModule('/src/stores/alertStore.ts')).useAlertStore
|
||||
config=(await server.ssrLoadModule('/src/stores/configStore.ts')).useConfigStore
|
||||
zh=(await server.ssrLoadModule('/src/i18n/locales/zh-CN.ts')).default
|
||||
})
|
||||
after(async()=>{await server?.close();globalThis.localStorage=oldStorage;globalThis.window=oldWindow})
|
||||
async function flow(enableChunk=1) {
|
||||
let f,notifications=[]
|
||||
const app=createSSRApp({setup(){alerts().showAlert=(message,type)=>notifications.push({message,type});config().config.enableChunk=enableChunk;f=createFlow();return()=>h('div')}})
|
||||
app.use(createPinia());app.use(createI18n({legacy:false,locale:'zh-CN',messages:{'zh-CN':zh}}));await renderToString(app)
|
||||
return {f,notifications}
|
||||
}
|
||||
test('empty selection and drop are rejected with a friendly message and cleared file',async()=>{
|
||||
for(const kind of ['select','drop']){
|
||||
const {f,notifications}=await flow()
|
||||
const file=new File([],'BNApp64-v3.13.5.apk')
|
||||
if(kind==='select')await f.handleFileSelected(file);else await f.handleFileDrop({dataTransfer:{files:[file]}})
|
||||
assert.equal(f.selectedFile.value,null);assert.deepEqual(notifications,[{message:'无法读取空文件',type:'error'}])
|
||||
}
|
||||
})
|
||||
test('submit revalidates selected file before both chunk and presign initialization',async()=>{
|
||||
const originals={chunk:service.initChunkUpload,presign:presign.initUpload};let requests=0
|
||||
service.initChunkUpload=presign.initUpload=async()=>{requests++;throw Error('request must not be sent')}
|
||||
try{
|
||||
for(const enabled of [0,1]){
|
||||
const {f,notifications}=await flow(enabled);f.selectedFile.value=new File([],'empty.apk')
|
||||
await f.handleSubmit();assert.equal(requests,0);assert.equal(f.selectedFile.value,null);assert.equal(f.isSubmitting.value,false)
|
||||
assert.deepEqual(notifications,[{message:'无法读取空文件',type:'error'}])
|
||||
}
|
||||
}finally{service.initChunkUpload=originals.chunk;presign.initUpload=originals.presign}
|
||||
})
|
||||
test('nonempty file initialization still carries its actual size',async()=>{
|
||||
const {f}=await flow();const original=service.initChunkUpload;let request
|
||||
service.initChunkUpload=async data=>{request=data;throw Error('stop after initialization')}
|
||||
try{
|
||||
f.selectedFile.value=new File(['apk-data'],'valid.apk');await f.handleSubmit()
|
||||
assert.equal(request.file_size,8);assert.equal(request.chunk_size,8);assert.equal(f.isSubmitting.value,false)
|
||||
}finally{service.initChunkUpload=original}
|
||||
})
|
||||
Reference in new issue
Block a user