fix: large carts upload all their files (Marcus's bugs of 11/11, B1 to B4)

B1: an upload start was limited to 60 per 15 minutes per session, so a
66-file cart failed with 'Muitas tentativas'; now 1000 per session and
3000 per source, and the Site waits Retry-After and retries quietly.
B2: a failed file shows its error and 'Tentar de novo' on its own card, and
payment stays blocked with 'Falta enviar N arquivos'. B3: after signing in
again, an upload id the new session does not own (403) is dropped and the
file starts again. B4: the quote is never created before every file is
sent, and an untranslatable 422 is logged for Sentry.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Cauê Faleiros
2026-11-11 14:49:04 -03:00
parent 07515abbd8
commit 7a24c66997
7 changed files with 180 additions and 12 deletions

View File

@@ -4,9 +4,36 @@ window.dtfUpload = async (file, {api, prefix='/uploads', startPath=prefix, progr
const samples=new Blob([file.slice(0,65536),file.slice(Math.max(0,file.size-65536))]);
const hash=Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',await samples.arrayBuffer())),b=>b.toString(16).padStart(2,'0')).join('');
const key='dtf-upload:'+JSON.stringify([scope,file.name,file.size,file.lastModified,hash]);
// A busy server asks for a wait: retry this file alone, quietly, before the
// customer sees the failure (Marcus, 2026-11-11). Only a 429 that carries
// Retry-After; the quota refusal has none and is shown at once.
const retryWait=error=>{
const seconds=Number(error?.retryAfter);
return error?.status===429&&Number.isFinite(seconds)?Math.min(60,Math.max(1,Math.ceil(seconds))):null;
};
const withRetry=async call=>{
for(let attempt=0;;attempt++){
try{return await call();}
catch(error){
const wait=retryWait(error);
if(wait===null||attempt>=3)throw error;
await new Promise(resolve=>setTimeout(resolve,wait*1000));
}
}
};
let id=resume?localStorage.getItem(key):null,state;
if(id){try{state=await api(prefix+'/'+id);}catch(error){if(![404,410].includes(error.status))throw error;id=null;}}
if(!id){state=await api(startPath,{name:file.name,size:file.size});id=state.id;if(resume)localStorage.setItem(key,id);}
if(id){
try{state=await api(prefix+'/'+id);}
catch(error){
// The id was started under another session (a sign-in changes the scope):
// the new session does not own it, so this file starts again as a new
// upload. A 403/404/410 must never keep the retry on the old id.
if(![403,404,410].includes(error.status))throw error;
id=null;
if(resume)localStorage.removeItem(key);
}
}
if(!id){state=await withRetry(()=>api(startPath,{name:file.name,size:file.size}));id=state.id;if(resume)localStorage.setItem(key,id);}
async function waitForScan(){
onBytes(file.size);
// The antivirus streams the whole file: about a second per 2 MB, and at
@@ -42,7 +69,7 @@ window.dtfUpload = async (file, {api, prefix='/uploads', startPath=prefix, progr
if(done.has(part))continue;
if(signal?.aborted)throw cancelled();
progress('Enviando '+file.name+' · parte '+part+'/'+Math.ceil(file.size/size));
const signed=await api(prefix+'/'+id+'/parts/'+part,{});
const signed=await withRetry(()=>api(prefix+'/'+id+'/parts/'+part,{}));
await put(signed.url,file.slice(offset,offset+size));
done.add(part);sent+=length(part);
onBytes(sent);