openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
3 lines • 4.21 kB
JavaScript
import{d as normalizeMimeType}from"./mime-ZmYZsEjx.mjs";import{a as getRemoteControlUiIngressContext,r as assertRemoteControlUiIngressCurrent}from"./ingress-attribution-Ct_hJMPU.mjs";import{r as respondNotFound}from"./control-ui-http-utils-M5A3tWWK.mjs";import{t as ARTIFACT_DOWNLOAD_PATH}from"./artifact-download-BzJIT0j4.mjs";import{d as retainArtifactDownloadStream,n as assertArtifactDownloadGrantCurrent,u as resolveArtifactDownloadGrant}from"./artifact-download-grants-D28hFNXi.mjs";import{d as sendMethodNotAllowed}from"./http-common-kI7-c_LU.mjs";import{n as normalizeChatImageRenditionWidth}from"./chat-image-renditions-CrJYtcId.mjs";import{t as buildAssistantMediaContentDisposition}from"./assistant-media-content-disposition-CJzSaLfG.mjs";import{i as writeByteHeaders,r as resolveByteResponse}from"./http-byte-range-CvNBag81.mjs";import{r as resolveManagedImageThumbnail,t as encodeImageThumbnail}from"./managed-image-thumbnail-cache-Btd6kKoi.mjs";import{t as ScratchFileExpiredError}from"./scratch-expiry-DdVFmxG5.mjs";import{createHash}from"node:crypto";import{pipeline}from"node:stream/promises";
//!src/gateway/artifact-downloads.ts
async function handleArtifactDownloadHttpRequest(e,t,n){const r=new URL(e.url??`/`,`http://localhost`);const i=n.basePath&&r.pathname.startsWith(`${n.basePath}${`/api/artifacts/download/`}`)?r.pathname.slice(n.basePath.length):r.pathname;if(!i.startsWith(`/api/artifacts/download/`))return false;if(e.method!==`GET`&&e.method!==`HEAD`){sendMethodNotAllowed(t,`GET, HEAD`);return true}const a=resolveArtifactDownloadGrant(n.clients,i.slice(ARTIFACT_DOWNLOAD_PATH.length));if(!a||a.grant.source.kind!==`bytes`){respondNotFound(t);return true}const{client:o,grant:s,ticket:c}=a;const l=a.grant.source;let assertGrantCurrent=()=>assertArtifactDownloadGrantCurrent(n.clients,o,c,s);const assertCurrent=()=>{const t=getRemoteControlUiIngressContext(e);assertRemoteControlUiIngressCurrent(t);if(t&&o.internal?.remoteIngressPrincipal?.assertCurrent!==t.resolvePrincipal().assertCurrent)throw new Error(`Artifact download belongs to another ingress grant`);assertGrantCurrent()};let u;const d=l.image&&r.searchParams.get(`variant`)===`thumbnail`;try{assertCurrent();u=await l.read({expectedDigest:l.digest,method:d?`GET`:e.method,headers:d?{}:{range:e.headers.range,"if-range":e.headers[`if-range`],"if-none-match":e.headers[`if-none-match`]}});assertCurrent();if(d){if(!u?.body||u.stream)throw new Error(`Artifact image preview bytes are unavailable`);const t=u.body;const n=normalizeChatImageRenditionWidth(r.searchParams.get(`width`));const i=`${createHash(`sha256`).update(t).digest(`hex`)}\0${n}`;const a=new Uint8Array(await resolveManagedImageThumbnail(i,()=>encodeImageThumbnail(t,n)));u.artifact={...u.artifact,mimeType:`image/webp`,title:`${u.artifact.title}-preview.webp`};const o=resolveByteResponse({file:{size:a.byteLength},method:e.method,request:e});u.response=o;u.body=e.method===`HEAD`||o.kind===`not-modified`||o.kind===`unsatisfiable`?void 0:o.kind===`partial`?a.subarray(o.range.start,o.range.end+1):a;assertCurrent()}}catch(e){if(e instanceof ScratchFileExpiredError){assertCurrent();t.writeHead(410,{"content-type":`text/plain; charset=utf-8`,"cache-control":`no-store`});t.end(e.message);return true}u=void 0}if(!u){respondNotFound(t);return true}const{artifact:f,response:p,body:m}=u;const h=normalizeMimeType(f.mimeType);const g=h&&/^[a-z0-9!#$&^_.+-]+\/[a-z0-9!#$&^_.+-]+$/i.test(h)?h:`application/octet-stream`;t.setHeader(`content-type`,g);t.setHeader(`content-disposition`,buildAssistantMediaContentDisposition(f.title));t.setHeader(`content-security-policy`,`default-src 'none'; sandbox`);t.setHeader(`x-content-type-options`,`nosniff`);t.setHeader(`referrer-policy`,`no-referrer`);t.setHeader(`cache-control`,`private, no-store`);writeByteHeaders(t,p);if(u.stream){const e=u.stream;let r;try{r=retainArtifactDownloadStream(n.clients,o,c,s);assertGrantCurrent=r.assertCurrent;await pipeline(async function*(t){const n=t.signal;for await(const t of e(n)){n.throwIfAborted();assertCurrent();yield t}},t,{signal:o.connectionSignal})}catch{t.destroy()}finally{r?.release()}}else t.end(m);return true}export{handleArtifactDownloadHttpRequest};