Files
appco_dist/.output/public/_nuxt/CfQT9Pjy.js
T
2026-01-27 16:02:45 +03:30

2 lines
2.2 KiB
JavaScript

const h={maxWidth:1200,maxHeight:1200,maxBytes:204800,startQuality:.82,minQuality:.5};function b(i){return(i||"image").replace(/\.[^.]+$/,"")}async function B(i,t,o){const e=document.createElement("canvas"),c=e.getContext("2d",{alpha:!0});let s=!1,a=0,r=0;if(typeof createImageBitmap=="function")try{const n=await createImageBitmap(i);a=n.width,r=n.height;const u=Math.min(1,t/a,o/r);e.width=Math.round(a*u),e.height=Math.round(r*u),c.drawImage(n,0,0,e.width,e.height),n.close?.(),s=!0}catch{}if(!s){const n=new Image;n.src=URL.createObjectURL(i),await n.decode(),a=n.naturalWidth,r=n.naturalHeight;const u=Math.min(1,t/a,o/r);e.width=Math.round(a*u),e.height=Math.round(r*u),c.drawImage(n,0,0,e.width,e.height),URL.revokeObjectURL(n.src)}return e}async function y(i,t,o,e=h.startQuality,c=h.minQuality){const s=u=>new Promise(p=>i.toBlob(p,t,u));let a=e,r=await s(a);if(!r)throw new Error("encode failed");let n=0;for(;r.size>o&&a>c&&n<12;)a-=.05,r=await s(a)??r,n++;return{blob:r,quality:a}}function Q(i,t){const o=(t||"").toLowerCase();return i===415||/invalid file type|unsupported/i.test(o)}async function x(i,t,o,e){const c=new FormData;c.append("file",t,o);const s=await fetch(i,{method:"POST",headers:{...e?{Authorization:`Bearer ${e}`}:{}},body:c}),a=await s.text();return{ok:s.ok,status:s.status,text:a}}async function j(i,t){try{if(!t?.endpoint)return{error:"uploadFile: endpoint is required"};const o=t.maxWidth??h.maxWidth,e=t.maxHeight??h.maxHeight,c=t.maxBytes??h.maxBytes,s=t.startQuality??h.startQuality,a=t.minQuality??h.minQuality,r=await B(i,o,e),n=b(i.name),u=await y(r,"image/webp",c,s,a),p=`${Date.now()}-${n}.webp`,d=await x(t.endpoint,u.blob,p,t.authToken);if(d.ok){let l;try{l=JSON.parse(d.text)}catch{l={}}const m=l?.path;return m?{data:{fullPath:m},error:null}:{error:"No path returned"}}if(Q(d.status,d.text)){const l=await y(r,"image/jpeg",c,s,a),m=`${Date.now()}-${n}.jpg`,w=await x(t.endpoint,l.blob,m,t.authToken);if(w.ok){let f;try{f=JSON.parse(w.text)}catch{f={}}const g=f?.path;return g?{data:{fullPath:g},error:null}:{error:"No path returned"}}return{error:w.text||`HTTP ${w.status}`}}return{error:d.text||`HTTP ${d.status}`}}catch(o){return{error:o?.message||o}}}export{j as u};