UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 3.33 kB
import e from"../../libs/repositories/media.mjs";import t from"../../libs/repositories/processed-images.mjs";import{getMediaKeyTenantKey as n,resolveMediaKeyTenant as r}from"../../utils/media/media-key-tenant.mjs";import i from"../media/adjust-storage-usage.mjs";import a from"../media/checks/check-has-media-strategy.mjs";import o from"./checks/check-can-store.mjs";import s from"./get-single-count.mjs";import c from"./optimize-image.mjs";import{splitBodyForProcessing as l,toNodeReadable as u}from"../../libs/media/normalize-body.mjs";import{createBufferETag as d,matchesETag as f}from"../../utils/http/etag.mjs";import{PassThrough as p}from"node:stream";const m=async(m,h)=>{let g=await a(m);if(g.error)return g;let _=r(m.config,h.key),v=r(m.config,h.processKey),y=n(h.key),b=await g.data.stream(m,{key:h.key,tenant:_});if(b.error)return b;if(!b.data?.contentType?.startsWith(`image/`))return{error:void 0,data:{key:h.key,contentLength:b.data.contentLength,contentType:b.data.contentType,body:b.data.body,etag:b.data.etag}};let{processingBody:x,fallbackBody:S}=l(b.data.body),C=await new e(m.db.client,m.config.db).selectSingle({select:[`focal_x`,`focal_y`],where:[{key:`key`,operator:`=`,value:h.key}]});if(C.error)return C;let w=C.data?.focal_x!=null&&C.data.focal_y!=null?{x:C.data.focal_x/1e4,y:C.data.focal_y/1e4}:void 0,[T,E]=await Promise.all([c(m,{stream:u(x),options:{...h.options,focalPoint:w}}),s(m,{key:h.key})]);if(T.error||E.error||!T.data||!T.data.processed)return{error:void 0,data:{key:h.key,contentLength:b.data.contentLength,contentType:b.data.contentType,body:S,etag:b.data.etag}};let D=d(T.data.buffer);if(h.ifNoneMatch&&f(h.ifNoneMatch,D))return{error:void 0,data:{key:h.processKey,contentLength:void 0,contentType:T.data.mimeType,body:new Uint8Array,etag:D,notModified:!0}};let O=new p;if(O.end(T.data.buffer),!T.data.shouldStore||E.data>=m.config.media.limits.processedImagesPerFile||(await o(m,{size:T.data.size,tenantKey:y})).error)return{error:void 0,data:{key:h.processKey,contentLength:T.data.size,contentType:T.data.mimeType,body:O,etag:D}};let k=new t(m.db.client,m.config.db);if(m.config.media.images.storeProcessed===!0){let e=m.config.media.limits.storageBytes,t=await i(m,{tenantKey:y,delta:T.data.size,max:e===!1?void 0:e,min:0});if(t.error||!t.data.applied)return{error:void 0,data:{key:h.processKey,contentLength:T.data.size,contentType:T.data.mimeType,body:O,etag:D}};let[n,r]=await Promise.all([k.createSingle({data:{key:h.processKey,media_key:h.key,file_size:T.data.size}}),g.data.upload(m,{key:h.processKey,body:T.data.buffer,mimeType:T.data.mimeType,extension:T.data.extension,size:T.data.size,type:`image`,tenant:v})]);if(n.error!==void 0||r.error!==void 0)return await i(m,{tenantKey:y,delta:T.data.size*-1,min:0}),await Promise.allSettled([n.error===void 0?k.deleteSingle({where:[{key:`key`,operator:`=`,value:h.processKey}],returning:[`key`]}):Promise.resolve(),r.error===void 0?g.data.delete(m,{key:h.processKey,tenant:v}):Promise.resolve()]),{error:void 0,data:{key:h.processKey,contentLength:T.data.size,contentType:T.data.mimeType,body:O,etag:D}};r.error===void 0&&r.data?.etag&&(D=r.data.etag)}return{error:void 0,data:{key:h.processKey,contentLength:T.data.size,contentType:T.data.mimeType,body:O,etag:D}}};export{m as default}; //# sourceMappingURL=process-image.mjs.map