openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
1 lines • 928 B
JavaScript
(function(){async function e(e,t){let n=await createImageBitmap(e),r=new OffscreenCanvas(1,1);try{let i=r.getContext(`2d`);if(!i||n.width<1||n.height<1)throw Error(`Image cannot be resized`);let a=Math.min(1,4096/Math.max(n.width,n.height),Math.sqrt(t/e.size)*.95);for(;;){r.width=Math.max(1,Math.floor(n.width*a)),r.height=Math.max(1,Math.floor(n.height*a)),i.drawImage(n,0,0,r.width,r.height);let o=await r.convertToBlob({type:e.type});if(!o||o.size===0||o.type!==e.type)throw Error(`Image encoding failed`);if(o.size<=t)return new File([o],e.name,{type:o.type,lastModified:e.lastModified});if(r.width===1&&r.height===1)throw Error(`Image cannot fit the attachment limit`);a*=Math.min(.8,Math.sqrt(t/o.size)*.95)}}finally{n.close(),r.width=r.height=0}}globalThis.addEventListener(`message`,({data:t})=>{e(t.file,t.maxBytes).then(e=>globalThis.postMessage(e,{transfer:[]}),()=>globalThis.postMessage(null,{transfer:[]}))})})();