diff --git a/tests/upload_retry_test.mjs b/tests/upload_retry_test.mjs index c0d6d4d..d9d436e 100644 --- a/tests/upload_retry_test.mjs +++ b/tests/upload_retry_test.mjs @@ -177,4 +177,22 @@ const run = async (completeSteps, {abortAfter} = {}) => { assert.equal(requested[0].options.method, 'DELETE'); } +// A file the phone can no longer read (Android NotReadableError, DTF-WEB-2A): +// the customer is told in Portuguese to pick it again, and nothing is sent. +{ + const RealBlob = globalThis.Blob; + globalThis.Blob = class extends RealBlob { + arrayBuffer() { return Promise.reject(new DOMException('The requested file could not be read', 'NotReadableError')); } + }; + try { + const {result, error, calls} = await run([{}]); + assert.equal(result, undefined); + assert.equal(error?.unreadable, true); + assert.match(error.message, /Escolha o arquivo de novo/); + assert.equal(calls.length, 0, 'nothing reaches the server'); + } finally { + globalThis.Blob = RealBlob; + } +} + console.log('upload retry checks passed'); diff --git a/web/checkout.js b/web/checkout.js index dad5182..58c44ab 100644 --- a/web/checkout.js +++ b/web/checkout.js @@ -288,7 +288,7 @@ const p=enviar(file); p.catch(()=>{}); return p; }, estado:file=>{const k=chaveArquivo(file),e=envios.get(k),f=falhas.get(k); - return f?{fase:'falhou',erro:f.error?.message||'erro no envio'} + return f?{fase:'falhou',erro:f.error?.message||'erro no envio',escolher:!!f.error?.unreadable} :!e?null:e.done?{fase:'pronto'}:e.sent>=file.size?{fase:'verificando'}:{fase:'enviando',enviado:e.sent};}, deNovo:file=>{falhas.delete(chaveArquivo(file));enviar(file).catch(()=>{});pintaEnvio();}, // A file already on the server (a saved sheet opened for editing): its id @@ -404,8 +404,12 @@ if (el) { el.replaceChildren(document.createTextNode('Não foi possível enviar '+falhou[0].file.name+': '+ (falhou[0].error?.message||'erro no envio').replace(/\.$/,'')+(falhou.length>1?' (e mais '+(falhou.length-1)+')':'')+'. ')); - const b=document.createElement('button'); b.type='button'; b.className='envioDeNovo'; b.textContent='Tentar de novo'; - b.onclick=tentaDeNovo; el.append(b); + // A file the phone cannot read again is picked again on its product + // page; a retry here would read the same dead file (DTF-WEB-2A). + if (!falhou[0].error?.unreadable) { + const b=document.createElement('button'); b.type='button'; b.className='envioDeNovo'; b.textContent='Tentar de novo'; + b.onclick=tentaDeNovo; el.append(b); + } el.classList.remove('ok'); el.classList.add('erro'); } if (topo) topo.textContent='· falha no envio'; diff --git a/web/site-upload.js b/web/site-upload.js index 4265a3d..66fa2e0 100644 --- a/web/site-upload.js +++ b/web/site-upload.js @@ -117,7 +117,7 @@ function pintaErrosDosArquivos(){ texto.textContent='Não foi possível enviar '+item.f.name+': '+ (estado.erro||'erro no envio').replace(/\.$/,'')+'.'; const button=document.createElement('button'); - button.type='button';button.textContent='Tentar de novo'; + button.type='button';button.textContent=estado.escolher?'Escolher de novo':'Tentar de novo'; button.dataset.envioCard=kind;button.dataset.envioIndex=String(index); box.append(texto,button);card.append(box); }); @@ -140,7 +140,8 @@ function pintaEnvios(){ return agora-enviadoEm.get(f)>ENVIADO_MS ? '' : '
'+icone('okCirculo')+''+nome+' enviado
'; } if(e.fase==='falhou') return '
Não foi possível enviar '+nome+': '+ - escapeHTML(e.erro.replace(/\.$/,''))+'.
'; + escapeHTML(e.erro.replace(/\.$/,''))+'.'; const pct=e.fase==='verificando' ? 100 : Math.floor(e.enviado/f.size*100); return '
'+(e.fase==='verificando' ? 'Verificando a segurança de '+nome+'…' : 'Enviando '+nome+' · '+pct+'% · '+fmt(e.enviado)+' de '+fmt(f.size))+''+ @@ -148,14 +149,25 @@ function pintaEnvios(){ }).join(''); } addEventListener('dtf-envio',agendaEnvios); +// A failed upload goes again; one the phone can no longer read (Android, +// DTF-WEB-2A) would fail the same way, so its file leaves the list and the +// picker opens for the customer to choose it again. +function refazEnvio(x, lista){ + if(!x) return; + if(!dtfEnvio.estado(x.f)?.escolher){ dtfEnvio.deNovo(x.f); return; } + const i=lista.indexOf(x); + if(i>=0){ lista.splice(i,1); lista===folhas?pintaFolha():pintaArtes(); } + $('inp').click(); +} $('envios').addEventListener('click',e=>{ const b=e.target.closest('[data-envio]'); if(!b || !MODOS[modo]) return; - const x=(ehFolha()?folhas:artes)[+b.dataset.envio]; if(x) dtfEnvio.deNovo(x.f); + const lista=ehFolha()?folhas:artes; + refazEnvio(lista[+b.dataset.envio], lista); }); $('lista').addEventListener('click',e=>{ const b=e.target.closest('[data-envio-card]'); if(!b || !window.dtfEnvio) return; - const x=(b.dataset.envioCard==='folha'?folhas:artes)[+b.dataset.envioIndex]; - if(x) dtfEnvio.deNovo(x.f); + const lista=b.dataset.envioCard==='folha'?folhas:artes; + refazEnvio(lista[+b.dataset.envioIndex], lista); }); // A sheet with residue gets its preview circled where the dots are. async function marcaResiduos(x){ diff --git a/web/upload.js b/web/upload.js index f294121..81d19be 100644 --- a/web/upload.js +++ b/web/upload.js @@ -15,7 +15,19 @@ function releaseUploadSlot(){ async function sendUpload(file, {api, prefix='/uploads', startPath=prefix, progress=()=>{}, onBytes=()=>{}, scope='guest', resume=true, signal=null}) { const cancelled=()=>new DOMException('Envio cancelado.','AbortError'); 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(''); + // A phone can lose the right to read a file it picked (Android: a file from + // Drive or Photos, after switching apps). The browser's message is English + // and a retry reads the same dead reference: picking the file again is the + // way out (Sentry DTF-WEB-2A). + let sampled; + try{ sampled=await samples.arrayBuffer(); } + catch(error){ + if(error?.name!=='NotReadableError' && error?.name!=='NotFoundError') throw error; + const unreadable=new Error('não conseguimos ler este arquivo no aparelho. Escolha o arquivo de novo'); + unreadable.unreadable=true; + throw unreadable; + } + const hash=Array.from(new Uint8Array(await crypto.subtle.digest('SHA-256',sampled)),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