✨ feat(upload): 支持后台配置上传分片大小

This commit is contained in:
orion committed 2026-09-18 17:50:43 +08:00
1 parent 5b78ec6fbc
commit 272741ee49
11 files changed
+64 -5

No files matched your search

+12 -1
View File
@@ -3,7 +3,7 @@ import assert from 'node:assert/strict'
import {createServer} from 'vite'
let server,upload,service
before(async()=>{
server=await createServer({server:{middlewareMode:true,watch:null},appType:'custom'})
server=await createServer({optimizeDeps:{noDiscovery:true,include:[]},server:{middlewareMode:true,watch:null},appType:'custom'})
service=(await server.ssrLoadModule('/src/services/file.ts')).FileService
upload=(await server.ssrLoadModule('/src/services/upload-strategy.ts')).uploadChunkedFile
})
@@ -28,3 +28,14 @@ test('small file initialization never requests a chunk bigger than the file',asy
service.initChunkUpload=async request=>{size=request.chunk_size;return {code:200,detail:{existed:true,code:'existing'}}}
try{await upload(new File(['small'],'a.txt'),{expireValue:1,expireStyle:'day'});assert.equal(size,5)}finally{service.initChunkUpload=originalInit;globalThis.window=previousWindow}
})
test('configured chunk size determines new requests; server session size controls resumed slicing',async()=>{
const original={init:service.initChunkUpload,chunk:service.uploadChunk,complete:service.completeChunkUpload};const previousWindow=globalThis.window;globalThis.window={isSecureContext:true}
let requested;const slices=[]
service.initChunkUpload=async request=>{requested=request.chunk_size;return {code:200,detail:{upload_id:'configured',chunk_size:1048576,uploaded_chunks:[0]}}}
service.uploadChunk=async(id,index,blob)=>{slices.push([index,blob.size]);return {code:200}}
service.completeChunkUpload=async()=>({code:200,detail:{code:'CONFIG01'}})
try{
await upload(new File([new Uint8Array(3*1048576+1)],'configured.bin'),{expireValue:1,expireStyle:'day',chunkSizeMB:2})
assert.equal(requested,2*1048576);assert.deepEqual(slices,[[1,1048576],[2,1048576],[3,1]])
}finally{service.initChunkUpload=original.init;service.uploadChunk=original.chunk;service.completeChunkUpload=original.complete;globalThis.window=previousWindow}
})