feat: a real progress bar per file while it uploads (Dropstar's third round, item 6)
All checks were successful
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Integration suite on a real stack (push) Successful in 3m42s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 56s
All checks were successful
Build and deploy / Validate source (push) Successful in 6s
Build and deploy / Integration suite on a real stack (push) Successful in 3m42s
Build and deploy / Secret scan and release gate (push) Successful in 5s
Build and deploy / Publish images (push) Successful in 56s
A file starts uploading as soon as it is on the product page, not once the item is in the cart. Under the uploader, each file has its row: "Enviando NOME · 62% · 1,2 MB de 2,0 MB", then the security check, then "enviado"; a failure says why, with "Tentar de novo". Each part goes through XMLHttpRequest, so the bytes move while a part is on its way instead of once per 8 MB part. A file taken off the page, and in no cart item, stops uploading. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
/* Shared direct multipart transport for customer originals/corrections and operator finals. */
|
||||
window.dtfUpload = async (file, {api, prefix='/uploads', startPath=prefix, progress=()=>{}, onBytes=()=>{}, scope='guest', resume=true}) => {
|
||||
window.dtfUpload = async (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('');
|
||||
const key='dtf-upload:'+JSON.stringify([scope,file.name,file.size,file.lastModified,hash]);
|
||||
@@ -22,15 +23,29 @@ window.dtfUpload = async (file, {api, prefix='/uploads', startPath=prefix, progr
|
||||
}
|
||||
if(state.complete)return waitForScan();
|
||||
const done=new Set(state.parts||[]),size=state.part_bytes;
|
||||
onBytes(Math.min(file.size,done.size*size));
|
||||
const length=part=>Math.min(size,file.size-(part-1)*size);
|
||||
let sent=[...done].reduce((t,part)=>t+length(part),0);
|
||||
onBytes(sent);
|
||||
// Each part through XMLHttpRequest, which reports the bytes as they go: the
|
||||
// bar moves while a part is on its way, not once per part (Dropstar, 2026-10-29).
|
||||
const put=(url,body)=>new Promise((resolve,reject)=>{
|
||||
if(signal?.aborted)return reject(cancelled());
|
||||
const xhr=new XMLHttpRequest();xhr.open('PUT',url);
|
||||
xhr.upload.onprogress=e=>onBytes(Math.min(file.size,sent+e.loaded));
|
||||
const lost=()=>reject(new Error('Upload interrompido. Tente novamente para retomar.'));
|
||||
xhr.onload=()=>xhr.status>=200&&xhr.status<300?resolve():lost();
|
||||
xhr.onerror=lost;xhr.ontimeout=lost;xhr.onabort=()=>reject(cancelled());
|
||||
signal?.addEventListener('abort',()=>xhr.abort(),{once:true});
|
||||
xhr.send(body);
|
||||
});
|
||||
for(let offset=0,part=1;offset<file.size;offset+=size,part++){
|
||||
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 response=await fetch(signed.url,{method:'PUT',body:file.slice(offset,offset+size)});
|
||||
if(!response.ok)throw new Error('Upload interrompido. Tente novamente para retomar.');
|
||||
done.add(part);
|
||||
onBytes(Math.min(file.size,done.size*size));
|
||||
await put(signed.url,file.slice(offset,offset+size));
|
||||
done.add(part);sent+=length(part);
|
||||
onBytes(sent);
|
||||
}
|
||||
await api(prefix+'/'+id+'/complete',{});
|
||||
return waitForScan();
|
||||
|
||||
Reference in New Issue
Block a user