✨ feat(frontend): 新增上传中断并联动历史回收状态

This commit is contained in:
orion committed 2026-09-18 16:53:40 +08:00
1 parent a962f7aec7
commit e7918e9317
20 files changed
+275 -71

No files matched your search

+8
View File
@@ -75,3 +75,11 @@ Playwright Page。Vite 固定运行于 `127.0.0.1:5180`;分别传入 `http`
`uploadCompletion.test.mjs` 覆盖 202 轮询、临时网络错误重试及明确失败退出;
`uploadStrategy.integration.test.mjs` 验证用户在丢失完成响应后手动重试时复用原上传 ID,
不重新上传分片。发送页在此阶段显示文件校验提示,确认成功后才给出取件码。
## 中断上传与历史状态
`uploadCancellation.integration.test.mjs` 验证中断当前请求、等待初始化返回会话后清理、
清理失败后重试及同文件重新上传。`cancelUpload.browser.mjs` 使用隔离 API 数据验证
发送页“中断上传”按钮和清理反馈,不会访问线上文件。
`historyStatus.test.mjs` 验证回收站原因共用标签、已清理、恢复后的现存状态。
分片使用原始请求体发送,最大 100 MiB,避免 multipart 额外开销超过 CDN 上限。
+18
View File
@@ -0,0 +1,18 @@
import assert from 'node:assert/strict'
export async function runCancelUploadCheck(page, base='http://127.0.0.1:5180/theme-assets/') {
let cleanup=0,completes=0,requests=0,startedResolve
const started=new Promise(resolve=>startedResolve=resolve)
await page.route('**/api/v1/config',r=>r.fulfill({json:{code:200,detail:{config:{name:'test',expireStyle:['day'],max_save_seconds:0,openUpload:1,enableChunk:1,uploadSize:1073741824}}}}))
await page.route('**/chunk/upload/init/**',r=>r.fulfill({json:{code:200,detail:{upload_id:'isolated-cancel',chunk_size:1048576,uploaded_chunks:[]}}}))
await page.route('**/chunk/upload/raw/**',async r=>{requests++;startedResolve();await new Promise(resolve=>setTimeout(resolve,1500));try{await r.fulfill({json:{code:200}})}catch{}})
await page.route('**/chunk/upload/complete/**',r=>{completes++;return r.fulfill({json:{code:200,detail:{code:'UNEXPECTED'}}})})
await page.route('**/chunk/upload/isolated-cancel',r=>{assert.equal(r.request().method(),'DELETE');cleanup++;return r.fulfill({json:{code:200,detail:{cancelled:true}}})})
await page.goto(base+'#/send')
await page.locator('input[type=file]').setInputFiles({name:'cancel.txt',mimeType:'text/plain',buffer:Buffer.alloc(3*1024*1024,97)})
await page.locator('button[type=submit]').click();await started
await page.getByRole('button',{name:'中断上传',exact:true}).click()
await page.getByText('上传已中断,临时分片已清理,可重新发送',{exact:true}).waitFor()
assert.equal(cleanup,1);assert.equal(requests,1);assert.equal(completes,0)
assert.equal(await page.locator('button[type=submit]').isEnabled(),true)
console.log('browser cancellation: stopped raw request, cleaned once, no subsequent chunks/publication')
}
+13
View File
@@ -0,0 +1,13 @@
import {test} from 'node:test'
import assert from 'node:assert/strict'
import {getHistoryStatusLabel} from '../src/utils/history-status.ts'
test('history labels follow trash and purge before expiry/current flags',()=>{
for(const [value,want] of [[{current:true},'现存'],[{status:'trashed',current:false,expired:true},'回收站中'],[{status:'purged',deleted:true},'已清理'],[{status:'active',current:true},'现存'],[{status:'expired',current:true},'现存'],[{deleted:true,current:false},'已删除'],[{current:false},'历史']])assert.equal(getHistoryStatusLabel(value),want)
})
test('restored history is distinct and recoverable reasons match trash labels',()=>{
assert.equal(getHistoryStatusLabel({status:'active',current:true,was_restored:true}),'现存,已从回收站恢复')
assert.equal(getHistoryStatusLabel({status:'trashed',trash_reason:'expired_count'}),'次数用尽')
assert.equal(getHistoryStatusLabel({status:'trashed',trash_reason:'expired_time'}),'保存到期')
assert.equal(getHistoryStatusLabel({status:'trashed',trash_reason:'manual'}),'手动删除')
})
@@ -0,0 +1,41 @@
import {test,before,after} from 'node:test'
import assert from 'node:assert/strict'
import {createServer} from 'vite'
let server,upload,service,Control
before(async()=>{
server=await createServer({server:{middlewareMode:true,watch:null,hmr:false},optimizeDeps:{noDiscovery:true,include:[]},appType:'custom'})
service=(await server.ssrLoadModule('/src/services/file.ts')).FileService
upload=(await server.ssrLoadModule('/src/services/upload-strategy.ts')).uploadChunkedFile
Control=(await server.ssrLoadModule('/src/services/upload-cancellation.ts')).UploadCancellation
globalThis.window={isSecureContext:true}
})
after(async()=>{await server?.close();delete globalThis.window})
test('cancel during chunk stops later chunks, waits for cleanup and does not publish',async()=>{
const original={init:service.initChunkUpload,chunk:service.uploadChunk,complete:service.completeChunkUpload,cancel:service.cancelChunkUpload};const control=new Control();let chunks=0,cleanup=0,completes=0
service.initChunkUpload=async()=>({code:200,detail:{upload_id:'cancel-id',chunk_size:4,uploaded_chunks:[]}})
service.uploadChunk=async(id,i,chunk,progress,signal)=>{chunks++;control.request();throw new DOMException('Aborted','AbortError')}
service.cancelChunkUpload=async id=>{assert.equal(id,'cancel-id');cleanup++;return {code:200,detail:{cancelled:true}}}
service.completeChunkUpload=async()=>{completes++;return {code:200}}
try{await assert.rejects(upload(new File(['abcdefghijkl'],'a.txt'),{expireValue:1,expireStyle:'day',cancellation:control}),/上传已中断/);assert.equal(chunks,1);assert.equal(cleanup,1);assert.equal(completes,0)}finally{Object.assign(service,{initChunkUpload:original.init,uploadChunk:original.chunk,completeChunkUpload:original.complete,cancelChunkUpload:original.cancel})}
})
test('cancel while initialization is pending cleans the returned id rather than leaking session',async()=>{
const original={init:service.initChunkUpload,cancel:service.cancelChunkUpload};const control=new Control();let cleaned=false
service.initChunkUpload=async()=>{control.request();return {code:200,detail:{upload_id:'late-id',chunk_size:4}}}
service.cancelChunkUpload=async id=>{cleaned=id==='late-id';return {code:200,detail:{cancelled:true}}}
try{await assert.rejects(upload(new File(['abcd'],'b.txt'),{expireValue:1,expireStyle:'day',cancellation:control}),/上传已中断/);assert.ok(cleaned)}finally{service.initChunkUpload=original.init;service.cancelChunkUpload=original.cancel}
})
test('failed cancellation cleanup is retryable and next submission initializes a new session',async()=>{
const original={init:service.initChunkUpload,chunk:service.uploadChunk,complete:service.completeChunkUpload,cancel:service.cancelChunkUpload}
const control=new Control();let inits=0,cancelCalls=0,completeCalls=0
service.initChunkUpload=async()=>({code:200,detail:{upload_id:'id-'+(++inits),chunk_size:4,uploaded_chunks:[]}})
service.uploadChunk=async()=>({code:200})
service.completeChunkUpload=async()=>{completeCalls++;if(completeCalls===1){control.request();throw new Error('aborted')}return {code:200,detail:{code:'NEWCODE1'}}}
service.cancelChunkUpload=async()=>{cancelCalls++;if(cancelCalls===1)throw new Error('cleanup unavailable');return {code:200,detail:{cancelled:true}}}
const file=new File(['abcd'],'retry.txt')
try{
await assert.rejects(upload(file,{expireValue:1,expireStyle:'day',cancellation:control}),/cleanup unavailable/)
assert.equal(control.needsCleanup,true);await control.clean();assert.equal(control.needsCleanup,false)
const done=await upload(file,{expireValue:1,expireStyle:'day',cancellation:new Control()})
assert.equal(done.detail.code,'NEWCODE1');assert.equal(inits,2)
}finally{Object.assign(service,{initChunkUpload:original.init,uploadChunk:original.chunk,completeChunkUpload:original.complete,cancelChunkUpload:original.cancel})}
})