@murumets-ee/media 0.2.1 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/dist/admin.d.mts.map +1 -1
  2. package/dist/admin.mjs +1 -1
  3. package/dist/admin.mjs.map +1 -1
  4. package/dist/client-B__UIAjI.mjs +2 -0
  5. package/dist/client-B__UIAjI.mjs.map +1 -0
  6. package/dist/client.d.mts +12 -12
  7. package/dist/client.d.mts.map +1 -1
  8. package/dist/client.mjs +1 -1
  9. package/dist/client.mjs.map +1 -1
  10. package/dist/entity-TVTU7wS3.mjs +2 -0
  11. package/dist/{entity-D5P2l05s.mjs.map → entity-TVTU7wS3.mjs.map} +1 -1
  12. package/dist/image-styles-settings.d.mts +1 -1
  13. package/dist/image-styles.d.mts.map +1 -1
  14. package/dist/index.d.mts +1 -61
  15. package/dist/index.d.mts.map +1 -1
  16. package/dist/index.mjs +1 -1
  17. package/dist/plugin.d.mts +1 -1
  18. package/dist/plugin.mjs +1 -1
  19. package/dist/processing.d.mts +65 -0
  20. package/dist/processing.d.mts.map +1 -0
  21. package/dist/processing.mjs +1 -0
  22. package/dist/query-client.d.mts +4 -9
  23. package/dist/query-client.d.mts.map +1 -1
  24. package/dist/query-client.mjs +1 -1
  25. package/dist/query-client.mjs.map +1 -1
  26. package/dist/regenerate-variants-BFnFcnxm.mjs +2 -0
  27. package/dist/regenerate-variants-BFnFcnxm.mjs.map +1 -0
  28. package/dist/{types-BV_pOm23.d.mts → types-BBAbJooM.d.mts} +71 -19
  29. package/dist/types-BBAbJooM.d.mts.map +1 -0
  30. package/dist/variant-key-gVMhzKyv.mjs +2 -0
  31. package/dist/variant-key-gVMhzKyv.mjs.map +1 -0
  32. package/package.json +12 -8
  33. package/dist/client-DNTvmvYS.mjs +0 -2
  34. package/dist/client-DNTvmvYS.mjs.map +0 -1
  35. package/dist/entity-D5P2l05s.mjs +0 -2
  36. package/dist/regenerate-variants-DY7D4Ky3.mjs +0 -2
  37. package/dist/regenerate-variants-DY7D4Ky3.mjs.map +0 -1
  38. package/dist/types-BV_pOm23.d.mts.map +0 -1
@@ -1 +1 @@
1
- {"version":3,"file":"admin.d.mts","names":[],"sources":["../src/admin/routes.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;iBAwbgB,WAAA,CAAA,GAAe,UAAA"}
1
+ {"version":3,"file":"admin.d.mts","names":[],"sources":["../src/admin/routes.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;iBAqdgB,WAAA,CAAA,GAAe,UAAA"}
package/dist/admin.mjs CHANGED
@@ -1,2 +1,2 @@
1
- const e=[`cover`,`contain`,`inside`,`outside`,`fill`],t=[`webp`,`jpeg`,`png`,`avif`];function n(n){for(let[r,i]of Object.entries(n)){if(typeof r!=`string`||r.length===0||!/^[a-z][a-z0-9_-]*$/.test(r))return{valid:!1,error:`Invalid style name "${r}": must be lowercase alphanumeric (a-z, 0-9, -, _)`};if(!i||typeof i!=`object`)return{valid:!1,error:`Style "${r}" must be an object`};let n=i;if(n.width!==void 0&&(typeof n.width!=`number`||n.width<=0))return{valid:!1,error:`Invalid width for style "${r}": must be a positive number`};if(n.height!==void 0&&(typeof n.height!=`number`||n.height<=0))return{valid:!1,error:`Invalid height for style "${r}": must be a positive number`};if(n.width===void 0&&n.height===void 0)return{valid:!1,error:`Style "${r}" must have at least width or height`};if(n.quality!==void 0&&(typeof n.quality!=`number`||n.quality<1||n.quality>100))return{valid:!1,error:`Invalid quality for style "${r}": must be 1-100`};if(n.fit!==void 0&&!e.includes(n.fit))return{valid:!1,error:`Invalid fit for style "${r}": must be one of ${e.join(`, `)}`};if(n.format!==void 0&&!t.includes(n.format))return{valid:!1,error:`Invalid format for style "${r}": must be one of ${t.join(`, `)}`}}return{valid:!0,styles:n}}const r=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function i(e){return r.test(e)}let a=null;function o(){return a||=(async()=>{let{getApp:e}=await import(`@murumets-ee/core`),{createStorageClient:t}=await import(`@murumets-ee/storage`),{getStorageConfig:n}=await import(`@murumets-ee/storage/plugin`),{MediaClient:r}=await import(`./client-DNTvmvYS.mjs`),i=e(),a=t(n(),{app:i});return new r({db:i.db.readWrite,storage:a,logger:i.logger.child({media:!0})})})(),a}function s(e,t=200){return new Response(JSON.stringify(e),{status:t,headers:{"Content-Type":`application/json`}})}function c(e,t){return s({error:e},t)}async function l(e,{segments:t}){let n=await o();if(t.length===1&&t[0]===`settings`){let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{getApp:n}=await import(`@murumets-ee/core`);return s({imageStyles:await e(t,{app:n()}).get(`imageStyles`)??{}})}if(t.length===2&&t[1]===`usage`){let e=t[0];if(!i(e))return c(`Invalid media ID format`,400);let{findMediaUsages:n}=await import(`./usage-D7Bn7Vvv.mjs`),{getApp:r}=await import(`@murumets-ee/core`);return s({usages:await n(e,r().db.readWrite)})}if(t.length>0){let e=t[0],r=await n.findById(e);if(!r)return c(`Media not found`,404);let i=await n.getUrl(e);return s({...r,url:i})}let r=new URL(e.url),a=r.searchParams.get(`search`)??void 0,l=r.searchParams.get(`mediaType`)??void 0,u=Math.min(Math.max(Number(r.searchParams.get(`limit`))||24,1),100),d=Math.max(Number(r.searchParams.get(`offset`))||0,0),f=await n.findMany({search:a,mediaType:l,limit:u,offset:d}),p=f.items.map(e=>e.id),[m,h]=await Promise.all([n.getUrls(p),n.getVariantUrls(p,`thumbnail`)]);return s({items:f.items.map(e=>({id:e.id,title:e.title,alt:e.alt,filename:e.filename,mimeType:e.mimeType,size:e.size,mediaType:e.mediaType,url:m.get(e.id)??``,thumbnailUrl:h.get(e.id),width:e.width,height:e.height})),total:f.total})}async function u(e,{segments:t,user:r,audit:i,checkPermission:a}){if(t.length===1&&t[0]===`regenerate-variants`){if(!a(`media`,`update`))return c(`Forbidden: media update permission required for variant regeneration`,403);let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{regenerateAllVariants:n}=await import(`./regenerate-variants-DY7D4Ky3.mjs`),{getApp:o}=await import(`@murumets-ee/core`),{createStorageClient:l}=await import(`@murumets-ee/storage`),{getStorageConfig:u}=await import(`@murumets-ee/storage/plugin`),d=o(),f=await e(t,{app:d}).get(`imageStyles`);if(!f||Object.keys(f).length===0)return c(`No image styles configured`,400);let p=l(u(),{app:d}),m=await n({db:d.db.readWrite,storage:p,logger:d.logger.child({media:!0}),styles:f});return i?.({action:`media.regenerate_variants`,userId:r.id,userName:r.name,metadata:{total:m.total,processed:m.processed,errors:m.errors}}),s(m)}if(t.length===1&&t[0]===`settings`){if(!a(`media`,`update`))return c(`Forbidden: media update permission required for image style management`,403);let t=await e.json();if(!t.imageStyles||typeof t.imageStyles!=`object`)return c(`Body must contain "imageStyles" object`,400);let l=n(t.imageStyles);if(!l.valid)return c(l.error,400);let{createSettingsClient:u}=await import(`@murumets-ee/settings`),{imageStylesSettings:d}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{getApp:f}=await import(`@murumets-ee/core`);return await u(d,{app:f()}).set(`imageStyles`,l.styles),(await o()).invalidateImageStylesCache(),i?.({action:`media.settings.update`,entityType:`settings`,userId:r.id,userName:r.name,changes:{imageStyles:l.styles}}),s({imageStyles:l.styles})}let l=await o(),u=(await e.formData()).get(`file`);if(!u||u.size===0)return c(`No file provided`,400);if(u.size>50*1024*1024)return c(`File too large: ${(u.size/1024/1024).toFixed(1)} MB exceeds 50 MB limit`,400);let d=Buffer.from(await u.arrayBuffer()),{detectMimeType:f}=await import(`@murumets-ee/storage`),{mimeType:p,mismatch:m}=await f(d,u.type||`application/octet-stream`);if(m)return c(`File content doesn't match declared type: claimed ${u.type}, detected ${p}`,400);let h=await l.upload(d,{filename:u.name,mimeType:p,size:u.size,uploadedBy:r.id}),g={id:h.media.id,title:h.media.title,alt:h.media.alt,filename:h.media.filename,mimeType:h.media.mimeType,size:h.media.size,mediaType:h.media.mediaType,url:h.url,width:h.media.width,height:h.media.height};return i?.({action:`media.upload`,entityType:`media`,entityId:h.media.id,userId:r.id,userName:r.name,changes:{filename:h.media.filename,mimeType:h.media.mimeType,size:h.media.size,mediaType:h.media.mediaType}}),s(g,201)}async function d(e,{segments:t,user:n,audit:r}){if(t.length===0)return c(`Media ID required`,400);let a=t[0];return i(a)?(await(await o()).delete(a),r?.({action:`media.delete`,entityType:`media`,entityId:a,userId:n.id,userName:n.name}),s({deleted:1})):c(`Invalid media ID format`,400)}function f(){return{prefix:`media`,resource:`media`,actions:[`view`,`create`,`update`,`delete`],handlers:{GET:l,POST:u,DELETE:d}}}export{f as mediaRoutes};
1
+ const e=[`cover`,`contain`,`inside`,`outside`,`fill`],t=[`webp`,`jpeg`,`png`,`avif`];function n(n){for(let[r,i]of Object.entries(n)){if(typeof r!=`string`||r.length===0||!/^[a-z][a-z0-9_-]*$/.test(r))return{valid:!1,error:`Invalid style name "${r}": must be lowercase alphanumeric (a-z, 0-9, -, _)`};if(!i||typeof i!=`object`)return{valid:!1,error:`Style "${r}" must be an object`};let n=i;if(n.width!==void 0&&(typeof n.width!=`number`||n.width<=0))return{valid:!1,error:`Invalid width for style "${r}": must be a positive number`};if(n.height!==void 0&&(typeof n.height!=`number`||n.height<=0))return{valid:!1,error:`Invalid height for style "${r}": must be a positive number`};if(n.width===void 0&&n.height===void 0)return{valid:!1,error:`Style "${r}" must have at least width or height`};if(n.quality!==void 0&&(typeof n.quality!=`number`||n.quality<1||n.quality>100))return{valid:!1,error:`Invalid quality for style "${r}": must be 1-100`};if(n.fit!==void 0&&!e.includes(n.fit))return{valid:!1,error:`Invalid fit for style "${r}": must be one of ${e.join(`, `)}`};if(n.format!==void 0&&!t.includes(n.format))return{valid:!1,error:`Invalid format for style "${r}": must be one of ${t.join(`, `)}`}}return{valid:!0,styles:n}}const r=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function i(e){return r.test(e)}let a=null;async function o(){return a||=(async()=>{let{getApp:e}=await import(`@murumets-ee/core`),{createStorageClient:t}=await import(`@murumets-ee/storage`),{getStorageConfig:n}=await import(`@murumets-ee/storage/plugin`),r=e();return t(n(),{app:r})})(),a}async function s(){let{createAdminClient:e}=await import(`@murumets-ee/core/clients`),{MediaClient:t}=await import(`./client-B__UIAjI.mjs`),{Media:n}=await import(`./entity-DZFku8b7.mjs`),r=await o();return new t({admin:e(n),storage:r})}function c(e,t=200){return new Response(JSON.stringify(e),{status:t,headers:{"Content-Type":`application/json`}})}function l(e,t){return c({error:e},t)}async function u(e,{segments:t}){let n=await s();if(t.length===1&&t[0]===`settings`){let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{getApp:n}=await import(`@murumets-ee/core`);return c({imageStyles:await e(t,{app:n()}).get(`imageStyles`)??{}})}if(t.length===2&&t[1]===`usage`){let e=t[0];if(!i(e))return l(`Invalid media ID format`,400);let{findMediaUsages:n}=await import(`./usage-D7Bn7Vvv.mjs`),{getApp:r}=await import(`@murumets-ee/core`);return c({usages:await n(e,r().db.readWrite)})}if(t.length>0){let e=t[0],r=await n.findById(e);if(!r)return l(`Media not found`,404);let i=await n.getUrl(e);return c({...r,url:i})}let r=new URL(e.url),a=r.searchParams.get(`search`)??void 0,o=r.searchParams.get(`mediaType`)??void 0,u=Math.min(Math.max(Number(r.searchParams.get(`limit`))||24,1),100),d=Math.max(Number(r.searchParams.get(`offset`))||0,0),f=await n.findMany({search:a,mediaType:o,limit:u,offset:d}),p=f.items.map(e=>e.id),[m,h]=await Promise.all([n.getUrls(p),n.getVariantUrls(p,`thumbnail`)]);return c({items:f.items.map(e=>({id:e.id,title:e.title??null,alt:e.alt??null,filename:e.filename,mimeType:e.mimeType,size:e.size,mediaType:e.mediaType,url:m.get(e.id)??``,thumbnailUrl:h.get(e.id),width:e.width??null,height:e.height??null})),total:f.total})}async function d(e,{segments:t,user:r,audit:i,checkPermission:a}){if(t.length===1&&t[0]===`regenerate-variants`){if(!a(`media`,`create`))return l(`Forbidden: media create permission required for variant regeneration`,403);let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{regenerateAllVariants:n}=await import(`./regenerate-variants-BFnFcnxm.mjs`),{getApp:o}=await import(`@murumets-ee/core`),{createStorageClient:s}=await import(`@murumets-ee/storage`),{getStorageConfig:u}=await import(`@murumets-ee/storage/plugin`),d=o(),f=await e(t,{app:d}).get(`imageStyles`);if(!f||Object.keys(f).length===0)return l(`No image styles configured`,400);let p=s(u(),{app:d}),m=await n({db:d.db.readWrite,storage:p,logger:d.logger.child({media:!0}),styles:f});return i?.({action:`media.regenerate_variants`,userId:r.id,userName:r.name,metadata:{total:m.total,processed:m.processed,errors:m.errors}}),c(m)}if(t.length===1&&t[0]===`settings`){if(!a(`media`,`create`))return l(`Forbidden: media create permission required for image style management`,403);let t=await e.json();if(!t.imageStyles||typeof t.imageStyles!=`object`)return l(`Body must contain "imageStyles" object`,400);let o=n(t.imageStyles);if(!o.valid)return l(o.error,400);let{createSettingsClient:u}=await import(`@murumets-ee/settings`),{imageStylesSettings:d}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{getApp:f}=await import(`@murumets-ee/core`);return await u(d,{app:f()}).set(`imageStyles`,o.styles),(await s()).invalidateImageStylesCache(),i?.({action:`media.settings.update`,entityType:`settings`,userId:r.id,userName:r.name,changes:{imageStyles:o.styles}}),c({imageStyles:o.styles})}if(!a(`media`,`create`))return l(`Forbidden: media create permission required for upload`,403);let o=await s(),u=(await e.formData()).get(`file`);if(!u||u.size===0)return l(`No file provided`,400);if(u.size>50*1024*1024)return l(`File too large: ${(u.size/1024/1024).toFixed(1)} MB exceeds 50 MB limit`,400);let d=Buffer.from(await u.arrayBuffer()),{detectMimeType:f}=await import(`@murumets-ee/storage`),{mimeType:p,mismatch:m}=await f(d,u.type||`application/octet-stream`);if(m)return l(`File content doesn't match declared type: claimed ${u.type}, detected ${p}`,400);let h=await o.upload(d,{filename:u.name,mimeType:p,size:u.size,uploadedBy:r.id}),g={id:h.media.id,title:h.media.title??null,alt:h.media.alt??null,filename:h.media.filename,mimeType:h.media.mimeType,size:h.media.size,mediaType:h.media.mediaType,url:h.url,width:h.media.width??null,height:h.media.height??null};return i?.({action:`media.upload`,entityType:`media`,entityId:h.media.id,userId:r.id,userName:r.name,changes:{filename:h.media.filename,mimeType:h.media.mimeType,size:h.media.size,mediaType:h.media.mediaType}}),c(g,201)}async function f(e,{segments:t,user:n,audit:r,checkPermission:a}){if(!a(`media`,`delete`))return l(`Forbidden: media delete permission required`,403);if(t.length===0)return l(`Media ID required`,400);let o=t[0];return i(o)?(await(await s()).delete(o),r?.({action:`media.delete`,entityType:`media`,entityId:o,userId:n.id,userName:n.name}),c({deleted:1})):l(`Invalid media ID format`,400)}function p(){return{prefix:`media`,resource:`media`,actions:[`view`,`create`,`update`,`delete`],handlers:{GET:u,POST:d,DELETE:f}}}export{p as mediaRoutes};
2
2
  //# sourceMappingURL=admin.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"admin.mjs","names":[],"sources":["../src/admin/routes.ts"],"sourcesContent":["/**\n * Media admin routes for the centralized admin API handler.\n *\n * Provides media-specific operations: upload, URL resolution, delete with storage cleanup,\n * image style settings, and variant regeneration.\n *\n * Standard entity CRUD (PATCH with translations) falls through to the generic entity\n * handler — add `Media` to the `entities` array in your handler config.\n *\n * @example\n * ```typescript\n * import { createAdminApiHandler } from '@murumets-ee/admin-ui/server'\n * import { mediaRoutes } from '@murumets-ee/media/admin'\n * import { Media } from '@murumets-ee/media'\n *\n * const handler = createAdminApiHandler({\n * authenticate: async (req) => { ... },\n * entities: [Article, Media],\n * routes: [mediaRoutes()],\n * })\n * ```\n */\n\nimport type { AdminRoute, AuditLogFn, AuthUser } from '@murumets-ee/core'\nimport type { MediaClient } from '../client.js'\nimport type { MediaPickerItem, MediaPickerListResult } from '../picker/types.js'\nimport type { ImageStyle } from '../types.js'\n\n// ---------------------------------------------------------------------------\n// Image style validation\n// ---------------------------------------------------------------------------\n\nconst VALID_FIT = ['cover', 'contain', 'inside', 'outside', 'fill'] as const\nconst VALID_FORMAT = ['webp', 'jpeg', 'png', 'avif'] as const\n\nfunction validateImageStyles(\n styles: Record<string, unknown>,\n): { valid: true; styles: Record<string, ImageStyle> } | { valid: false; error: string } {\n for (const [name, raw] of Object.entries(styles)) {\n if (typeof name !== 'string' || name.length === 0 || !/^[a-z][a-z0-9_-]*$/.test(name)) {\n return {\n valid: false,\n error: `Invalid style name \"${name}\": must be lowercase alphanumeric (a-z, 0-9, -, _)`,\n }\n }\n if (!raw || typeof raw !== 'object') {\n return { valid: false, error: `Style \"${name}\" must be an object` }\n }\n const style = raw as Record<string, unknown>\n if (style.width !== undefined && (typeof style.width !== 'number' || style.width <= 0)) {\n return { valid: false, error: `Invalid width for style \"${name}\": must be a positive number` }\n }\n if (style.height !== undefined && (typeof style.height !== 'number' || style.height <= 0)) {\n return {\n valid: false,\n error: `Invalid height for style \"${name}\": must be a positive number`,\n }\n }\n if (style.width === undefined && style.height === undefined) {\n return { valid: false, error: `Style \"${name}\" must have at least width or height` }\n }\n if (\n style.quality !== undefined &&\n (typeof style.quality !== 'number' || style.quality < 1 || style.quality > 100)\n ) {\n return { valid: false, error: `Invalid quality for style \"${name}\": must be 1-100` }\n }\n if (\n style.fit !== undefined &&\n !(VALID_FIT as readonly string[]).includes(style.fit as string)\n ) {\n return {\n valid: false,\n error: `Invalid fit for style \"${name}\": must be one of ${VALID_FIT.join(', ')}`,\n }\n }\n if (\n style.format !== undefined &&\n !(VALID_FORMAT as readonly string[]).includes(style.format as string)\n ) {\n return {\n valid: false,\n error: `Invalid format for style \"${name}\": must be one of ${VALID_FORMAT.join(', ')}`,\n }\n }\n }\n return { valid: true, styles: styles as Record<string, ImageStyle> }\n}\n\n// ---------------------------------------------------------------------------\n// Validation\n// ---------------------------------------------------------------------------\n\nconst UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i\n\nfunction isValidUuid(value: string): boolean {\n return UUID_RE.test(value)\n}\n\n// ---------------------------------------------------------------------------\n// Lazy client initialization\n// ---------------------------------------------------------------------------\n\nlet clientPromise: Promise<MediaClient> | null = null\n\nfunction getClient(): Promise<MediaClient> {\n if (!clientPromise) {\n clientPromise = (async () => {\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n const { MediaClient } = await import('../client.js')\n\n const app = getApp()\n const storageConfig = getStorageConfig()\n const storage = createStorageClient(storageConfig, { app })\n\n return new MediaClient({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n })\n })()\n }\n return clientPromise\n}\n\n// ---------------------------------------------------------------------------\n// Response helpers\n// ---------------------------------------------------------------------------\n\nfunction json(data: unknown, status = 200) {\n return new Response(JSON.stringify(data), {\n status,\n headers: { 'Content-Type': 'application/json' },\n })\n}\n\nfunction errorJson(message: string, status: number) {\n return json({ error: message }, status)\n}\n\n// ---------------------------------------------------------------------------\n// Handlers\n// ---------------------------------------------------------------------------\n\nasync function handleGet(\n req: Request,\n { segments }: { segments: string[]; user: AuthUser; audit?: AuditLogFn },\n): Promise<Response> {\n const client = await getClient()\n\n // GET /media/settings — return current image styles\n if (segments.length === 1 && segments[0] === 'settings') {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('../image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const styles = await settingsClient.get('imageStyles')\n return json({ imageStyles: styles ?? {} })\n }\n\n // GET /media/:id/usage — find all entities referencing this media\n if (segments.length === 2 && segments[1] === 'usage') {\n const id = segments[0]\n if (!isValidUuid(id)) return errorJson('Invalid media ID format', 400)\n\n const { findMediaUsages } = await import('../usage.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const usages = await findMediaUsages(id, app.db.readWrite)\n return json({ usages })\n }\n\n // GET /media/:id — single item with URL (full record for EntityForm + picker)\n if (segments.length > 0) {\n const id = segments[0]\n const record = await client.findById(id)\n if (!record) return errorJson('Media not found', 404)\n\n const url = await client.getUrl(id)\n return json({ ...record, url })\n }\n\n // GET /media — list with search/filter + batch URL resolution\n const url = new URL(req.url)\n const search = url.searchParams.get('search') ?? undefined\n const mediaType = url.searchParams.get('mediaType') ?? undefined\n const limit = Math.min(Math.max(Number(url.searchParams.get('limit')) || 24, 1), 100)\n const offset = Math.max(Number(url.searchParams.get('offset')) || 0, 0)\n\n const result = await client.findMany({\n search,\n mediaType: mediaType as 'image' | 'video' | 'audio' | 'document' | 'other' | undefined,\n limit,\n offset,\n })\n\n // Resolve original URLs + thumbnail variant URLs for all items\n const ids = result.items.map((item) => item.id)\n const [urlMap, thumbMap] = await Promise.all([\n client.getUrls(ids),\n client.getVariantUrls(ids, 'thumbnail'),\n ])\n\n const items: (MediaPickerItem & { thumbnailUrl?: string })[] = result.items.map((item) => ({\n id: item.id,\n title: item.title,\n alt: item.alt,\n filename: item.filename,\n mimeType: item.mimeType,\n size: item.size,\n mediaType: item.mediaType,\n url: urlMap.get(item.id) ?? '',\n thumbnailUrl: thumbMap.get(item.id),\n width: item.width,\n height: item.height,\n }))\n\n const response: MediaPickerListResult = { items, total: result.total }\n return json(response)\n}\n\nasync function handlePost(\n req: Request,\n {\n segments,\n user,\n audit,\n checkPermission,\n }: {\n segments: string[]\n user: AuthUser\n audit?: AuditLogFn\n checkPermission: (resource: string, action: string) => boolean\n },\n): Promise<Response> {\n // POST /media/regenerate-variants — requires media update permission\n if (segments.length === 1 && segments[0] === 'regenerate-variants') {\n if (!checkPermission('media', 'update')) {\n return errorJson('Forbidden: media update permission required for variant regeneration', 403)\n }\n\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('../image-styles-settings.js')\n const { regenerateAllVariants } = await import('../regenerate-variants.js')\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const styles = await settingsClient.get('imageStyles')\n if (!styles || Object.keys(styles).length === 0) {\n return errorJson('No image styles configured', 400)\n }\n\n const storageConfig = getStorageConfig()\n const storage = createStorageClient(storageConfig, { app })\n\n const result = await regenerateAllVariants({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n styles,\n })\n\n audit?.({\n action: 'media.regenerate_variants',\n userId: user.id,\n userName: user.name,\n metadata: { total: result.total, processed: result.processed, errors: result.errors },\n })\n\n return json(result)\n }\n\n // POST /media/settings — requires media update permission\n if (segments.length === 1 && segments[0] === 'settings') {\n if (!checkPermission('media', 'update')) {\n return errorJson(\n 'Forbidden: media update permission required for image style management',\n 403,\n )\n }\n\n const body = (await req.json()) as { imageStyles?: Record<string, unknown> }\n if (!body.imageStyles || typeof body.imageStyles !== 'object') {\n return errorJson('Body must contain \"imageStyles\" object', 400)\n }\n\n const validation = validateImageStyles(body.imageStyles)\n if (!validation.valid) {\n return errorJson(validation.error, 400)\n }\n\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('../image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n await settingsClient.set('imageStyles', validation.styles)\n\n // Invalidate cached styles on the MediaClient singleton\n const client = await getClient()\n client.invalidateImageStylesCache()\n\n audit?.({\n action: 'media.settings.update',\n entityType: 'settings',\n userId: user.id,\n userName: user.name,\n changes: { imageStyles: validation.styles },\n })\n\n return json({ imageStyles: validation.styles })\n }\n\n // POST /media — upload file\n const client = await getClient()\n\n const formData = await req.formData()\n const file = formData.get('file') as File | null\n if (!file || file.size === 0) {\n return errorJson('No file provided', 400)\n }\n\n // Guard against oversized uploads (50 MB limit)\n const MAX_UPLOAD_SIZE = 50 * 1024 * 1024\n if (file.size > MAX_UPLOAD_SIZE) {\n return errorJson(\n `File too large: ${(file.size / 1024 / 1024).toFixed(1)} MB exceeds 50 MB limit`,\n 400,\n )\n }\n\n const buffer = Buffer.from(await file.arrayBuffer())\n\n // Detect actual MIME type from file content (prevents spoofing)\n const { detectMimeType } = await import('@murumets-ee/storage')\n const { mimeType, mismatch } = await detectMimeType(\n buffer,\n file.type || 'application/octet-stream',\n )\n if (mismatch) {\n return errorJson(\n `File content doesn't match declared type: claimed ${file.type}, detected ${mimeType}`,\n 400,\n )\n }\n\n const result = await client.upload(buffer, {\n filename: file.name,\n mimeType,\n size: file.size,\n uploadedBy: user.id,\n })\n\n const item: MediaPickerItem = {\n id: result.media.id,\n title: result.media.title,\n alt: result.media.alt,\n filename: result.media.filename,\n mimeType: result.media.mimeType,\n size: result.media.size,\n mediaType: result.media.mediaType,\n url: result.url,\n width: result.media.width,\n height: result.media.height,\n }\n\n audit?.({\n action: 'media.upload',\n entityType: 'media',\n entityId: result.media.id,\n userId: user.id,\n userName: user.name,\n changes: {\n filename: result.media.filename,\n mimeType: result.media.mimeType,\n size: result.media.size,\n mediaType: result.media.mediaType,\n },\n })\n\n return json(item, 201)\n}\n\nasync function handleDelete(\n _req: Request,\n { segments, user, audit }: { segments: string[]; user: AuthUser; audit?: AuditLogFn },\n): Promise<Response> {\n if (segments.length === 0) {\n return errorJson('Media ID required', 400)\n }\n\n const id = segments[0]\n if (!isValidUuid(id)) return errorJson('Invalid media ID format', 400)\n\n // AdminClient.delete() checks entity_refs and throws ReferencedEntityError\n // if this media is still referenced. The error bubbles to the caller's\n // error handler which returns 409 with usage details.\n const client = await getClient()\n await client.delete(id)\n\n audit?.({\n action: 'media.delete',\n entityType: 'media',\n entityId: id,\n userId: user.id,\n userName: user.name,\n })\n\n return json({ deleted: 1 })\n}\n\n// ---------------------------------------------------------------------------\n// Route factory\n// ---------------------------------------------------------------------------\n\n/**\n * Create admin API routes for media-specific operations.\n *\n * Standard entity CRUD (PATCH with translations) is handled by the generic entity\n * handler — add `Media` to the `entities` array in your handler config.\n *\n * Routes handled by this plugin:\n * - `GET /api/admin/media` — List media with search/filter + URLs\n * - `GET /api/admin/media/settings` — Get current image styles\n * - `GET /api/admin/media/:id` — Get single media item with URL\n * - `GET /api/admin/media/:id/usage` — Find entities referencing this media\n * - `POST /api/admin/media` — Upload file (FormData with `file` field)\n * - `POST /api/admin/media/settings` — Save image styles (admin only)\n * - `POST /api/admin/media/regenerate-variants` — Regenerate all image variants (admin only)\n * - `DELETE /api/admin/media/:id` — Delete media + storage file (blocked if in use)\n *\n * Routes handled by generic entity handler (via fallthrough):\n * - `PATCH /api/admin/media/:id` — Update metadata with translation support\n */\nexport function mediaRoutes(): AdminRoute {\n return {\n prefix: 'media',\n resource: 'media',\n actions: ['view', 'create', 'update', 'delete'],\n handlers: {\n GET: handleGet,\n POST: handlePost,\n DELETE: handleDelete,\n },\n }\n}\n"],"mappings":"AAgCA,MAAM,EAAY,CAAC,QAAS,UAAW,SAAU,UAAW,OAAO,CAC7D,EAAe,CAAC,OAAQ,OAAQ,MAAO,OAAO,CAEpD,SAAS,EACP,EACuF,CACvF,IAAK,GAAM,CAAC,EAAM,KAAQ,OAAO,QAAQ,EAAO,CAAE,CAChD,GAAI,OAAO,GAAS,UAAY,EAAK,SAAW,GAAK,CAAC,qBAAqB,KAAK,EAAK,CACnF,MAAO,CACL,MAAO,GACP,MAAO,uBAAuB,EAAK,oDACpC,CAEH,GAAI,CAAC,GAAO,OAAO,GAAQ,SACzB,MAAO,CAAE,MAAO,GAAO,MAAO,UAAU,EAAK,qBAAsB,CAErE,IAAM,EAAQ,EACd,GAAI,EAAM,QAAU,IAAA,KAAc,OAAO,EAAM,OAAU,UAAY,EAAM,O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,MAAM,GAAW,EACnB,OAAO,EAAG,CAEvB,IAAQ,CACN,OAAQ,eACR,WAAY,QACZ,SAAU,EACV,OAAQ,EAAK,GACb,SAAU,EAAK,KAChB,CAAC,CAEK,EAAK,CAAE,QAAS,EAAG,CAAC,EAhBE,EAAU,0BAA2B,IAAI,CA0CxE,SAAgB,GAA0B,CACxC,MAAO,CACL,OAAQ,QACR,SAAU,QACV,QAAS,CAAC,OAAQ,SAAU,SAAU,SAAS,CAC/C,SAAU,CACR,IAAK,EACL,KAAM,EACN,OAAQ,EACT,CACF"}
1
+ {"version":3,"file":"admin.mjs","names":[],"sources":["../src/admin/routes.ts"],"sourcesContent":["/**\n * Media admin routes for the centralized admin API handler.\n *\n * Provides media-specific operations: upload, URL resolution, delete with storage cleanup,\n * image style settings, and variant regeneration.\n *\n * Standard entity CRUD (PATCH with translations) falls through to the generic entity\n * handler — add `Media` to the `entities` array in your handler config.\n *\n * @example\n * ```typescript\n * import { createAdminApiHandler } from '@murumets-ee/admin-ui/server'\n * import { mediaRoutes } from '@murumets-ee/media/admin'\n * import { Media } from '@murumets-ee/media'\n *\n * const handler = createAdminApiHandler({\n * authenticate: async (req) => { ... },\n * entities: [Article, Media],\n * routes: [mediaRoutes()],\n * })\n * ```\n */\n\nimport type { AdminRoute, AuditLogFn, AuthUser } from '@murumets-ee/core'\nimport type { MediaClient } from '../client.js'\nimport type { MediaPickerItem, MediaPickerListResult } from '../picker/types.js'\nimport type { ImageStyle } from '../types.js'\n\n// ---------------------------------------------------------------------------\n// Image style validation\n// ---------------------------------------------------------------------------\n\nconst VALID_FIT = ['cover', 'contain', 'inside', 'outside', 'fill'] as const\nconst VALID_FORMAT = ['webp', 'jpeg', 'png', 'avif'] as const\n\nfunction validateImageStyles(\n styles: Record<string, unknown>,\n): { valid: true; styles: Record<string, ImageStyle> } | { valid: false; error: string } {\n for (const [name, raw] of Object.entries(styles)) {\n if (typeof name !== 'string' || name.length === 0 || !/^[a-z][a-z0-9_-]*$/.test(name)) {\n return {\n valid: false,\n error: `Invalid style name \"${name}\": must be lowercase alphanumeric (a-z, 0-9, -, _)`,\n }\n }\n if (!raw || typeof raw !== 'object') {\n return { valid: false, error: `Style \"${name}\" must be an object` }\n }\n const style = raw as Record<string, unknown>\n if (style.width !== undefined && (typeof style.width !== 'number' || style.width <= 0)) {\n return { valid: false, error: `Invalid width for style \"${name}\": must be a positive number` }\n }\n if (style.height !== undefined && (typeof style.height !== 'number' || style.height <= 0)) {\n return {\n valid: false,\n error: `Invalid height for style \"${name}\": must be a positive number`,\n }\n }\n if (style.width === undefined && style.height === undefined) {\n return { valid: false, error: `Style \"${name}\" must have at least width or height` }\n }\n if (\n style.quality !== undefined &&\n (typeof style.quality !== 'number' || style.quality < 1 || style.quality > 100)\n ) {\n return { valid: false, error: `Invalid quality for style \"${name}\": must be 1-100` }\n }\n if (\n style.fit !== undefined &&\n !(VALID_FIT as readonly string[]).includes(style.fit as string)\n ) {\n return {\n valid: false,\n error: `Invalid fit for style \"${name}\": must be one of ${VALID_FIT.join(', ')}`,\n }\n }\n if (\n style.format !== undefined &&\n !(VALID_FORMAT as readonly string[]).includes(style.format as string)\n ) {\n return {\n valid: false,\n error: `Invalid format for style \"${name}\": must be one of ${VALID_FORMAT.join(', ')}`,\n }\n }\n }\n return { valid: true, styles: styles as Record<string, ImageStyle> }\n}\n\n// ---------------------------------------------------------------------------\n// Validation\n// ---------------------------------------------------------------------------\n\nconst UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i\n\nfunction isValidUuid(value: string): boolean {\n return UUID_RE.test(value)\n}\n\n// ---------------------------------------------------------------------------\n// Per-request MediaClient factory\n// ---------------------------------------------------------------------------\n//\n// MediaClient and its AdminClient MUST be built per-request — AdminClient's\n// context resolver is captured eagerly at construction (see\n// buildContextResolver in @murumets-ee/core/clients). A module-level singleton\n// would bake the first request's user + tenant scope into every subsequent\n// request's writes — a cross-request permission/scope leak.\n//\n// Storage config is process-global and safe to cache once.\n\nlet storagePromise: Promise<Awaited<ReturnType<typeof import('@murumets-ee/storage').createStorageClient>>> | null = null\n\nasync function getStorage() {\n if (!storagePromise) {\n storagePromise = (async () => {\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n const app = getApp()\n return createStorageClient(getStorageConfig(), { app })\n })()\n }\n return storagePromise\n}\n\nasync function getClient(): Promise<MediaClient> {\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const { MediaClient } = await import('../client.js')\n const { Media } = await import('../entity.js')\n const storage = await getStorage()\n const admin = createAdminClient(Media)\n return new MediaClient({ admin, storage })\n}\n\n// ---------------------------------------------------------------------------\n// Response helpers\n// ---------------------------------------------------------------------------\n\nfunction json(data: unknown, status = 200) {\n return new Response(JSON.stringify(data), {\n status,\n headers: { 'Content-Type': 'application/json' },\n })\n}\n\nfunction errorJson(message: string, status: number) {\n return json({ error: message }, status)\n}\n\n// ---------------------------------------------------------------------------\n// Handlers\n// ---------------------------------------------------------------------------\n\nasync function handleGet(\n req: Request,\n { segments }: { segments: string[]; user: AuthUser; audit?: AuditLogFn },\n): Promise<Response> {\n const client = await getClient()\n\n // GET /media/settings — return current image styles\n if (segments.length === 1 && segments[0] === 'settings') {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('../image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const styles = await settingsClient.get('imageStyles')\n return json({ imageStyles: styles ?? {} })\n }\n\n // GET /media/:id/usage — find all entities referencing this media\n if (segments.length === 2 && segments[1] === 'usage') {\n const id = segments[0]\n if (!isValidUuid(id)) return errorJson('Invalid media ID format', 400)\n\n const { findMediaUsages } = await import('../usage.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const usages = await findMediaUsages(id, app.db.readWrite)\n return json({ usages })\n }\n\n // GET /media/:id — single item with URL (full record for EntityForm + picker)\n if (segments.length > 0) {\n const id = segments[0]\n const record = await client.findById(id)\n if (!record) return errorJson('Media not found', 404)\n\n const url = await client.getUrl(id)\n return json({ ...record, url })\n }\n\n // GET /media — list with search/filter + batch URL resolution\n const url = new URL(req.url)\n const search = url.searchParams.get('search') ?? undefined\n const mediaType = url.searchParams.get('mediaType') ?? undefined\n const limit = Math.min(Math.max(Number(url.searchParams.get('limit')) || 24, 1), 100)\n const offset = Math.max(Number(url.searchParams.get('offset')) || 0, 0)\n\n const result = await client.findMany({\n search,\n mediaType: mediaType as 'image' | 'video' | 'audio' | 'document' | 'other' | undefined,\n limit,\n offset,\n })\n\n // Resolve original URLs + thumbnail variant URLs for all items\n const ids = result.items.map((item) => item.id)\n const [urlMap, thumbMap] = await Promise.all([\n client.getUrls(ids),\n client.getVariantUrls(ids, 'thumbnail'),\n ])\n\n const items: (MediaPickerItem & { thumbnailUrl?: string })[] = result.items.map((item) => ({\n id: item.id,\n title: item.title ?? null,\n alt: item.alt ?? null,\n filename: item.filename,\n mimeType: item.mimeType,\n size: item.size,\n mediaType: item.mediaType,\n url: urlMap.get(item.id) ?? '',\n thumbnailUrl: thumbMap.get(item.id),\n width: item.width ?? null,\n height: item.height ?? null,\n }))\n\n const response: MediaPickerListResult = { items, total: result.total }\n return json(response)\n}\n\nasync function handlePost(\n req: Request,\n {\n segments,\n user,\n audit,\n checkPermission,\n }: {\n segments: string[]\n user: AuthUser\n audit?: AuditLogFn\n checkPermission: (resource: string, action: string) => boolean\n },\n): Promise<Response> {\n // POST /media/regenerate-variants — creates new variant files.\n // Framework-level gate already requires 'media.create' (METHOD_TO_ACTION maps POST → create),\n // so we don't need a redundant handler check here. Leaving a brief assert for defence-in-depth.\n if (segments.length === 1 && segments[0] === 'regenerate-variants') {\n if (!checkPermission('media', 'create')) {\n return errorJson('Forbidden: media create permission required for variant regeneration', 403)\n }\n\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('../image-styles-settings.js')\n const { regenerateAllVariants } = await import('../regenerate-variants.js')\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const styles = await settingsClient.get('imageStyles')\n if (!styles || Object.keys(styles).length === 0) {\n return errorJson('No image styles configured', 400)\n }\n\n const storageConfig = getStorageConfig()\n const storage = createStorageClient(storageConfig, { app })\n\n const result = await regenerateAllVariants({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n styles,\n })\n\n audit?.({\n action: 'media.regenerate_variants',\n userId: user.id,\n userName: user.name,\n metadata: { total: result.total, processed: result.processed, errors: result.errors },\n })\n\n return json(result)\n }\n\n // POST /media/settings — manages image style definitions.\n // Framework-level gate (POST → media.create) already enforces access.\n if (segments.length === 1 && segments[0] === 'settings') {\n if (!checkPermission('media', 'create')) {\n return errorJson(\n 'Forbidden: media create permission required for image style management',\n 403,\n )\n }\n\n const body = (await req.json()) as { imageStyles?: Record<string, unknown> }\n if (!body.imageStyles || typeof body.imageStyles !== 'object') {\n return errorJson('Body must contain \"imageStyles\" object', 400)\n }\n\n const validation = validateImageStyles(body.imageStyles)\n if (!validation.valid) {\n return errorJson(validation.error, 400)\n }\n\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('../image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n await settingsClient.set('imageStyles', validation.styles)\n\n // Invalidate cached styles on the MediaClient singleton\n const client = await getClient()\n client.invalidateImageStylesCache()\n\n audit?.({\n action: 'media.settings.update',\n entityType: 'settings',\n userId: user.id,\n userName: user.name,\n changes: { imageStyles: validation.styles },\n })\n\n return json({ imageStyles: validation.styles })\n }\n\n // POST /media — upload file (requires media create permission)\n if (!checkPermission('media', 'create')) {\n return errorJson('Forbidden: media create permission required for upload', 403)\n }\n\n const client = await getClient()\n\n const formData = await req.formData()\n const file = formData.get('file') as File | null\n if (!file || file.size === 0) {\n return errorJson('No file provided', 400)\n }\n\n // Guard against oversized uploads (50 MB limit)\n const MAX_UPLOAD_SIZE = 50 * 1024 * 1024\n if (file.size > MAX_UPLOAD_SIZE) {\n return errorJson(\n `File too large: ${(file.size / 1024 / 1024).toFixed(1)} MB exceeds 50 MB limit`,\n 400,\n )\n }\n\n const buffer = Buffer.from(await file.arrayBuffer())\n\n // Detect actual MIME type from file content (prevents spoofing)\n const { detectMimeType } = await import('@murumets-ee/storage')\n const { mimeType, mismatch } = await detectMimeType(\n buffer,\n file.type || 'application/octet-stream',\n )\n if (mismatch) {\n return errorJson(\n `File content doesn't match declared type: claimed ${file.type}, detected ${mimeType}`,\n 400,\n )\n }\n\n const result = await client.upload(buffer, {\n filename: file.name,\n mimeType,\n size: file.size,\n uploadedBy: user.id,\n })\n\n const item: MediaPickerItem = {\n id: result.media.id,\n title: result.media.title ?? null,\n alt: result.media.alt ?? null,\n filename: result.media.filename,\n mimeType: result.media.mimeType,\n size: result.media.size,\n mediaType: result.media.mediaType,\n url: result.url,\n width: result.media.width ?? null,\n height: result.media.height ?? null,\n }\n\n audit?.({\n action: 'media.upload',\n entityType: 'media',\n entityId: result.media.id,\n userId: user.id,\n userName: user.name,\n changes: {\n filename: result.media.filename,\n mimeType: result.media.mimeType,\n size: result.media.size,\n mediaType: result.media.mediaType,\n },\n })\n\n return json(item, 201)\n}\n\nasync function handleDelete(\n _req: Request,\n {\n segments,\n user,\n audit,\n checkPermission,\n }: {\n segments: string[]\n user: AuthUser\n audit?: AuditLogFn\n checkPermission: (resource: string, action: string) => boolean\n },\n): Promise<Response> {\n if (!checkPermission('media', 'delete')) {\n return errorJson('Forbidden: media delete permission required', 403)\n }\n\n if (segments.length === 0) {\n return errorJson('Media ID required', 400)\n }\n\n const id = segments[0]\n if (!isValidUuid(id)) return errorJson('Invalid media ID format', 400)\n\n // AdminClient.delete() checks entity_refs and throws ReferencedEntityError\n // if this media is still referenced. The error bubbles to the caller's\n // error handler which returns 409 with usage details.\n const client = await getClient()\n await client.delete(id)\n\n audit?.({\n action: 'media.delete',\n entityType: 'media',\n entityId: id,\n userId: user.id,\n userName: user.name,\n })\n\n return json({ deleted: 1 })\n}\n\n// ---------------------------------------------------------------------------\n// Route factory\n// ---------------------------------------------------------------------------\n\n/**\n * Create admin API routes for media-specific operations.\n *\n * Standard entity CRUD (PATCH with translations) is handled by the generic entity\n * handler — add `Media` to the `entities` array in your handler config.\n *\n * Routes handled by this plugin:\n * - `GET /api/admin/media` — List media with search/filter + URLs\n * - `GET /api/admin/media/settings` — Get current image styles\n * - `GET /api/admin/media/:id` — Get single media item with URL\n * - `GET /api/admin/media/:id/usage` — Find entities referencing this media\n * - `POST /api/admin/media` — Upload file (FormData with `file` field)\n * - `POST /api/admin/media/settings` — Save image styles (admin only)\n * - `POST /api/admin/media/regenerate-variants` — Regenerate all image variants (admin only)\n * - `DELETE /api/admin/media/:id` — Delete media + storage file (blocked if in use)\n *\n * Routes handled by generic entity handler (via fallthrough):\n * - `PATCH /api/admin/media/:id` — Update metadata with translation support\n */\nexport function mediaRoutes(): AdminRoute {\n return {\n prefix: 'media',\n resource: 'media',\n actions: ['view', 'create', 'update', 'delete'],\n handlers: {\n GET: handleGet,\n POST: handlePost,\n DELETE: handleDelete,\n },\n }\n}\n"],"mappings":"AAgCA,MAAM,EAAY,CAAC,QAAS,UAAW,SAAU,UAAW,OAAO,CAC7D,EAAe,CAAC,OAAQ,OAAQ,MAAO,OAAO,CAEpD,SAAS,EACP,EACuF,CACvF,IAAK,GAAM,CAAC,EAAM,KAAQ,OAAO,QAAQ,EAAO,CAAE,CAChD,GAAI,OAAO,GAAS,UAAY,EAAK,SAAW,GAAK,CAAC,qBAAqB,KAAK,EAAK,CACnF,MAAO,CACL,MAAO,GACP,MAAO,uBAAuB,EAAK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,SAAU,EAAO,MAAM,GACvB,OAAQ,EAAK,GACb,SAAU,EAAK,KACf,QAAS,CACP,SAAU,EAAO,MAAM,SACvB,SAAU,EAAO,MAAM,SACvB,KAAM,EAAO,MAAM,KACnB,UAAW,EAAO,MAAM,UACzB,CACF,CAAC,CAEK,EAAK,EAAM,IAAI,CAGxB,eAAe,EACb,EACA,CACE,WACA,OACA,QACA,mBAOiB,CACnB,GAAI,CAAC,EAAgB,QAAS,SAAS,CACrC,OAAO,EAAU,8CAA+C,IAAI,CAGtE,GAAI,EAAS,SAAW,EACtB,OAAO,EAAU,oBAAqB,IAAI,CAG5C,IAAM,EAAK,EAAS,GAiBpB,OAhBK,EAAY,EAAG,EAMpB,MADe,MAAM,GAAW,EACnB,OAAO,EAAG,CAEvB,IAAQ,CACN,OAAQ,eACR,WAAY,QACZ,SAAU,EACV,OAAQ,EAAK,GACb,SAAU,EAAK,KAChB,CAAC,CAEK,EAAK,CAAE,QAAS,EAAG,CAAC,EAhBE,EAAU,0BAA2B,IAAI,CA0CxE,SAAgB,GAA0B,CACxC,MAAO,CACL,OAAQ,QACR,SAAU,QACV,QAAS,CAAC,OAAQ,SAAU,SAAU,SAAS,CAC/C,SAAU,CACR,IAAK,EACL,KAAM,EACN,OAAQ,EACT,CACF"}
@@ -0,0 +1,2 @@
1
+ import"./entity-DZFku8b7.mjs";import{n as e,r as t,t as n}from"./variant-key-DZUYURS5.mjs";import"server-only";var r=class{admin;storage;imageStyles;constructor(e){this.admin=e.admin,this.storage=e.storage,this.imageStyles=e.imageStyles??null}async resolveImageStyles(){if(this.imageStyles)return this.imageStyles;try{let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{getApp:n}=await import(`@murumets-ee/core`),r=await e(t,{app:n()}).get(`imageStyles`);if(r&&Object.keys(r).length>0)return this.imageStyles=r,r}catch{}try{let{getMediaConfig:e}=await import(`./plugin-DV7lvImm.mjs`);return this.imageStyles=e().imageStyles,this.imageStyles}catch{let e={thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}};return this.imageStyles=e,e}}invalidateImageStylesCache(){this.imageStyles=null}async upload(r,o){let s=await this.storage.upload(r,{filename:o.filename,mimeType:o.mimeType,size:o.size,visibility:o.visibility,uploadedBy:o.uploadedBy}),c=o.width??null,l=o.height??null,u={};if(r instanceof Buffer&&e(o.mimeType))try{let e=await t(r,await this.resolveImageStyles());c=e.width,l=e.height;let i=s.visibility;await Promise.all([...e.variants.entries()].map(async([e,t])=>{let r=n(s.key,e,t.format);try{await this.storage.upload(t.buffer,{key:r,filename:`${e}_${o.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:i,metadata:{variantOf:s.key,style:e},uploadedBy:o.uploadedBy}),u[e]=r}catch{}})),Object.keys(u).length>0&&await this.storage.updateMetadata(s.key,{metadata:{...s.metadata??{},variants:u}}).catch(()=>{})}catch{}let d=i(o.mimeType);try{return{media:await this.admin.create({title:o.title??a(o.filename),alt:o.alt??null,description:o.description??null,fileKey:s.key,filename:o.filename,mimeType:o.mimeType,size:o.size,width:c,height:l,mediaType:d}),url:await this.storage.getUrl(s.key)}}catch(e){for(let e of Object.values(u))await this.storage.delete(e).catch(()=>{});throw await this.storage.delete(s.key).catch(()=>{}),e}}async findById(e,t){return this.admin.findById(e,t)}async findMany(e){let{schemaRegistry:t}=await import(`@murumets-ee/db`),{and:n,asc:r,desc:i,eq:a,ilike:o,or:s,sql:c}=await import(`drizzle-orm`),l=t.get(`media`);if(!l)throw Error(`Media schema not registered. Is the media() plugin loaded?`);let u=[];if(e?.mediaType&&u.push(a(l.mediaType,e.mediaType)),e?.mimeTypePrefix){let t=e.mimeTypePrefix.replace(/[\\%_]/g,`\\$&`);u.push(o(l.mimeType,`${t}%`))}if(e?.search){let t=`%${e.search.replace(/[\\%_]/g,`\\$&`)}%`;u.push(s(o(l.filename,t),c`${l.fields} ->> 'title' ILIKE ${t}`))}let d=e?.limit??50,f=e?.offset??0,p=u.length>0?n(...u):void 0,m=await this.admin.count({where:p}),h=e?.orderBy===`filename`?l.filename:l.createdAt,g=(e?.orderDirection??`desc`)===`asc`?r:i;return{items:await this.admin.findMany({where:p,limit:d,offset:f,orderBy:g(h)}),total:m,limit:d,offset:f}}async update(e,t){return this.admin.update(e,t)}async delete(e){let t=await this.admin.findById(e);if(!t)throw Error(`Media not found: ${e}`);let n=t.fileKey;await this.admin.delete(e);let r=(await this.storage.getMetadata(n))?.metadata?.variants;if(r)for(let e of Object.values(r))await this.storage.delete(e).catch(()=>{});await this.storage.delete(n).catch(()=>{})}async getUrl(e){let t=await this.admin.findById(e);if(!t)throw Error(`Media not found: ${e}`);return this.storage.getUrl(t.fileKey)}async getUrls(e){if(e.length===0)return new Map;let{schemaRegistry:t}=await import(`@murumets-ee/db`),{inArray:n}=await import(`drizzle-orm`),r=t.get(`media`);if(!r)return new Map;let i=await this.admin.findMany({where:n(r.id,e),limit:e.length}),a=new Map;return await Promise.all(i.map(async e=>{let t=await this.storage.getUrl(e.fileKey);a.set(e.id,t)})),a}async getVariantUrl(e,t){let r=await this.admin.findById(e);if(!r)return null;let i=r.fileKey,a=(await this.resolveImageStyles())[t];if(a){let e=n(i,t,a.format??`webp`);try{return await this.storage.getUrl(e)}catch{}}try{return await this.storage.getUrl(i)}catch{return null}}async getVariantUrls(e,t){if(e.length===0)return new Map;let{schemaRegistry:r}=await import(`@murumets-ee/db`),{inArray:i}=await import(`drizzle-orm`),a=r.get(`media`);if(!a)return new Map;let o=await this.admin.findMany({where:i(a.id,e),limit:e.length}),s=(await this.resolveImageStyles())[t],c=new Map;return await Promise.all(o.map(async e=>{if(s){let r=n(e.fileKey,t,s.format??`webp`);try{let t=await this.storage.getUrl(r);c.set(e.id,t);return}catch{}}try{let t=await this.storage.getUrl(e.fileKey);c.set(e.id,t)}catch{}})),c}};function i(e){return e.startsWith(`image/`)?`image`:e.startsWith(`video/`)?`video`:e.startsWith(`audio/`)?`audio`:e===`application/pdf`||e.startsWith(`application/msword`)||e.startsWith(`application/vnd.`)?`document`:`other`}function a(e){return e.replace(/\.[^.]+$/,``).replace(/[-_]/g,` `)}export{r as MediaClient};
2
+ //# sourceMappingURL=client-B__UIAjI.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client-B__UIAjI.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["/**\n * MediaClient — wraps AdminClient + StorageClient for media management.\n *\n * Usage:\n * import { createMediaClient } from '@murumets-ee/media/client'\n * const media = await createMediaClient(storageClient)\n * const result = await media.upload(buffer, { filename: 'photo.jpg', mimeType: 'image/jpeg', size: 12345 })\n */\n\nimport 'server-only'\n\nimport type { AdminClient } from '@murumets-ee/entity/admin'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport { Media } from './entity.js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type {\n ImageStyle,\n MediaListOptions,\n MediaListResult,\n MediaRecord,\n MediaType,\n MediaUploadOptions,\n MediaUploadResult,\n} from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\ntype MediaFields = typeof Media.allFields\n\nexport interface MediaClientConfig {\n admin: AdminClient<MediaFields>\n storage: StorageClient\n /** Image styles to generate on upload. Loaded from plugin config if not provided. */\n imageStyles?: Record<string, ImageStyle>\n}\n\nexport class MediaClient {\n private admin: AdminClient<MediaFields>\n private storage: StorageClient\n private imageStyles: Record<string, ImageStyle> | null\n\n constructor(config: MediaClientConfig) {\n this.admin = config.admin\n this.storage = config.storage\n this.imageStyles = config.imageStyles ?? null\n }\n\n /**\n * Resolve image styles: settings DB → plugin config → hardcoded defaults.\n * Result is cached; call `invalidateImageStylesCache()` after settings update.\n */\n private async resolveImageStyles(): Promise<Record<string, ImageStyle>> {\n if (this.imageStyles) return this.imageStyles\n\n // 1. Try settings DB (source of truth after first boot)\n try {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('./image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const stored = await settingsClient.get('imageStyles')\n if (stored && Object.keys(stored).length > 0) {\n this.imageStyles = stored\n return stored\n }\n } catch {\n // Settings not available — fall through to plugin config\n }\n\n // 2. Fall back to plugin config singleton\n try {\n const { getMediaConfig } = await import('./plugin.js')\n const config = getMediaConfig()\n this.imageStyles = config.imageStyles\n return this.imageStyles\n } catch {\n // Plugin not initialized — hardcoded defaults\n const defaults: Record<string, ImageStyle> = {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n }\n this.imageStyles = defaults\n return defaults\n }\n }\n\n /** Clear cached styles so next access re-reads from settings DB. */\n invalidateImageStylesCache(): void {\n this.imageStyles = null\n }\n\n // ---------------------------------------------------------------\n // Upload — the key convenience method\n // ---------------------------------------------------------------\n\n /**\n * Upload a file and create a media entity record in one step.\n * 1. Uploads original to storage (StorageClient)\n * 2. If image: extracts dimensions via Sharp + generates variants\n * 3. Creates media entity record (AdminClient)\n * 4. Returns the media record + URL\n *\n * Rolls back storage upload if entity creation fails.\n * Variant generation failures are logged but don't fail the upload.\n */\n async upload(\n data: Buffer | ReadableStream<Uint8Array>,\n options: MediaUploadOptions,\n ): Promise<MediaUploadResult> {\n // 1. Upload original file to storage\n const fileRecord = await this.storage.upload(data, {\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n visibility: options.visibility,\n uploadedBy: options.uploadedBy,\n })\n\n // 2. Image processing — extract dimensions + generate variants\n let width = options.width ?? null\n let height = options.height ?? null\n const variantKeys: Record<string, string> = {}\n\n if (data instanceof Buffer && isProcessableImage(options.mimeType)) {\n try {\n const styles = await this.resolveImageStyles()\n const processed = await processImage(data, styles)\n\n // Set dimensions from Sharp metadata\n width = processed.width\n height = processed.height\n\n // Upload each variant to storage\n const visibility = fileRecord.visibility\n await Promise.all(\n [...processed.variants.entries()].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(fileRecord.key, styleName, variant.format)\n try {\n await this.storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${options.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: fileRecord.key, style: styleName },\n uploadedBy: options.uploadedBy,\n })\n variantKeys[styleName] = vKey\n } catch {\n // Variant upload failure is non-fatal — original is saved\n }\n }),\n )\n\n // Store variant keys in original file's metadata\n if (Object.keys(variantKeys).length > 0) {\n await this.storage\n .updateMetadata(fileRecord.key, {\n metadata: {\n ...(fileRecord.metadata ?? {}),\n variants: variantKeys,\n },\n })\n .catch(() => {\n // Metadata update failure is non-fatal\n })\n }\n } catch {\n // Image processing failure is non-fatal — original is saved\n }\n }\n\n // 3. Create media entity record\n const mediaType = deriveMediaType(options.mimeType)\n\n try {\n const entityRecord = await this.admin.create({\n title: options.title ?? deriveTitle(options.filename),\n alt: options.alt ?? null,\n description: options.description ?? null,\n fileKey: fileRecord.key,\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n width,\n height,\n mediaType,\n })\n\n // 4. Get URL\n const url = await this.storage.getUrl(fileRecord.key)\n\n return {\n media: entityRecord,\n url,\n }\n } catch (error) {\n // Rollback: delete variants + original if entity creation fails\n\n // Delete variants first (best-effort)\n for (const vKey of Object.values(variantKeys)) {\n await this.storage.delete(vKey).catch(() => {})\n }\n\n // Delete original\n await this.storage.delete(fileRecord.key).catch(() => {\n // Storage rollback failure is already being handled — propagate original error\n })\n throw error\n }\n }\n\n // ---------------------------------------------------------------\n // CRUD delegation\n // ---------------------------------------------------------------\n\n async findById(id: string, options?: { locale?: string }): Promise<MediaRecord | null> {\n return this.admin.findById(id, options)\n }\n\n async findMany(options?: MediaListOptions): Promise<MediaListResult> {\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { and, asc, desc, eq, ilike, or, sql } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered. Is the media() plugin loaded?')\n\n // Build where conditions\n const conditions = []\n\n if (options?.mediaType) {\n conditions.push(eq(table.mediaType, options.mediaType))\n }\n if (options?.mimeTypePrefix) {\n // Escape ILIKE wildcards (%, _) in user input to prevent pattern injection\n const escaped = options.mimeTypePrefix.replace(/[\\\\%_]/g, '\\\\$&')\n conditions.push(ilike(table.mimeType, `${escaped}%`))\n }\n if (options?.search) {\n // Escape ILIKE wildcards in user input, then wrap with %...%\n const escaped = options.search.replace(/[\\\\%_]/g, '\\\\$&')\n const pattern = `%${escaped}%`\n conditions.push(\n or(\n ilike(table.filename, pattern),\n sql`${table.fields} ->> 'title' ILIKE ${pattern}`,\n )!,\n )\n }\n\n const limit = options?.limit ?? 50\n const offset = options?.offset ?? 0\n const whereClause = conditions.length > 0 ? and(...conditions) : undefined\n\n // Count total via AdminClient\n const total = await this.admin.count({ where: whereClause })\n\n // Fetch items via AdminClient for proper DTO shaping\n const orderField = options?.orderBy === 'filename' ? table.filename : table.createdAt\n const orderFn = (options?.orderDirection ?? 'desc') === 'asc' ? asc : desc\n\n const items = await this.admin.findMany({\n where: whereClause,\n limit,\n offset,\n orderBy: orderFn(orderField),\n })\n\n return {\n items,\n total,\n limit,\n offset,\n }\n }\n\n async update(\n id: string,\n data: { title?: string; alt?: string; description?: string },\n ): Promise<MediaRecord> {\n return this.admin.update(id, data)\n }\n\n /**\n * Delete a media entity, its variants, and its original file in storage.\n */\n async delete(id: string): Promise<void> {\n const record = await this.admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n const fileKey = record.fileKey\n\n // 1. Delete entity first (ref checking via entity_refs happens here)\n await this.admin.delete(id)\n\n // 2. Look up original file record for variant metadata\n const fileRecord = await this.storage.getMetadata(fileKey)\n const variants = (fileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n\n // 3. Delete variants (best-effort)\n if (variants) {\n for (const vKey of Object.values(variants)) {\n await this.storage.delete(vKey).catch(() => {\n // Variant deletion failure is non-fatal\n })\n }\n }\n\n // 4. Delete original file (best-effort)\n await this.storage.delete(fileKey).catch(() => {\n // Storage deletion failure is non-fatal — entity is already deleted\n })\n }\n\n // ---------------------------------------------------------------\n // URL resolution\n // ---------------------------------------------------------------\n\n /**\n * Get URL for a media entity by its ID.\n * Resolves entity -> fileKey -> storage URL.\n */\n async getUrl(id: string): Promise<string> {\n const record = await this.admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n return this.storage.getUrl(record.fileKey)\n }\n\n /**\n * Get URLs for multiple media entities (batch).\n * Returns a Map of mediaId -> url.\n */\n async getUrls(ids: string[]): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await this.admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n records.map(async (record) => {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n }),\n )\n\n return urlMap\n }\n\n /**\n * Get variant URL for a specific image style.\n * Falls back to original URL if the variant doesn't exist.\n *\n * @param id - Media entity ID\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns The variant URL, or original URL as fallback, or null if media not found\n */\n async getVariantUrl(id: string, styleName: string): Promise<string | null> {\n const record = await this.admin.findById(id)\n if (!record) return null\n\n const fileKey = record.fileKey\n\n // Try variant key first\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n if (style) {\n const vKey = deriveVariantKey(fileKey, styleName, style.format ?? 'webp')\n try {\n return await this.storage.getUrl(vKey)\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n return await this.storage.getUrl(fileKey)\n } catch {\n return null\n }\n }\n\n /**\n * Get variant URLs for multiple media entities (batch).\n * Falls back to original URL per item if the variant doesn't exist.\n *\n * @param ids - Media entity IDs\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns Map of mediaId -> variant URL (or original URL as fallback)\n */\n async getVariantUrls(ids: string[], styleName: string): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await this.admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n records.map(async (record) => {\n // Try variant URL\n if (style) {\n const vKey = deriveVariantKey(record.fileKey, styleName, style.format ?? 'webp')\n try {\n const url = await this.storage.getUrl(vKey)\n urlMap.set(record.id, url)\n return\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n } catch {\n // Skip — no URL available\n }\n }),\n )\n\n return urlMap\n }\n}\n\n// ---------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------\n\nfunction deriveMediaType(mimeType: string): MediaType {\n if (mimeType.startsWith('image/')) return 'image'\n if (mimeType.startsWith('video/')) return 'video'\n if (mimeType.startsWith('audio/')) return 'audio'\n if (\n mimeType === 'application/pdf' ||\n mimeType.startsWith('application/msword') ||\n mimeType.startsWith('application/vnd.')\n ) {\n return 'document'\n }\n return 'other'\n}\n\nfunction deriveTitle(filename: string): string {\n const withoutExt = filename.replace(/\\.[^.]+$/, '')\n return withoutExt.replace(/[-_]/g, ' ')\n}\n\n/**\n * Factory — creates a MediaClient with an explicit StorageClient.\n * Must be called after createApp().\n */\nexport async function createMediaClient(storage: StorageClient): Promise<MediaClient> {\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const admin = createAdminClient(Media)\n return new MediaClient({ admin, storage })\n}\n\n// ---------------------------------------------------------------------------\n// Per-request MediaClient factory\n// ---------------------------------------------------------------------------\n//\n// NOTE: Storage config is process-global and safe to cache.\n// MediaClient / AdminClient must be built per-request — they carry a\n// context resolver tied to the calling request's user + permissions.\n// A singleton would leak one request's security context across others.\n\nlet _storagePromise: Promise<StorageClient> | null = null\n\nasync function getStorageSingleton(): Promise<StorageClient> {\n if (!_storagePromise) {\n _storagePromise = (async () => {\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n const app = getApp()\n return createStorageClient(getStorageConfig(), { app })\n })()\n }\n return _storagePromise\n}\n\n/**\n * Returns a fresh MediaClient wired to the current request's context.\n * Must be called after createApp(), inside a request context\n * (withAdminContext, runAsCli, etc.).\n *\n * Despite the name, this is NOT cached — the storage config is cached\n * internally but the MediaClient and its AdminClient are rebuilt per call\n * so the security context resolver attaches to the correct request.\n */\nexport async function getMediaClient(): Promise<MediaClient> {\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const storage = await getStorageSingleton()\n const admin = createAdminClient(Media)\n return new MediaClient({ admin, storage })\n}\n"],"mappings":"+GAmCA,IAAa,EAAb,KAAyB,CACvB,MACA,QACA,YAEA,YAAY,EAA2B,CACrC,KAAK,MAAQ,EAAO,MACpB,KAAK,QAAU,EAAO,QACtB,KAAK,YAAc,EAAO,aAAe,KAO3C,MAAc,oBAA0D,CACtE,GAAI,KAAK,YAAa,OAAO,KAAK,YAGlC,GAAI,CACF,GAAM,CAAE,wBAAyB,MAAM,OAAO,yBACxC,CAAE,uBAAwB,MAAM,OAAO,wCACvC,CAAE,UAAW,MAAM,OAAO,qBAG1B,EAAS,MADQ,EAAqB,EAAqB,CAAE,IADvD,GAAQ,CACoD,CAAC,CACrC,IAAI,cAAc,CACtD,GAAI,GAAU,OAAO,KAAK,EAAO,CAAC,OAAS,EAEzC,MADA,MAAK,YAAc,EACZ,OAEH,EAKR,GAAI,CACF,GAAM,CAAE,kBAAmB,MAAM,OAAO,yBAGxC,MADA,MAAK,YADU,GAAgB,CACL,YACnB,KAAK,iBACN,CAEN,IAAM,EAAuC,CAC3C,UAAW,C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AAM,KAAK,QAAQ,OAAO,EAAK,CAC3C,EAAO,IAAI,EAAO,GAAI,EAAI,CAC1B,YACM,GAMV,GAAI,CACF,IAAM,EAAM,MAAM,KAAK,QAAQ,OAAO,EAAO,QAAQ,CACrD,EAAO,IAAI,EAAO,GAAI,EAAI,MACpB,IAGR,CACH,CAEM,IAQX,SAAS,EAAgB,EAA6B,CAWpD,OAVI,EAAS,WAAW,SAAS,CAAS,QACtC,EAAS,WAAW,SAAS,CAAS,QACtC,EAAS,WAAW,SAAS,CAAS,QAExC,IAAa,mBACb,EAAS,WAAW,qBAAqB,EACzC,EAAS,WAAW,mBAAmB,CAEhC,WAEF,QAGT,SAAS,EAAY,EAA0B,CAE7C,OADmB,EAAS,QAAQ,WAAY,GAAG,CACjC,QAAQ,QAAS,IAAI"}
package/dist/client.d.mts CHANGED
@@ -1,24 +1,20 @@
1
- import { a as MediaRecord, c as MediaUploadResult, n as MediaListOptions, r as MediaListResult, s as MediaUploadOptions, t as ImageStyle } from "./types-BV_pOm23.mjs";
2
- import { Logger } from "@murumets-ee/core";
1
+ import { a as MediaRecord, c as MediaUploadResult, l as Media, n as MediaListOptions, r as MediaListResult, s as MediaUploadOptions, t as ImageStyle } from "./types-BBAbJooM.mjs";
2
+ import { AdminClient } from "@murumets-ee/entity/admin";
3
3
  import { StorageClient } from "@murumets-ee/storage";
4
- import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
5
4
 
6
5
  //#region src/client.d.ts
6
+ type MediaFields = typeof Media.allFields;
7
7
  interface MediaClientConfig {
8
- db: PostgresJsDatabase;
8
+ admin: AdminClient<MediaFields>;
9
9
  storage: StorageClient;
10
- logger?: Logger;
11
10
  /** Image styles to generate on upload. Loaded from plugin config if not provided. */
12
11
  imageStyles?: Record<string, ImageStyle>;
13
12
  }
14
13
  declare class MediaClient {
15
- private db;
16
- private storage;
17
- private logger?;
18
14
  private admin;
15
+ private storage;
19
16
  private imageStyles;
20
17
  constructor(config: MediaClientConfig);
21
- private getAdmin;
22
18
  /**
23
19
  * Resolve image styles: settings DB → plugin config → hardcoded defaults.
24
20
  * Result is cached; call `invalidateImageStylesCache()` after settings update.
@@ -85,9 +81,13 @@ declare class MediaClient {
85
81
  */
86
82
  declare function createMediaClient(storage: StorageClient): Promise<MediaClient>;
87
83
  /**
88
- * Lazy singleton returns a shared MediaClient instance.
89
- * Auto-discovers storage configuration from the storage plugin.
90
- * Must be called after createApp().
84
+ * Returns a fresh MediaClient wired to the current request's context.
85
+ * Must be called after createApp(), inside a request context
86
+ * (withAdminContext, runAsCli, etc.).
87
+ *
88
+ * Despite the name, this is NOT cached — the storage config is cached
89
+ * internally but the MediaClient and its AdminClient are rebuilt per call
90
+ * so the security context resolver attaches to the correct request.
91
91
  */
92
92
  declare function getMediaClient(): Promise<MediaClient>;
93
93
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.mts","names":[],"sources":["../src/client.ts"],"mappings":";;;;;;UAgCiB,iBAAA;EACf,EAAA,EAAI,kBAAA;EACJ,OAAA,EAAS,aAAA;EACT,MAAA,GAAS,MAAA;EAFT;EAIA,WAAA,GAAc,MAAA,SAAe,UAAA;AAAA;AAAA,cAGlB,WAAA;EAAA,QACH,EAAA;EAAA,QACA,OAAA;EAAA,QACA,MAAA;EAAA,QACA,KAAA;EAAA,QACA,WAAA;cAEI,MAAA,EAAQ,iBAAA;EAAA,QAON,QAAA;EAdH;;;;EAAA,QA8BG,kBAAA;EAuDkB;EAnBhC,0BAAA,CAAA;EAoBW;;;;;;;;;;EAFL,MAAA,CACJ,IAAA,EAAM,MAAA,GAAS,cAAA,CAAe,UAAA,GAC9B,OAAA,EAAS,kBAAA,GACR,OAAA,CAAQ,iBAAA;EAwIL,QAAA,CAAS,EAAA,UAAY,OAAA;IAAY,MAAA;EAAA,IAAoB,OAAA,CAAQ,WAAA;EAM7D,QAAA,CAAS,OAAA,GAAU,gBAAA,GAAmB,OAAA,CAAQ,eAAA;EAqD9C,MAAA,CACJ,EAAA,UACA,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,GAAA;IAAc,WAAA;EAAA,IACrC,OAAA,CAAQ,WAAA;EA0FyC;;;EAnF9C,MAAA,CAAO,EAAA,WAAa,OAAA;EAqHqC;;;;EA/EzD,MAAA,CAAO,EAAA,WAAa,OAAA;EA3RN;;;;EAsSd,OAAA,CAAQ,GAAA,aAAgB,OAAA,CAAQ,GAAA;EArO9B;;;;;;;;EAuQF,aAAA,CAAc,EAAA,UAAY,SAAA,WAAoB,OAAA;EAzJrC;;;;;;;;EA2LT,cAAA,CAAe,GAAA,YAAe,SAAA,WAAoB,OAAA,CAAQ,GAAA;AAAA;;;;;iBAyE5C,iBAAA,CAAkB,OAAA,EAAS,aAAA,GAAgB,OAAA,CAAQ,WAAA;;;;;;;;;;iBAuCnD,cAAA,CAAA,GAAkB,OAAA,CAAQ,WAAA"}
package/dist/client.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{t as e}from"./entity-D5P2l05s.mjs";import"server-only";import t from"sharp";const n=new Set([`image/svg+xml`,`image/gif`]);function r(e){return e.startsWith(`image/`)&&!n.has(e)}async function i(e,n){let r=await t(e).metadata(),i=r.width??0,a=r.height??0,o=new Map,s=Object.entries(n);return await Promise.all(s.map(async([n,r])=>{let i=r.format??`webp`,a=r.quality??80,s=r.fit??`cover`,{data:c,info:l}=await t(e).resize({width:r.width,height:r.height,fit:s,withoutEnlargement:!0})[i]({quality:a}).toBuffer({resolveWithObject:!0});o.set(n,{buffer:c,format:i,mimeType:`image/${i}`,width:l.width,height:l.height})})),{width:i,height:a,variants:o}}function a(e,t,n=`webp`){let r=e.lastIndexOf(`/`);return`${e.substring(0,r)}/${t}_${e.substring(r+1).replace(/\.[^.]+$/,``)}.${n}`}var o=class{db;storage;logger;admin=null;imageStyles;constructor(e){this.db=e.db,this.storage=e.storage,this.logger=e.logger,this.imageStyles=e.imageStyles??null}async getAdmin(){if(!this.admin){let{AdminClient:t}=await import(`@murumets-ee/entity/admin`);this.admin=new t({entity:e,db:this.db,logger:this.logger})}return this.admin}async resolveImageStyles(){if(this.imageStyles)return this.imageStyles;try{let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings.mjs`),{getApp:n}=await import(`@murumets-ee/core`),r=await e(t,{app:n()}).get(`imageStyles`);if(r&&Object.keys(r).length>0)return this.imageStyles=r,r}catch{}try{let{getMediaConfig:e}=await import(`./plugin.mjs`);return this.imageStyles=e().imageStyles,this.imageStyles}catch{let e={thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}};return this.imageStyles=e,e}}invalidateImageStylesCache(){this.imageStyles=null}async upload(e,t){let n=await this.storage.upload(e,{filename:t.filename,mimeType:t.mimeType,size:t.size,visibility:t.visibility,uploadedBy:t.uploadedBy}),o=t.width??null,l=t.height??null,u={};if(e instanceof Buffer&&r(t.mimeType))try{let r=await i(e,await this.resolveImageStyles());o=r.width,l=r.height;let s=n.visibility;await Promise.all([...r.variants.entries()].map(async([e,r])=>{let i=a(n.key,e,r.format);try{await this.storage.upload(r.buffer,{key:i,filename:`${e}_${t.filename}`,mimeType:r.mimeType,size:r.buffer.byteLength,visibility:s,metadata:{variantOf:n.key,style:e},uploadedBy:t.uploadedBy}),u[e]=i,this.logger?.debug({style:e,key:i,width:r.width,height:r.height},`Variant uploaded`)}catch(t){this.logger?.warn({style:e,key:i,error:t},`Failed to upload variant (non-fatal)`)}})),Object.keys(u).length>0&&await this.storage.updateMetadata(n.key,{metadata:{...n.metadata??{},variants:u}}).catch(e=>{this.logger?.warn({key:n.key,error:e},`Failed to update original file metadata with variant keys (non-fatal)`)}),this.logger?.info({width:o,height:l,variants:Object.keys(u)},`Image processed`)}catch(e){this.logger?.warn({filename:t.filename,error:e},`Image processing failed (non-fatal, original saved)`)}let d=s(t.mimeType),f=await this.getAdmin();try{let e=await f.create({title:t.title??c(t.filename),alt:t.alt??null,description:t.description??null,fileKey:n.key,filename:t.filename,mimeType:t.mimeType,size:t.size,width:o,height:l,mediaType:d}),r=await this.storage.getUrl(n.key);return this.logger?.info({id:e.id,fileKey:n.key,mediaType:d},`Media uploaded`),{media:e,url:r}}catch(e){this.logger?.error({fileKey:n.key,error:e},`Media entity creation failed, rolling back storage uploads`);for(let e of Object.values(u))await this.storage.delete(e).catch(()=>{});throw await this.storage.delete(n.key).catch(e=>{this.logger?.error({fileKey:n.key,error:e},`Storage rollback also failed`)}),e}}async findById(e,t){return await(await this.getAdmin()).findById(e,t)}async findMany(e){let t=await this.getAdmin(),{schemaRegistry:n}=await import(`@murumets-ee/db`),{and:r,asc:i,desc:a,eq:o,ilike:s,sql:c}=await import(`drizzle-orm`),l=n.get(`media`);if(!l)throw Error(`Media schema not registered. Is the media() plugin loaded?`);let u=[];if(e?.mediaType&&u.push(o(l.mediaType,e.mediaType)),e?.mimeTypePrefix&&u.push(s(l.mimeType,`${e.mimeTypePrefix}%`)),e?.search){let t=`%${e.search}%`;u.push(c`(${s(l.filename,t)} OR ${l.fields} ->> 'title' ILIKE ${t})`)}let d=e?.limit??50,f=e?.offset??0,p=u.length>0?r(...u):void 0,[m]=await this.db.select({count:c`count(*)::int`}).from(l).where(p),h=e?.orderBy===`filename`?l.filename:l.createdAt,g=(e?.orderDirection??`desc`)===`asc`?i:a;return{items:await t.findMany({where:p,limit:d,offset:f,orderBy:g(h)}),total:m?.count??0,limit:d,offset:f}}async update(e,t){return await(await this.getAdmin()).update(e,t)}async delete(e){let t=await this.getAdmin(),n=await t.findById(e);if(!n)throw Error(`Media not found: ${e}`);let r=n.fileKey;await t.delete(e);let i=(await this.storage.getMetadata(r))?.metadata?.variants;if(i)for(let e of Object.values(i))await this.storage.delete(e).catch(t=>{this.logger?.warn({variantKey:e,error:t},`Failed to delete variant file`)});await this.storage.delete(r).catch(t=>{this.logger?.error({id:e,fileKey:r,error:t},`Failed to delete file from storage after entity deletion`)}),this.logger?.info({id:e,fileKey:r,deletedVariants:i?Object.keys(i).length:0},`Media deleted`)}async getUrl(e){let t=await(await this.getAdmin()).findById(e);if(!t)throw Error(`Media not found: ${e}`);return this.storage.getUrl(t.fileKey)}async getUrls(e){if(e.length===0)return new Map;let t=await this.getAdmin(),{schemaRegistry:n}=await import(`@murumets-ee/db`),{inArray:r}=await import(`drizzle-orm`),i=n.get(`media`);if(!i)return new Map;let a=await t.findMany({where:r(i.id,e),limit:e.length}),o=new Map;return await Promise.all(a.map(async e=>{let t=await this.storage.getUrl(e.fileKey);o.set(e.id,t)})),o}async getVariantUrl(e,t){let n=await(await this.getAdmin()).findById(e);if(!n)return null;let r=n.fileKey,i=(await this.resolveImageStyles())[t];if(i){let e=a(r,t,i.format??`webp`);try{return await this.storage.getUrl(e)}catch{}}try{return await this.storage.getUrl(r)}catch{return null}}async getVariantUrls(e,t){if(e.length===0)return new Map;let n=await this.getAdmin(),{schemaRegistry:r}=await import(`@murumets-ee/db`),{inArray:i}=await import(`drizzle-orm`),o=r.get(`media`);if(!o)return new Map;let s=await n.findMany({where:i(o.id,e),limit:e.length}),c=(await this.resolveImageStyles())[t],l=new Map;return await Promise.all(s.map(async e=>{if(c){let n=a(e.fileKey,t,c.format??`webp`);try{let t=await this.storage.getUrl(n);l.set(e.id,t);return}catch{}}try{let t=await this.storage.getUrl(e.fileKey);l.set(e.id,t)}catch{}})),l}};function s(e){return e.startsWith(`image/`)?`image`:e.startsWith(`video/`)?`video`:e.startsWith(`audio/`)?`audio`:e===`application/pdf`||e.startsWith(`application/msword`)||e.startsWith(`application/vnd.`)?`document`:`other`}function c(e){return e.replace(/\.[^.]+$/,``).replace(/[-_]/g,` `)}async function l(e){let{getApp:t}=await import(`@murumets-ee/core`),n=t();return new o({db:n.db.readWrite,storage:e,logger:n.logger.child({media:!0})})}let u=null;function d(){return u||=(async()=>{let{getApp:e}=await import(`@murumets-ee/core`),{createStorageClient:t}=await import(`@murumets-ee/storage`),{getStorageConfig:n}=await import(`@murumets-ee/storage/plugin`),r=e(),i=t(n(),{app:r});return new o({db:r.db.readWrite,storage:i,logger:r.logger.child({media:!0})})})(),u}export{o as MediaClient,l as createMediaClient,d as getMediaClient};
1
+ import{t as e}from"./entity-TVTU7wS3.mjs";import{i as t,r as n,t as r}from"./variant-key-gVMhzKyv.mjs";import"server-only";var i=class{admin;storage;imageStyles;constructor(e){this.admin=e.admin,this.storage=e.storage,this.imageStyles=e.imageStyles??null}async resolveImageStyles(){if(this.imageStyles)return this.imageStyles;try{let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings.mjs`),{getApp:n}=await import(`@murumets-ee/core`),r=await e(t,{app:n()}).get(`imageStyles`);if(r&&Object.keys(r).length>0)return this.imageStyles=r,r}catch{}try{let{getMediaConfig:e}=await import(`./plugin.mjs`);return this.imageStyles=e().imageStyles,this.imageStyles}catch{let e={thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}};return this.imageStyles=e,e}}invalidateImageStylesCache(){this.imageStyles=null}async upload(e,i){let s=await this.storage.upload(e,{filename:i.filename,mimeType:i.mimeType,size:i.size,visibility:i.visibility,uploadedBy:i.uploadedBy}),c=i.width??null,l=i.height??null,u={};if(e instanceof Buffer&&n(i.mimeType))try{let n=await t(e,await this.resolveImageStyles());c=n.width,l=n.height;let a=s.visibility;await Promise.all([...n.variants.entries()].map(async([e,t])=>{let n=r(s.key,e,t.format);try{await this.storage.upload(t.buffer,{key:n,filename:`${e}_${i.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:a,metadata:{variantOf:s.key,style:e},uploadedBy:i.uploadedBy}),u[e]=n}catch{}})),Object.keys(u).length>0&&await this.storage.updateMetadata(s.key,{metadata:{...s.metadata??{},variants:u}}).catch(()=>{})}catch{}let d=a(i.mimeType);try{return{media:await this.admin.create({title:i.title??o(i.filename),alt:i.alt??null,description:i.description??null,fileKey:s.key,filename:i.filename,mimeType:i.mimeType,size:i.size,width:c,height:l,mediaType:d}),url:await this.storage.getUrl(s.key)}}catch(e){for(let e of Object.values(u))await this.storage.delete(e).catch(()=>{});throw await this.storage.delete(s.key).catch(()=>{}),e}}async findById(e,t){return this.admin.findById(e,t)}async findMany(e){let{schemaRegistry:t}=await import(`@murumets-ee/db`),{and:n,asc:r,desc:i,eq:a,ilike:o,or:s,sql:c}=await import(`drizzle-orm`),l=t.get(`media`);if(!l)throw Error(`Media schema not registered. Is the media() plugin loaded?`);let u=[];if(e?.mediaType&&u.push(a(l.mediaType,e.mediaType)),e?.mimeTypePrefix){let t=e.mimeTypePrefix.replace(/[\\%_]/g,`\\$&`);u.push(o(l.mimeType,`${t}%`))}if(e?.search){let t=`%${e.search.replace(/[\\%_]/g,`\\$&`)}%`;u.push(s(o(l.filename,t),c`${l.fields} ->> 'title' ILIKE ${t}`))}let d=e?.limit??50,f=e?.offset??0,p=u.length>0?n(...u):void 0,m=await this.admin.count({where:p}),h=e?.orderBy===`filename`?l.filename:l.createdAt,g=(e?.orderDirection??`desc`)===`asc`?r:i;return{items:await this.admin.findMany({where:p,limit:d,offset:f,orderBy:g(h)}),total:m,limit:d,offset:f}}async update(e,t){return this.admin.update(e,t)}async delete(e){let t=await this.admin.findById(e);if(!t)throw Error(`Media not found: ${e}`);let n=t.fileKey;await this.admin.delete(e);let r=(await this.storage.getMetadata(n))?.metadata?.variants;if(r)for(let e of Object.values(r))await this.storage.delete(e).catch(()=>{});await this.storage.delete(n).catch(()=>{})}async getUrl(e){let t=await this.admin.findById(e);if(!t)throw Error(`Media not found: ${e}`);return this.storage.getUrl(t.fileKey)}async getUrls(e){if(e.length===0)return new Map;let{schemaRegistry:t}=await import(`@murumets-ee/db`),{inArray:n}=await import(`drizzle-orm`),r=t.get(`media`);if(!r)return new Map;let i=await this.admin.findMany({where:n(r.id,e),limit:e.length}),a=new Map;return await Promise.all(i.map(async e=>{let t=await this.storage.getUrl(e.fileKey);a.set(e.id,t)})),a}async getVariantUrl(e,t){let n=await this.admin.findById(e);if(!n)return null;let i=n.fileKey,a=(await this.resolveImageStyles())[t];if(a){let e=r(i,t,a.format??`webp`);try{return await this.storage.getUrl(e)}catch{}}try{return await this.storage.getUrl(i)}catch{return null}}async getVariantUrls(e,t){if(e.length===0)return new Map;let{schemaRegistry:n}=await import(`@murumets-ee/db`),{inArray:i}=await import(`drizzle-orm`),a=n.get(`media`);if(!a)return new Map;let o=await this.admin.findMany({where:i(a.id,e),limit:e.length}),s=(await this.resolveImageStyles())[t],c=new Map;return await Promise.all(o.map(async e=>{if(s){let n=r(e.fileKey,t,s.format??`webp`);try{let t=await this.storage.getUrl(n);c.set(e.id,t);return}catch{}}try{let t=await this.storage.getUrl(e.fileKey);c.set(e.id,t)}catch{}})),c}};function a(e){return e.startsWith(`image/`)?`image`:e.startsWith(`video/`)?`video`:e.startsWith(`audio/`)?`audio`:e===`application/pdf`||e.startsWith(`application/msword`)||e.startsWith(`application/vnd.`)?`document`:`other`}function o(e){return e.replace(/\.[^.]+$/,``).replace(/[-_]/g,` `)}async function s(t){let{createAdminClient:n}=await import(`@murumets-ee/core/clients`);return new i({admin:n(e),storage:t})}let c=null;async function l(){return c||=(async()=>{let{getApp:e}=await import(`@murumets-ee/core`),{createStorageClient:t}=await import(`@murumets-ee/storage`),{getStorageConfig:n}=await import(`@murumets-ee/storage/plugin`),r=e();return t(n(),{app:r})})(),c}async function u(){let{createAdminClient:t}=await import(`@murumets-ee/core/clients`),n=await l();return new i({admin:t(e),storage:n})}export{i as MediaClient,s as createMediaClient,u as getMediaClient};
2
2
  //# sourceMappingURL=client.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"client.mjs","names":[],"sources":["../src/process-image.ts","../src/variant-key.ts","../src/client.ts"],"sourcesContent":["/**\n * Server-side image processing via Sharp.\n *\n * Pure processing module — no DB or storage dependencies.\n * Takes a buffer and image styles, returns metadata + variant buffers.\n */\n\nimport sharp from 'sharp'\nimport type { ImageStyle } from './types.js'\n\n/** Result of processing an image through Sharp */\nexport interface ProcessedImage {\n /** Original image width in pixels */\n width: number\n /** Original image height in pixels */\n height: number\n /** Generated variant buffers keyed by style name */\n variants: Map<string, ProcessedVariant>\n}\n\n/** A single processed variant */\nexport interface ProcessedVariant {\n buffer: Buffer\n format: string\n mimeType: string\n width: number\n height: number\n}\n\n/** MIME types that should NOT be processed (vectors, animations) */\nconst SKIP_MIME_TYPES = new Set(['image/svg+xml', 'image/gif'])\n\n/**\n * Check if a MIME type is eligible for Sharp processing.\n * Returns false for SVG, GIF, and non-image types.\n */\nexport function isProcessableImage(mimeType: string): boolean {\n return mimeType.startsWith('image/') && !SKIP_MIME_TYPES.has(mimeType)\n}\n\n/**\n * Extract image dimensions and generate resized variants.\n *\n * @param buffer - Original image file as a Buffer\n * @param styles - Named image style presets to generate\n * @returns Metadata (width/height) and variant buffers\n */\nexport async function processImage(\n buffer: Buffer,\n styles: Record<string, ImageStyle>,\n): Promise<ProcessedImage> {\n const meta = await sharp(buffer).metadata()\n const width = meta.width ?? 0\n const height = meta.height ?? 0\n\n const variants = new Map<string, ProcessedVariant>()\n\n const entries = Object.entries(styles)\n await Promise.all(\n entries.map(async ([name, style]) => {\n const fmt = style.format ?? 'webp'\n const quality = style.quality ?? 80\n const fit = style.fit ?? 'cover'\n\n const resized = sharp(buffer).resize({\n width: style.width,\n height: style.height,\n fit,\n withoutEnlargement: true,\n })\n\n const { data: variantBuffer, info } = await resized[fmt]({ quality }).toBuffer({\n resolveWithObject: true,\n })\n\n variants.set(name, {\n buffer: variantBuffer,\n format: fmt,\n mimeType: `image/${fmt}`,\n width: info.width,\n height: info.height,\n })\n }),\n )\n\n return { width, height, variants }\n}\n\n/**\n * Extract only image dimensions (no variant generation).\n * Useful for getting width/height when styles are empty.\n */\nexport async function getImageDimensions(\n buffer: Buffer,\n): Promise<{ width: number; height: number }> {\n const meta = await sharp(buffer).metadata()\n return { width: meta.width ?? 0, height: meta.height ?? 0 }\n}\n","/**\n * Variant key derivation convention.\n *\n * Given an original storage key and a style name, produces a deterministic\n * variant key in the same directory.\n *\n * Convention:\n * Original: uploads/2026/02/{uuid}/photo.jpg\n * Variant: uploads/2026/02/{uuid}/thumbnail_photo.webp\n */\n\n/**\n * Derive a variant storage key from the original key + style name.\n *\n * @param originalKey - The original file's storage key\n * @param styleName - The image style name (e.g., 'thumbnail', 'medium')\n * @param format - The variant output format (default: 'webp')\n * @returns The derived variant key\n */\nexport function deriveVariantKey(originalKey: string, styleName: string, format = 'webp'): string {\n const lastSlash = originalKey.lastIndexOf('/')\n const dir = originalKey.substring(0, lastSlash)\n const filename = originalKey.substring(lastSlash + 1)\n const baseName = filename.replace(/\\.[^.]+$/, '')\n return `${dir}/${styleName}_${baseName}.${format}`\n}\n","/**\n * MediaClient — wraps AdminClient + StorageClient for media management.\n *\n * Usage:\n * import { createMediaClient } from '@murumets-ee/media/client'\n * const media = await createMediaClient(storageClient)\n * const result = await media.upload(buffer, { filename: 'photo.jpg', mimeType: 'image/jpeg', size: 12345 })\n */\n\nimport 'server-only'\n\nimport type { Logger } from '@murumets-ee/core'\nimport type { FieldConfig, FieldToTS } from '@murumets-ee/entity'\nimport type { AdminClient } from '@murumets-ee/entity/admin'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { Media } from './entity.js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type {\n ImageStyle,\n MediaListOptions,\n MediaListResult,\n MediaRecord,\n MediaType,\n MediaUploadOptions,\n MediaUploadResult,\n} from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\n/** Value type that any entity field can hold */\ntype EntityFieldValue = FieldToTS<FieldConfig> | null | undefined\n\nexport interface MediaClientConfig {\n db: PostgresJsDatabase\n storage: StorageClient\n logger?: Logger\n /** Image styles to generate on upload. Loaded from plugin config if not provided. */\n imageStyles?: Record<string, ImageStyle>\n}\n\nexport class MediaClient {\n private db: PostgresJsDatabase\n private storage: StorageClient\n private logger?: Logger\n private admin: AdminClient | null = null\n private imageStyles: Record<string, ImageStyle> | null\n\n constructor(config: MediaClientConfig) {\n this.db = config.db\n this.storage = config.storage\n this.logger = config.logger\n this.imageStyles = config.imageStyles ?? null\n }\n\n private async getAdmin(): Promise<AdminClient> {\n if (!this.admin) {\n const { AdminClient } = await import('@murumets-ee/entity/admin')\n this.admin = new AdminClient({\n entity: Media,\n db: this.db,\n logger: this.logger,\n })\n }\n return this.admin\n }\n\n /**\n * Resolve image styles: settings DB → plugin config → hardcoded defaults.\n * Result is cached; call `invalidateImageStylesCache()` after settings update.\n */\n private async resolveImageStyles(): Promise<Record<string, ImageStyle>> {\n if (this.imageStyles) return this.imageStyles\n\n // 1. Try settings DB (source of truth after first boot)\n try {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('./image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const stored = await settingsClient.get('imageStyles')\n if (stored && Object.keys(stored).length > 0) {\n this.imageStyles = stored\n return stored\n }\n } catch {\n // Settings not available — fall through to plugin config\n }\n\n // 2. Fall back to plugin config singleton\n try {\n const { getMediaConfig } = await import('./plugin.js')\n const config = getMediaConfig()\n this.imageStyles = config.imageStyles\n return this.imageStyles\n } catch {\n // Plugin not initialized — hardcoded defaults\n const defaults: Record<string, ImageStyle> = {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n }\n this.imageStyles = defaults\n return defaults\n }\n }\n\n /** Clear cached styles so next access re-reads from settings DB. */\n invalidateImageStylesCache(): void {\n this.imageStyles = null\n }\n\n // ---------------------------------------------------------------\n // Upload — the key convenience method\n // ---------------------------------------------------------------\n\n /**\n * Upload a file and create a media entity record in one step.\n * 1. Uploads original to storage (StorageClient)\n * 2. If image: extracts dimensions via Sharp + generates variants\n * 3. Creates media entity record (AdminClient)\n * 4. Returns the media record + URL\n *\n * Rolls back storage upload if entity creation fails.\n * Variant generation failures are logged but don't fail the upload.\n */\n async upload(\n data: Buffer | ReadableStream<Uint8Array>,\n options: MediaUploadOptions,\n ): Promise<MediaUploadResult> {\n // 1. Upload original file to storage\n const fileRecord = await this.storage.upload(data, {\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n visibility: options.visibility,\n uploadedBy: options.uploadedBy,\n })\n\n // 2. Image processing — extract dimensions + generate variants\n let width = options.width ?? null\n let height = options.height ?? null\n const variantKeys: Record<string, string> = {}\n\n if (data instanceof Buffer && isProcessableImage(options.mimeType)) {\n try {\n const styles = await this.resolveImageStyles()\n const processed = await processImage(data, styles)\n\n // Set dimensions from Sharp metadata\n width = processed.width\n height = processed.height\n\n // Upload each variant to storage\n const visibility = fileRecord.visibility\n await Promise.all(\n [...processed.variants.entries()].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(fileRecord.key, styleName, variant.format)\n try {\n await this.storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${options.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: fileRecord.key, style: styleName },\n uploadedBy: options.uploadedBy,\n })\n variantKeys[styleName] = vKey\n this.logger?.debug(\n { style: styleName, key: vKey, width: variant.width, height: variant.height },\n 'Variant uploaded',\n )\n } catch (variantErr: unknown) {\n this.logger?.warn(\n { style: styleName, key: vKey, error: variantErr },\n 'Failed to upload variant (non-fatal)',\n )\n }\n }),\n )\n\n // Store variant keys in original file's metadata\n if (Object.keys(variantKeys).length > 0) {\n await this.storage\n .updateMetadata(fileRecord.key, {\n metadata: {\n ...(fileRecord.metadata ?? {}),\n variants: variantKeys,\n },\n })\n .catch((metaErr: unknown) => {\n this.logger?.warn(\n { key: fileRecord.key, error: metaErr },\n 'Failed to update original file metadata with variant keys (non-fatal)',\n )\n })\n }\n\n this.logger?.info({ width, height, variants: Object.keys(variantKeys) }, 'Image processed')\n } catch (processErr: unknown) {\n this.logger?.warn(\n { filename: options.filename, error: processErr },\n 'Image processing failed (non-fatal, original saved)',\n )\n }\n }\n\n // 3. Create media entity record\n const mediaType = deriveMediaType(options.mimeType)\n const admin = await this.getAdmin()\n\n try {\n const entityRecord = await admin.create({\n title: options.title ?? deriveTitle(options.filename),\n alt: options.alt ?? null,\n description: options.description ?? null,\n fileKey: fileRecord.key,\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n width,\n height,\n mediaType,\n } as Record<string, EntityFieldValue>)\n\n // 4. Get URL\n const url = await this.storage.getUrl(fileRecord.key)\n\n this.logger?.info(\n { id: entityRecord.id, fileKey: fileRecord.key, mediaType },\n 'Media uploaded',\n )\n\n return {\n media: entityRecord as unknown as MediaRecord,\n url,\n }\n } catch (error) {\n // Rollback: delete variants + original if entity creation fails\n this.logger?.error(\n { fileKey: fileRecord.key, error },\n 'Media entity creation failed, rolling back storage uploads',\n )\n\n // Delete variants first (best-effort)\n for (const vKey of Object.values(variantKeys)) {\n await this.storage.delete(vKey).catch(() => {})\n }\n\n // Delete original\n await this.storage.delete(fileRecord.key).catch((rollbackErr: unknown) => {\n this.logger?.error(\n { fileKey: fileRecord.key, error: rollbackErr },\n 'Storage rollback also failed',\n )\n })\n throw error\n }\n }\n\n // ---------------------------------------------------------------\n // CRUD delegation\n // ---------------------------------------------------------------\n\n async findById(id: string, options?: { locale?: string }): Promise<MediaRecord | null> {\n const admin = await this.getAdmin()\n const result = await admin.findById(id, options)\n return result as unknown as MediaRecord | null\n }\n\n async findMany(options?: MediaListOptions): Promise<MediaListResult> {\n const admin = await this.getAdmin()\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { and, asc, desc, eq, ilike, sql } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered. Is the media() plugin loaded?')\n\n // Build where conditions\n const conditions = []\n\n if (options?.mediaType) {\n conditions.push(eq(table.mediaType, options.mediaType))\n }\n if (options?.mimeTypePrefix) {\n conditions.push(ilike(table.mimeType, `${options.mimeTypePrefix}%`))\n }\n if (options?.search) {\n const pattern = `%${options.search}%`\n conditions.push(\n sql`(${ilike(table.filename, pattern)} OR ${table.fields} ->> 'title' ILIKE ${pattern})`,\n )\n }\n\n const limit = options?.limit ?? 50\n const offset = options?.offset ?? 0\n const whereClause = conditions.length > 0 ? and(...conditions) : undefined\n\n // Count total\n const [countResult] = await this.db\n .select({ count: sql<number>`count(*)::int` })\n .from(table)\n .where(whereClause)\n\n // Fetch items via AdminClient for proper DTO shaping\n const orderField = options?.orderBy === 'filename' ? table.filename : table.createdAt\n const orderFn = (options?.orderDirection ?? 'desc') === 'asc' ? asc : desc\n\n const items = await admin.findMany({\n where: whereClause,\n limit,\n offset,\n orderBy: orderFn(orderField),\n })\n\n return {\n items: items as unknown as MediaRecord[],\n total: countResult?.count ?? 0,\n limit,\n offset,\n }\n }\n\n async update(\n id: string,\n data: { title?: string; alt?: string; description?: string },\n ): Promise<MediaRecord> {\n const admin = await this.getAdmin()\n const result = await admin.update(id, data as Record<string, EntityFieldValue>)\n return result as unknown as MediaRecord\n }\n\n /**\n * Delete a media entity, its variants, and its original file in storage.\n */\n async delete(id: string): Promise<void> {\n const admin = await this.getAdmin()\n const record = await admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n const fileKey = (record as unknown as MediaRecord).fileKey\n\n // 1. Delete entity first (ref checking via entity_refs happens here)\n await admin.delete(id)\n\n // 2. Look up original file record for variant metadata\n const fileRecord = await this.storage.getMetadata(fileKey)\n const variants = (fileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n\n // 3. Delete variants (best-effort)\n if (variants) {\n for (const vKey of Object.values(variants)) {\n await this.storage.delete(vKey).catch((err: unknown) => {\n this.logger?.warn({ variantKey: vKey, error: err }, 'Failed to delete variant file')\n })\n }\n }\n\n // 4. Delete original file (best-effort)\n await this.storage.delete(fileKey).catch((err: unknown) => {\n this.logger?.error(\n { id, fileKey, error: err },\n 'Failed to delete file from storage after entity deletion',\n )\n })\n\n this.logger?.info(\n { id, fileKey, deletedVariants: variants ? Object.keys(variants).length : 0 },\n 'Media deleted',\n )\n }\n\n // ---------------------------------------------------------------\n // URL resolution\n // ---------------------------------------------------------------\n\n /**\n * Get URL for a media entity by its ID.\n * Resolves entity -> fileKey -> storage URL.\n */\n async getUrl(id: string): Promise<string> {\n const admin = await this.getAdmin()\n const record = await admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n return this.storage.getUrl((record as unknown as MediaRecord).fileKey)\n }\n\n /**\n * Get URLs for multiple media entities (batch).\n * Returns a Map of mediaId -> url.\n */\n async getUrls(ids: string[]): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const admin = await this.getAdmin()\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n (records as unknown as MediaRecord[]).map(async (record) => {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n }),\n )\n\n return urlMap\n }\n\n /**\n * Get variant URL for a specific image style.\n * Falls back to original URL if the variant doesn't exist.\n *\n * @param id - Media entity ID\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns The variant URL, or original URL as fallback, or null if media not found\n */\n async getVariantUrl(id: string, styleName: string): Promise<string | null> {\n const admin = await this.getAdmin()\n const record = await admin.findById(id)\n if (!record) return null\n\n const fileKey = (record as unknown as MediaRecord).fileKey\n\n // Try variant key first\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n if (style) {\n const vKey = deriveVariantKey(fileKey, styleName, style.format ?? 'webp')\n try {\n return await this.storage.getUrl(vKey)\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n return await this.storage.getUrl(fileKey)\n } catch {\n return null\n }\n }\n\n /**\n * Get variant URLs for multiple media entities (batch).\n * Falls back to original URL per item if the variant doesn't exist.\n *\n * @param ids - Media entity IDs\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns Map of mediaId -> variant URL (or original URL as fallback)\n */\n async getVariantUrls(ids: string[], styleName: string): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const admin = await this.getAdmin()\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n (records as unknown as MediaRecord[]).map(async (record) => {\n // Try variant URL\n if (style) {\n const vKey = deriveVariantKey(record.fileKey, styleName, style.format ?? 'webp')\n try {\n const url = await this.storage.getUrl(vKey)\n urlMap.set(record.id, url)\n return\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n } catch {\n // Skip — no URL available\n }\n }),\n )\n\n return urlMap\n }\n}\n\n// ---------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------\n\nfunction deriveMediaType(mimeType: string): MediaType {\n if (mimeType.startsWith('image/')) return 'image'\n if (mimeType.startsWith('video/')) return 'video'\n if (mimeType.startsWith('audio/')) return 'audio'\n if (\n mimeType === 'application/pdf' ||\n mimeType.startsWith('application/msword') ||\n mimeType.startsWith('application/vnd.')\n ) {\n return 'document'\n }\n return 'other'\n}\n\nfunction deriveTitle(filename: string): string {\n const withoutExt = filename.replace(/\\.[^.]+$/, '')\n return withoutExt.replace(/[-_]/g, ' ')\n}\n\n/**\n * Factory — creates a MediaClient with an explicit StorageClient.\n * Must be called after createApp().\n */\nexport async function createMediaClient(storage: StorageClient): Promise<MediaClient> {\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n return new MediaClient({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n })\n}\n\n// ---------------------------------------------------------------------------\n// Lazy singleton — auto-discovers storage config\n// ---------------------------------------------------------------------------\n\nlet _clientPromise: Promise<MediaClient> | null = null\n\n/**\n * Lazy singleton — returns a shared MediaClient instance.\n * Auto-discovers storage configuration from the storage plugin.\n * Must be called after createApp().\n */\nexport function getMediaClient(): Promise<MediaClient> {\n if (!_clientPromise) {\n _clientPromise = (async () => {\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n\n const app = getApp()\n const config = getStorageConfig()\n const storage = createStorageClient(config, { app })\n\n return new MediaClient({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n })\n })()\n }\n return _clientPromise\n}\n"],"mappings":"mFA8BA,MAAM,EAAkB,IAAI,IAAI,CAAC,gBAAiB,YAAY,CAAC,CAM/D,SAAgB,EAAmB,EAA2B,CAC5D,OAAO,EAAS,WAAW,SAAS,EAAI,CAAC,EAAgB,IAAI,EAAS,CAUxE,eAAsB,EACpB,EACA,EACyB,CACzB,IAAM,EAAO,MAAM,EAAM,EAAO,CAAC,UAAU,CACrC,EAAQ,EAAK,OAAS,EACtB,EAAS,EAAK,QAAU,EAExB,EAAW,IAAI,IAEf,EAAU,OAAO,QAAQ,EAAO,CA4BtC,OA3BA,MAAM,QAAQ,IACZ,EAAQ,IAAI,MAAO,CAAC,EAAM,KAAW,CACnC,IAAM,EAAM,EAAM,QAAU,OACtB,EAAU,EAAM,SAAW,GAC3B,EAAM,EAAM,KAAO,QASnB,CAAE,KAAM,EAAe,QAAS,MAPtB,EAAM,EAAO,CAAC,OAAO,CACnC,MAAO,EAAM,MACb,OAAQ,EAAM,OACd,MACA,mBAAoB,GACrB,CAAC,CAEkD,GAAK,CAAE,UAAS,CAAC,CAAC,SAAS,CAC7E,kBAAmB,GACpB,CAAC,CAEF,EAAS,IAAI,EAAM,CACjB,OAAQ,EACR,OAAQ,EACR,SAAU,SAAS,IACnB,MAAO,EAAK,MACZ,OAAQ,EAAK,OACd,CAAC,EACF,CACH,CAEM,CAAE,QAAO,SAAQ,WAAU,CCl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AAS,QACtC,EAAS,WAAW,SAAS,CAAS,QAExC,IAAa,mBACb,EAAS,WAAW,qBAAqB,EACzC,EAAS,WAAW,mBAAmB,CAEhC,WAEF,QAGT,SAAS,EAAY,EAA0B,CAE7C,OADmB,EAAS,QAAQ,WAAY,GAAG,CACjC,QAAQ,QAAS,IAAI,CAOzC,eAAsB,EAAkB,EAA8C,CACpF,GAAM,CAAE,UAAW,MAAM,OAAO,qBAC1B,EAAM,GAAQ,CACpB,OAAO,IAAI,EAAY,CACrB,GAAI,EAAI,GAAG,UACX,UACA,OAAQ,EAAI,OAAO,MAAM,CAAE,MAAO,GAAM,CAAC,CAC1C,CAAC,CAOJ,IAAI,EAA8C,KAOlD,SAAgB,GAAuC,CAkBrD,MAjBA,CACE,KAAkB,SAAY,CAC5B,GAAM,CAAE,UAAW,MAAM,OAAO,qBAC1B,CAAE,uBAAwB,MAAM,OAAO,wBACvC,CAAE,oBAAqB,MAAM,OAAO,+BAEpC,EAAM,GAAQ,CAEd,EAAU,EADD,GAAkB,CACW,CAAE,MAAK,CAAC,CAEpD,OAAO,IAAI,EAAY,CACrB,GAAI,EAAI,GAAG,UACX,UACA,OAAQ,EAAI,OAAO,MAAM,CAAE,MAAO,GAAM,CAAC,CAC1C,CAAC,IACA,CAEC"}
1
+ {"version":3,"file":"client.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["/**\n * MediaClient — wraps AdminClient + StorageClient for media management.\n *\n * Usage:\n * import { createMediaClient } from '@murumets-ee/media/client'\n * const media = await createMediaClient(storageClient)\n * const result = await media.upload(buffer, { filename: 'photo.jpg', mimeType: 'image/jpeg', size: 12345 })\n */\n\nimport 'server-only'\n\nimport type { AdminClient } from '@murumets-ee/entity/admin'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport { Media } from './entity.js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type {\n ImageStyle,\n MediaListOptions,\n MediaListResult,\n MediaRecord,\n MediaType,\n MediaUploadOptions,\n MediaUploadResult,\n} from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\ntype MediaFields = typeof Media.allFields\n\nexport interface MediaClientConfig {\n admin: AdminClient<MediaFields>\n storage: StorageClient\n /** Image styles to generate on upload. Loaded from plugin config if not provided. */\n imageStyles?: Record<string, ImageStyle>\n}\n\nexport class MediaClient {\n private admin: AdminClient<MediaFields>\n private storage: StorageClient\n private imageStyles: Record<string, ImageStyle> | null\n\n constructor(config: MediaClientConfig) {\n this.admin = config.admin\n this.storage = config.storage\n this.imageStyles = config.imageStyles ?? null\n }\n\n /**\n * Resolve image styles: settings DB → plugin config → hardcoded defaults.\n * Result is cached; call `invalidateImageStylesCache()` after settings update.\n */\n private async resolveImageStyles(): Promise<Record<string, ImageStyle>> {\n if (this.imageStyles) return this.imageStyles\n\n // 1. Try settings DB (source of truth after first boot)\n try {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('./image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const stored = await settingsClient.get('imageStyles')\n if (stored && Object.keys(stored).length > 0) {\n this.imageStyles = stored\n return stored\n }\n } catch {\n // Settings not available — fall through to plugin config\n }\n\n // 2. Fall back to plugin config singleton\n try {\n const { getMediaConfig } = await import('./plugin.js')\n const config = getMediaConfig()\n this.imageStyles = config.imageStyles\n return this.imageStyles\n } catch {\n // Plugin not initialized — hardcoded defaults\n const defaults: Record<string, ImageStyle> = {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n }\n this.imageStyles = defaults\n return defaults\n }\n }\n\n /** Clear cached styles so next access re-reads from settings DB. */\n invalidateImageStylesCache(): void {\n this.imageStyles = null\n }\n\n // ---------------------------------------------------------------\n // Upload — the key convenience method\n // ---------------------------------------------------------------\n\n /**\n * Upload a file and create a media entity record in one step.\n * 1. Uploads original to storage (StorageClient)\n * 2. If image: extracts dimensions via Sharp + generates variants\n * 3. Creates media entity record (AdminClient)\n * 4. Returns the media record + URL\n *\n * Rolls back storage upload if entity creation fails.\n * Variant generation failures are logged but don't fail the upload.\n */\n async upload(\n data: Buffer | ReadableStream<Uint8Array>,\n options: MediaUploadOptions,\n ): Promise<MediaUploadResult> {\n // 1. Upload original file to storage\n const fileRecord = await this.storage.upload(data, {\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n visibility: options.visibility,\n uploadedBy: options.uploadedBy,\n })\n\n // 2. Image processing — extract dimensions + generate variants\n let width = options.width ?? null\n let height = options.height ?? null\n const variantKeys: Record<string, string> = {}\n\n if (data instanceof Buffer && isProcessableImage(options.mimeType)) {\n try {\n const styles = await this.resolveImageStyles()\n const processed = await processImage(data, styles)\n\n // Set dimensions from Sharp metadata\n width = processed.width\n height = processed.height\n\n // Upload each variant to storage\n const visibility = fileRecord.visibility\n await Promise.all(\n [...processed.variants.entries()].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(fileRecord.key, styleName, variant.format)\n try {\n await this.storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${options.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: fileRecord.key, style: styleName },\n uploadedBy: options.uploadedBy,\n })\n variantKeys[styleName] = vKey\n } catch {\n // Variant upload failure is non-fatal — original is saved\n }\n }),\n )\n\n // Store variant keys in original file's metadata\n if (Object.keys(variantKeys).length > 0) {\n await this.storage\n .updateMetadata(fileRecord.key, {\n metadata: {\n ...(fileRecord.metadata ?? {}),\n variants: variantKeys,\n },\n })\n .catch(() => {\n // Metadata update failure is non-fatal\n })\n }\n } catch {\n // Image processing failure is non-fatal — original is saved\n }\n }\n\n // 3. Create media entity record\n const mediaType = deriveMediaType(options.mimeType)\n\n try {\n const entityRecord = await this.admin.create({\n title: options.title ?? deriveTitle(options.filename),\n alt: options.alt ?? null,\n description: options.description ?? null,\n fileKey: fileRecord.key,\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n width,\n height,\n mediaType,\n })\n\n // 4. Get URL\n const url = await this.storage.getUrl(fileRecord.key)\n\n return {\n media: entityRecord,\n url,\n }\n } catch (error) {\n // Rollback: delete variants + original if entity creation fails\n\n // Delete variants first (best-effort)\n for (const vKey of Object.values(variantKeys)) {\n await this.storage.delete(vKey).catch(() => {})\n }\n\n // Delete original\n await this.storage.delete(fileRecord.key).catch(() => {\n // Storage rollback failure is already being handled — propagate original error\n })\n throw error\n }\n }\n\n // ---------------------------------------------------------------\n // CRUD delegation\n // ---------------------------------------------------------------\n\n async findById(id: string, options?: { locale?: string }): Promise<MediaRecord | null> {\n return this.admin.findById(id, options)\n }\n\n async findMany(options?: MediaListOptions): Promise<MediaListResult> {\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { and, asc, desc, eq, ilike, or, sql } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered. Is the media() plugin loaded?')\n\n // Build where conditions\n const conditions = []\n\n if (options?.mediaType) {\n conditions.push(eq(table.mediaType, options.mediaType))\n }\n if (options?.mimeTypePrefix) {\n // Escape ILIKE wildcards (%, _) in user input to prevent pattern injection\n const escaped = options.mimeTypePrefix.replace(/[\\\\%_]/g, '\\\\$&')\n conditions.push(ilike(table.mimeType, `${escaped}%`))\n }\n if (options?.search) {\n // Escape ILIKE wildcards in user input, then wrap with %...%\n const escaped = options.search.replace(/[\\\\%_]/g, '\\\\$&')\n const pattern = `%${escaped}%`\n conditions.push(\n or(\n ilike(table.filename, pattern),\n sql`${table.fields} ->> 'title' ILIKE ${pattern}`,\n )!,\n )\n }\n\n const limit = options?.limit ?? 50\n const offset = options?.offset ?? 0\n const whereClause = conditions.length > 0 ? and(...conditions) : undefined\n\n // Count total via AdminClient\n const total = await this.admin.count({ where: whereClause })\n\n // Fetch items via AdminClient for proper DTO shaping\n const orderField = options?.orderBy === 'filename' ? table.filename : table.createdAt\n const orderFn = (options?.orderDirection ?? 'desc') === 'asc' ? asc : desc\n\n const items = await this.admin.findMany({\n where: whereClause,\n limit,\n offset,\n orderBy: orderFn(orderField),\n })\n\n return {\n items,\n total,\n limit,\n offset,\n }\n }\n\n async update(\n id: string,\n data: { title?: string; alt?: string; description?: string },\n ): Promise<MediaRecord> {\n return this.admin.update(id, data)\n }\n\n /**\n * Delete a media entity, its variants, and its original file in storage.\n */\n async delete(id: string): Promise<void> {\n const record = await this.admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n const fileKey = record.fileKey\n\n // 1. Delete entity first (ref checking via entity_refs happens here)\n await this.admin.delete(id)\n\n // 2. Look up original file record for variant metadata\n const fileRecord = await this.storage.getMetadata(fileKey)\n const variants = (fileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n\n // 3. Delete variants (best-effort)\n if (variants) {\n for (const vKey of Object.values(variants)) {\n await this.storage.delete(vKey).catch(() => {\n // Variant deletion failure is non-fatal\n })\n }\n }\n\n // 4. Delete original file (best-effort)\n await this.storage.delete(fileKey).catch(() => {\n // Storage deletion failure is non-fatal — entity is already deleted\n })\n }\n\n // ---------------------------------------------------------------\n // URL resolution\n // ---------------------------------------------------------------\n\n /**\n * Get URL for a media entity by its ID.\n * Resolves entity -> fileKey -> storage URL.\n */\n async getUrl(id: string): Promise<string> {\n const record = await this.admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n return this.storage.getUrl(record.fileKey)\n }\n\n /**\n * Get URLs for multiple media entities (batch).\n * Returns a Map of mediaId -> url.\n */\n async getUrls(ids: string[]): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await this.admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n records.map(async (record) => {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n }),\n )\n\n return urlMap\n }\n\n /**\n * Get variant URL for a specific image style.\n * Falls back to original URL if the variant doesn't exist.\n *\n * @param id - Media entity ID\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns The variant URL, or original URL as fallback, or null if media not found\n */\n async getVariantUrl(id: string, styleName: string): Promise<string | null> {\n const record = await this.admin.findById(id)\n if (!record) return null\n\n const fileKey = record.fileKey\n\n // Try variant key first\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n if (style) {\n const vKey = deriveVariantKey(fileKey, styleName, style.format ?? 'webp')\n try {\n return await this.storage.getUrl(vKey)\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n return await this.storage.getUrl(fileKey)\n } catch {\n return null\n }\n }\n\n /**\n * Get variant URLs for multiple media entities (batch).\n * Falls back to original URL per item if the variant doesn't exist.\n *\n * @param ids - Media entity IDs\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns Map of mediaId -> variant URL (or original URL as fallback)\n */\n async getVariantUrls(ids: string[], styleName: string): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await this.admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n records.map(async (record) => {\n // Try variant URL\n if (style) {\n const vKey = deriveVariantKey(record.fileKey, styleName, style.format ?? 'webp')\n try {\n const url = await this.storage.getUrl(vKey)\n urlMap.set(record.id, url)\n return\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n } catch {\n // Skip — no URL available\n }\n }),\n )\n\n return urlMap\n }\n}\n\n// ---------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------\n\nfunction deriveMediaType(mimeType: string): MediaType {\n if (mimeType.startsWith('image/')) return 'image'\n if (mimeType.startsWith('video/')) return 'video'\n if (mimeType.startsWith('audio/')) return 'audio'\n if (\n mimeType === 'application/pdf' ||\n mimeType.startsWith('application/msword') ||\n mimeType.startsWith('application/vnd.')\n ) {\n return 'document'\n }\n return 'other'\n}\n\nfunction deriveTitle(filename: string): string {\n const withoutExt = filename.replace(/\\.[^.]+$/, '')\n return withoutExt.replace(/[-_]/g, ' ')\n}\n\n/**\n * Factory — creates a MediaClient with an explicit StorageClient.\n * Must be called after createApp().\n */\nexport async function createMediaClient(storage: StorageClient): Promise<MediaClient> {\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const admin = createAdminClient(Media)\n return new MediaClient({ admin, storage })\n}\n\n// ---------------------------------------------------------------------------\n// Per-request MediaClient factory\n// ---------------------------------------------------------------------------\n//\n// NOTE: Storage config is process-global and safe to cache.\n// MediaClient / AdminClient must be built per-request — they carry a\n// context resolver tied to the calling request's user + permissions.\n// A singleton would leak one request's security context across others.\n\nlet _storagePromise: Promise<StorageClient> | null = null\n\nasync function getStorageSingleton(): Promise<StorageClient> {\n if (!_storagePromise) {\n _storagePromise = (async () => {\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n const app = getApp()\n return createStorageClient(getStorageConfig(), { app })\n })()\n }\n return _storagePromise\n}\n\n/**\n * Returns a fresh MediaClient wired to the current request's context.\n * Must be called after createApp(), inside a request context\n * (withAdminContext, runAsCli, etc.).\n *\n * Despite the name, this is NOT cached — the storage config is cached\n * internally but the MediaClient and its AdminClient are rebuilt per call\n * so the security context resolver attaches to the correct request.\n */\nexport async function getMediaClient(): Promise<MediaClient> {\n const { createAdminClient } = await import('@murumets-ee/core/clients')\n const storage = await getStorageSingleton()\n const admin = createAdminClient(Media)\n return new MediaClient({ admin, storage })\n}\n"],"mappings":"2HAm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ACN,UAAS,CAAC,CAY5C,IAAI,EAAiD,KAErD,eAAe,GAA8C,CAU3D,MATA,CACE,KAAmB,SAAY,CAC7B,GAAM,CAAE,UAAW,MAAM,OAAO,qBAC1B,CAAE,uBAAwB,MAAM,OAAO,wBACvC,CAAE,oBAAqB,MAAM,OAAO,+BACpC,EAAM,GAAQ,CACpB,OAAO,EAAoB,GAAkB,CAAE,CAAE,MAAK,CAAC,IACrD,CAEC,EAYT,eAAsB,GAAuC,CAC3D,GAAM,CAAE,qBAAsB,MAAM,OAAO,6BACrC,EAAU,MAAM,GAAqB,CAE3C,OAAO,IAAI,EAAY,CAAE,MADX,EAAkB,EAAM,CACN,UAAS,CAAC"}
@@ -0,0 +1,2 @@
1
+ import{behavior as e,defineEntity as t,field as n}from"@murumets-ee/entity";var r=Object.defineProperty,i=((e,t)=>{let n={};for(var i in e)r(n,i,{get:e[i],enumerable:!0});return t||r(n,Symbol.toStringTag,{value:`Module`}),n})({Media:()=>a});const a=t({name:`media`,fields:{title:n.text({translatable:!0}),alt:n.text({translatable:!0}),description:n.text({translatable:!0}),fileKey:n.text({required:!0,indexed:!0}),filename:n.text({required:!0}),mimeType:n.text({required:!0,indexed:!0}),size:n.number({required:!0,integer:!0}),width:n.number({integer:!0}),height:n.number({integer:!0}),mediaType:n.select({options:[`image`,`video`,`audio`,`document`,`other`],required:!0,indexed:!0})},behaviors:[e.auditable()],scope:`global`,access:{view:`public`,create:`group.editor`,update:`group.editor`,delete:`group.admin`}});export{i as n,a as t};
2
+ //# sourceMappingURL=entity-TVTU7wS3.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"entity-D5P2l05s.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * Pre-defined Media entity.\n *\n * Auto-registered by the media() plugin — users do NOT add this to their entities array.\n * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineConfig({\n * plugins: [storage(), media()],\n * // Media entity is auto-added — no need to list it here\n * })\n * ```\n */\n\nimport { behavior, defineEntity, field } from '@murumets-ee/entity'\n\nexport const Media = defineEntity({\n name: 'media',\n fields: {\n // --- Display metadata (editable, translatable) ---\n title: field.text({ translatable: true }),\n alt: field.text({ translatable: true }),\n description: field.text({ translatable: true }),\n\n // --- File linkage (set on upload, immutable in practice) ---\n /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */\n fileKey: field.text({ required: true, indexed: true }),\n\n // --- File metadata (set on upload) ---\n filename: field.text({ required: true }),\n mimeType: field.text({ required: true, indexed: true }),\n size: field.number({ required: true, integer: true }),\n\n // --- Image-specific metadata (client-measured on upload) ---\n width: field.number({ integer: true }),\n height: field.number({ integer: true }),\n\n // --- Classification (derived from mimeType on upload) ---\n mediaType: field.select({\n options: ['image', 'video', 'audio', 'document', 'other'] as const,\n required: true,\n indexed: true,\n }),\n },\n behaviors: [behavior.auditable()],\n scope: 'global',\n access: {\n view: 'public',\n create: 'group.editor',\n update: 'group.editor',\n delete: 'group.admin',\n },\n})\n"],"mappings":"4EAmBA,MAAa,EAAQ,EAAa,CAChC,KAAM,QACN,OAAQ,CAEN,MAAO,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACzC,IAAK,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACvC,YAAa,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CAI/C,QAAS,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGtD,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,CAAC,CACxC,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CACvD,KAAM,EAAM,OAAO,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGrD,MAAO,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CACtC,OAAQ,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CAGvC,UAAW,EAAM,OAAO,CACtB,QAAS,CAAC,QAAS,QAAS,QAAS,WAAY,QAAQ,CACzD,SAAU,GACV,QAAS,GACV,CAAC,CACH,CACD,UAAW,CAAC,EAAS,WAAW,CAAC,CACjC,MAAO,SACP,OAAQ,CACN,KAAM,SACN,OAAQ,eACR,OAAQ,eACR,OAAQ,cACT,CACF,CAAC"}
1
+ {"version":3,"file":"entity-TVTU7wS3.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * Pre-defined Media entity.\n *\n * Auto-registered by the media() plugin — users do NOT add this to their entities array.\n * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineConfig({\n * plugins: [storage(), media()],\n * // Media entity is auto-added — no need to list it here\n * })\n * ```\n */\n\nimport { behavior, defineEntity, field } from '@murumets-ee/entity'\n\nexport const Media = defineEntity({\n name: 'media',\n fields: {\n // --- Display metadata (editable, translatable) ---\n title: field.text({ translatable: true }),\n alt: field.text({ translatable: true }),\n description: field.text({ translatable: true }),\n\n // --- File linkage (set on upload, immutable in practice) ---\n /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */\n fileKey: field.text({ required: true, indexed: true }),\n\n // --- File metadata (set on upload) ---\n filename: field.text({ required: true }),\n mimeType: field.text({ required: true, indexed: true }),\n size: field.number({ required: true, integer: true }),\n\n // --- Image-specific metadata (client-measured on upload) ---\n width: field.number({ integer: true }),\n height: field.number({ integer: true }),\n\n // --- Classification (derived from mimeType on upload) ---\n mediaType: field.select({\n options: ['image', 'video', 'audio', 'document', 'other'] as const,\n required: true,\n indexed: true,\n }),\n },\n behaviors: [behavior.auditable()],\n scope: 'global',\n access: {\n view: 'public',\n create: 'group.editor',\n update: 'group.editor',\n delete: 'group.admin',\n },\n})\n"],"mappings":"iPAmBA,MAAa,EAAQ,EAAa,CAChC,KAAM,QACN,OAAQ,CAEN,MAAO,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACzC,IAAK,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACvC,YAAa,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CAI/C,QAAS,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGtD,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,CAAC,CACxC,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CACvD,KAAM,EAAM,OAAO,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGrD,MAAO,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CACtC,OAAQ,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CAGvC,UAAW,EAAM,OAAO,CACtB,QAAS,CAAC,QAAS,QAAS,QAAS,WAAY,QAAQ,CACzD,SAAU,GACV,QAAS,GACV,CAAC,CACH,CACD,UAAW,CAAC,EAAS,WAAW,CAAC,CACjC,MAAO,SACP,OAAQ,CACN,KAAM,SACN,OAAQ,eACR,OAAQ,eACR,OAAQ,cACT,CACF,CAAC"}
@@ -1,4 +1,4 @@
1
- import { t as ImageStyle } from "./types-BV_pOm23.mjs";
1
+ import { t as ImageStyle } from "./types-BBAbJooM.mjs";
2
2
  import * as _$_murumets_ee_settings0 from "@murumets-ee/settings";
3
3
 
4
4
  //#region src/image-styles-settings.d.ts
@@ -1 +1 @@
1
- {"version":3,"file":"image-styles.d.mts","names":[],"sources":["../src/types.ts","../src/image-styles/types.ts","../src/image-styles/image-styles-manager.tsx"],"mappings":";;;;UAkFiB,UAAA;ECtC0B;EDwCzC,KAAA;;EAEA,MAAA;EEvBc;EFyBd,GAAA;;EAEA,MAAA;EEzBA;EF2BA,OAAA;AAAA;;;UC1Fe,wBAAA;EACf,KAAA;EACA,WAAA;EACA,QAAA;EACA,SAAA;EACA,SAAA;EACA,KAAA;EACA,MAAA;EACA,GAAA;EACA,MAAA;EACA,OAAA;EACA,IAAA;EACA,MAAA;EACA,MAAA;EACA,kBAAA;EACA,wBAAA;EACA,WAAA;EACA,QAAA;EACA,UAAA;EACA,qBAAA;EACA,YAAA;EACA,MAAA;EACA,EAAA;AAAA;AAAA,UAGe,4BAAA;EACf,IAAA;EACA,MAAA;EACA,KAAA;EACA,MAAA;EACA,OAAA;EACA,iBAAA;AAAA;AAAA,UAGe,uBAAA;EArBf;EAuBA,aAAA,EAAe,MAAA,SAAe,UAAA;EArB9B;EAuBA,WAAA;EArBA;EAuBA,MAAA,GAAS,wBAAA;EArBT;EAuBA,UAAA,GAAa,4BAAA;AAAA;;;iBCmBC,kBAAA,CAAA;EACd,aAAA;EACA,WAAA;EACA,MAAA,EAAQ,UAAA;EACR;AAAA,GACC,uBAAA,GAAuB,oBAAA,CAAA,GAAA,CAAA,OAAA"}
1
+ {"version":3,"file":"image-styles.d.mts","names":[],"sources":["../src/types.ts","../src/image-styles/types.ts","../src/image-styles/image-styles-manager.tsx"],"mappings":";;;;UA2EiB,UAAA;ECrCA;EDuCf,KAAA;ECrCA;EDuCA,MAAA;ECrCS;EDuCT,GAAA;ECrCa;EDuCb,MAAA;ECvCyC;EDyCzC,OAAA;AAAA;;;UCnFe,wBAAA;EACf,KAAA;EACA,WAAA;EACA,QAAA;EACA,SAAA;EACA,SAAA;EACA,KAAA;EACA,MAAA;EACA,GAAA;EACA,MAAA;EACA,OAAA;EACA,IAAA;EACA,MAAA;EACA,MAAA;EACA,kBAAA;EACA,wBAAA;EACA,WAAA;EACA,QAAA;EACA,UAAA;EACA,qBAAA;EACA,YAAA;EACA,MAAA;EACA,EAAA;AAAA;AAAA,UAGe,4BAAA;EACf,IAAA;EACA,MAAA;EACA,KAAA;EACA,MAAA;EACA,OAAA;EACA,iBAAA;AAAA;AAAA,UAGe,uBAAA;EAtBf;EAwBA,aAAA,EAAe,MAAA,SAAe,UAAA;EAtB9B;EAwBA,WAAA;EAtBA;EAwBA,MAAA,GAAS,wBAAA;EAtBT;EAwBA,UAAA,GAAa,4BAAA;AAAA;;;iBCmBC,kBAAA,CAAA;EACd,aAAA;EACA,WAAA;EACA,MAAA,EAAQ,UAAA;EACR;AAAA,GACC,uBAAA,GAAuB,oBAAA,CAAA,GAAA,CAAA,OAAA"}
package/dist/index.d.mts CHANGED
@@ -1,6 +1,5 @@
1
- import { a as MediaRecord, c as MediaUploadResult, i as MediaPluginConfig, n as MediaListOptions, o as MediaType, r as MediaListResult, s as MediaUploadOptions, t as ImageStyle } from "./types-BV_pOm23.mjs";
1
+ import { a as MediaRecord, c as MediaUploadResult, i as MediaPluginConfig, l as Media, n as MediaListOptions, o as MediaType, r as MediaListResult, s as MediaUploadOptions, t as ImageStyle } from "./types-BBAbJooM.mjs";
2
2
  import { defaultImageStyles, imageStylesSettings } from "./image-styles-settings.mjs";
3
- import * as _$_murumets_ee_entity0 from "@murumets-ee/entity";
4
3
  //#region src/enrich.d.ts
5
4
  /**
6
5
  * Enrich entity list items by resolving media field UUIDs to variant URLs.
@@ -17,65 +16,6 @@ declare function enrichWithMediaUrls(entity: {
17
16
  }>;
18
17
  }, items: Record<string, unknown>[], styleName?: string): Promise<void>;
19
18
  //#endregion
20
- //#region src/entity.d.ts
21
- /**
22
- * Pre-defined Media entity.
23
- *
24
- * Auto-registered by the media() plugin — users do NOT add this to their entities array.
25
- * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).
26
- *
27
- * @example
28
- * ```typescript
29
- * import { media } from '@murumets-ee/media/plugin'
30
- *
31
- * export default defineConfig({
32
- * plugins: [storage(), media()],
33
- * // Media entity is auto-added — no need to list it here
34
- * })
35
- * ```
36
- */
37
- declare const Media: _$_murumets_ee_entity0.Entity<{
38
- id: _$_murumets_ee_entity0.IdField;
39
- } & _$_murumets_ee_entity0.AuditableFields & {
40
- title: _$_murumets_ee_entity0.TextField & {
41
- readonly translatable: true;
42
- };
43
- alt: _$_murumets_ee_entity0.TextField & {
44
- readonly translatable: true;
45
- };
46
- description: _$_murumets_ee_entity0.TextField & {
47
- readonly translatable: true;
48
- }; /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */
49
- fileKey: _$_murumets_ee_entity0.TextField & {
50
- readonly required: true;
51
- readonly indexed: true;
52
- };
53
- filename: _$_murumets_ee_entity0.TextField & {
54
- readonly required: true;
55
- };
56
- mimeType: _$_murumets_ee_entity0.TextField & {
57
- readonly required: true;
58
- readonly indexed: true;
59
- };
60
- size: _$_murumets_ee_entity0.NumberField & {
61
- readonly required: true;
62
- readonly integer: true;
63
- };
64
- width: _$_murumets_ee_entity0.NumberField & {
65
- readonly integer: true;
66
- };
67
- height: _$_murumets_ee_entity0.NumberField & {
68
- readonly integer: true;
69
- };
70
- mediaType: _$_murumets_ee_entity0.SelectField & {
71
- options: readonly ["image", "video", "audio", "document", "other"];
72
- } & {
73
- readonly options: readonly ["image", "video", "audio", "document", "other"];
74
- readonly required: true;
75
- readonly indexed: true;
76
- };
77
- }>;
78
- //#endregion
79
19
  //#region src/picker/types.d.ts
80
20
  /** Lightweight media item for the picker (no server-only deps) */
81
21
  interface MediaPickerItem {
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.mts","names":[],"sources":["../src/enrich.ts","../src/entity.ts","../src/picker/types.ts"],"mappings":";;;;;;;;;;;;;iBA8BsB,mBAAA,CACpB,MAAA;EAAU,SAAA,EAAW,MAAA;IAAiB,IAAA;EAAA;AAAA,GACtC,KAAA,EAAO,MAAA,qBACP,SAAA,YACC,OAAA;;;;;;;;;;AAJH;;;;;;;;;cCXa,KAAA,yBAAK,MAAA;MAoChB,sBAAA,CAAA,OAAA;AAAA;;;;;;;;;KApCW;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;UChBI,eAAA;EACf,EAAA;EACA,KAAA;EACA,GAAA;EACA,QAAA;EACA,QAAA;EACA,IAAA;EACA,KAAA;EACA,MAAA;EACA,SAAA;EACA,GAAA;AAAA;;UAIe,qBAAA;EACf,KAAA,EAAO,eAAA;EACP,KAAA;AAAA;;UAIe,oBAAA;EFUf;EERA,UAAA,GAAa,OAAA;IACX,MAAA;IACA,SAAA;IACA,KAAA;IACA,MAAA;EAAA,MACI,OAAA,CAAQ,qBAAA;EDXH;ECcX,WAAA,GAAc,IAAA,EAAM,IAAA,KAAS,OAAA,CAAQ,eAAA;AAAA"}
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../src/enrich.ts","../src/picker/types.ts"],"mappings":";;;;;;;;;;;ACGA;iBD2BsB,mBAAA,CACpB,MAAA;EAAU,SAAA,EAAW,MAAA;IAAiB,IAAA;EAAA;AAAA,GACtC,KAAA,EAAO,MAAA,qBACP,SAAA,YACC,OAAA;;;;UC/Bc,eAAA;EACf,EAAA;EACA,KAAA;EACA,GAAA;EACA,QAAA;EACA,QAAA;EACA,IAAA;EACA,KAAA;EACA,MAAA;EACA,SAAA;EACA,GAAA;AAAA;;UAIe,qBAAA;EACf,KAAA,EAAO,eAAA;EACP,KAAA;AAAA;;UAIe,oBAAA;EDWd;ECTD,UAAA,GAAa,OAAA;IACX,MAAA;IACA,SAAA;IACA,KAAA;IACA,MAAA;EAAA,MACI,OAAA,CAAQ,qBAAA;EA3BgB;EA8B9B,WAAA,GAAc,IAAA,EAAM,IAAA,KAAS,OAAA,CAAQ,eAAA;AAAA"}
package/dist/index.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{t as e}from"./entity-D5P2l05s.mjs";import{defaultImageStyles as t,imageStylesSettings as n}from"./image-styles-settings.mjs";import"server-only";async function r(e,t,n=`thumbnail`){let r=Object.entries(e.allFields).filter(([,e])=>e.type===`media`).map(([e])=>e);if(r.length===0)return;let i=new Set;for(let e of t)for(let t of r){let n=e[t];typeof n==`string`&&n.length>0&&i.add(n)}if(i.size===0)return;let{getMediaClient:a}=await import(`./client.mjs`),o=await(await a()).getVariantUrls([...i],n);for(let e of t)for(let t of r){let n=e[t];typeof n==`string`&&o.has(n)&&(e[`${t}Url`]=o.get(n))}}export{e as Media,t as defaultImageStyles,r as enrichWithMediaUrls,n as imageStylesSettings};
1
+ import{t as e}from"./entity-TVTU7wS3.mjs";import{defaultImageStyles as t,imageStylesSettings as n}from"./image-styles-settings.mjs";import"server-only";async function r(e,t,n=`thumbnail`){let r=Object.entries(e.allFields).filter(([,e])=>e.type===`media`).map(([e])=>e);if(r.length===0)return;let i=new Set;for(let e of t)for(let t of r){let n=e[t];typeof n==`string`&&n.length>0&&i.add(n)}if(i.size===0)return;let{getMediaClient:a}=await import(`./client.mjs`),o=await(await a()).getVariantUrls([...i],n);for(let e of t)for(let t of r){let n=e[t];typeof n==`string`&&o.has(n)&&(e[`${t}Url`]=o.get(n))}}export{e as Media,t as defaultImageStyles,r as enrichWithMediaUrls,n as imageStylesSettings};
2
2
  //# sourceMappingURL=index.mjs.map
package/dist/plugin.d.mts CHANGED
@@ -1,4 +1,4 @@
1
- import { i as MediaPluginConfig } from "./types-BV_pOm23.mjs";
1
+ import { i as MediaPluginConfig } from "./types-BBAbJooM.mjs";
2
2
  import { Plugin } from "@murumets-ee/core";
3
3
 
4
4
  //#region src/plugin.d.ts
package/dist/plugin.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import{t as e}from"./entity-D5P2l05s.mjs";let t=null;function n(){if(!t)throw Error(`@murumets-ee/media plugin not initialized. Add media() to your plugins array.`);return t}function r(n){let r={acceptedTypes:n?.acceptedTypes??[`image/*`,`video/*`,`audio/*`,`application/pdf`],maxUploadSize:n?.maxUploadSize??50*1024*1024,defaultVisibility:n?.defaultVisibility??`public`,imageStyles:n?.imageStyles??{thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}}};return{name:`@murumets-ee/media`,entities:[e],init:async e=>{if(!e.plugins.has(`@murumets-ee/storage`))throw Error(`@murumets-ee/media requires @murumets-ee/storage plugin. Add storage() before media() in your plugins array.`);if(!e.plugins.has(`@murumets-ee/settings`))throw Error(`@murumets-ee/media requires @murumets-ee/settings plugin. Add settings() before media() in your plugins array.`);t=r;try{let{createSettingsClient:t}=await import(`@murumets-ee/settings`),{imageStylesSettings:n}=await import(`./image-styles-settings.mjs`),i=t(n,{app:e});await i.has(`imageStyles`)||(await i.set(`imageStyles`,r.imageStyles),e.logger.info({styles:Object.keys(r.imageStyles)},`Seeded default image styles to settings DB`))}catch(t){e.logger.warn({error:t},`Failed to seed image styles to settings DB (non-fatal)`)}e.logger.info({acceptedTypes:r.acceptedTypes,maxUploadSize:r.maxUploadSize,defaultVisibility:r.defaultVisibility},`Media plugin initialized`)}}}export{n as getMediaConfig,r as media};
1
+ import{t as e}from"./entity-TVTU7wS3.mjs";let t=null;function n(){if(!t)throw Error(`@murumets-ee/media plugin not initialized. Add media() to your plugins array.`);return t}function r(n){let r={acceptedTypes:n?.acceptedTypes??[`image/*`,`video/*`,`audio/*`,`application/pdf`],maxUploadSize:n?.maxUploadSize??50*1024*1024,defaultVisibility:n?.defaultVisibility??`public`,imageStyles:n?.imageStyles??{thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}}};return{name:`@murumets-ee/media`,entities:[e],init:async e=>{if(!e.plugins.has(`@murumets-ee/storage`))throw Error(`@murumets-ee/media requires @murumets-ee/storage plugin. Add storage() before media() in your plugins array.`);if(!e.plugins.has(`@murumets-ee/settings`))throw Error(`@murumets-ee/media requires @murumets-ee/settings plugin. Add settings() before media() in your plugins array.`);t=r;try{let{createSettingsClient:t}=await import(`@murumets-ee/settings`),{imageStylesSettings:n}=await import(`./image-styles-settings.mjs`),i=t(n,{app:e});await i.has(`imageStyles`)||(await i.set(`imageStyles`,r.imageStyles),e.logger.info({styles:Object.keys(r.imageStyles)},`Seeded default image styles to settings DB`))}catch(t){e.logger.warn({error:t},`Failed to seed image styles to settings DB (non-fatal)`)}e.logger.info({acceptedTypes:r.acceptedTypes,maxUploadSize:r.maxUploadSize,defaultVisibility:r.defaultVisibility},`Media plugin initialized`)}}}export{n as getMediaConfig,r as media};
2
2
  //# sourceMappingURL=plugin.mjs.map
@@ -0,0 +1,65 @@
1
+ import { t as ImageStyle } from "./types-BBAbJooM.mjs";
2
+
3
+ //#region src/process-image.d.ts
4
+ /** Result of processing an image through Sharp */
5
+ interface ProcessedImage {
6
+ /** Original image width in pixels */
7
+ width: number;
8
+ /** Original image height in pixels */
9
+ height: number;
10
+ /** Generated variant buffers keyed by style name */
11
+ variants: Map<string, ProcessedVariant>;
12
+ }
13
+ /** A single processed variant */
14
+ interface ProcessedVariant {
15
+ buffer: Buffer;
16
+ format: string;
17
+ mimeType: string;
18
+ width: number;
19
+ height: number;
20
+ }
21
+ /**
22
+ * Check if a MIME type is eligible for Sharp processing.
23
+ * Returns false for SVG, GIF, and non-image types.
24
+ */
25
+ declare function isProcessableImage(mimeType: string): boolean;
26
+ /**
27
+ * Extract image dimensions and generate resized variants.
28
+ *
29
+ * @param buffer - Original image file as a Buffer
30
+ * @param styles - Named image style presets to generate
31
+ * @returns Metadata (width/height) and variant buffers
32
+ */
33
+ declare function processImage(buffer: Buffer, styles: Record<string, ImageStyle>): Promise<ProcessedImage>;
34
+ /**
35
+ * Extract only image dimensions (no variant generation).
36
+ * Useful for getting width/height when styles are empty.
37
+ */
38
+ declare function getImageDimensions(buffer: Buffer): Promise<{
39
+ width: number;
40
+ height: number;
41
+ }>;
42
+ //#endregion
43
+ //#region src/variant-key.d.ts
44
+ /**
45
+ * Variant key derivation convention.
46
+ *
47
+ * Given an original storage key and a style name, produces a deterministic
48
+ * variant key in the same directory.
49
+ *
50
+ * Convention:
51
+ * Original: uploads/2026/02/{uuid}/photo.jpg
52
+ * Variant: uploads/2026/02/{uuid}/thumbnail_photo.webp
53
+ */
54
+ /**
55
+ * Derive a variant storage key from the original key + style name.
56
+ *
57
+ * @param originalKey - The original file's storage key
58
+ * @param styleName - The image style name (e.g., 'thumbnail', 'medium')
59
+ * @param format - The variant output format (default: 'webp')
60
+ * @returns The derived variant key
61
+ */
62
+ declare function deriveVariantKey(originalKey: string, styleName: string, format?: string): string;
63
+ //#endregion
64
+ export { type ImageStyle, type ProcessedImage, type ProcessedVariant, deriveVariantKey, getImageDimensions, isProcessableImage, processImage };
65
+ //# sourceMappingURL=processing.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"processing.d.mts","names":[],"sources":["../src/process-image.ts","../src/variant-key.ts"],"mappings":";;;;UAWiB,cAAA;EAMf;EAJA,KAAA;EAIsB;EAFtB,MAAA;EAEsC;EAAtC,QAAA,EAAU,GAAA,SAAY,gBAAA;AAAA;;UAIP,gBAAA;EACf,MAAA,EAAQ,MAAA;EACR,MAAA;EACA,QAAA;EACA,KAAA;EACA,MAAA;AAAA;;;AAUF;;iBAAgB,kBAAA,CAAmB,QAAA;;;AAWnC;;;;;iBAAsB,YAAA,CACpB,MAAA,EAAQ,MAAA,EACR,MAAA,EAAQ,MAAA,SAAe,UAAA,IACtB,OAAA,CAAQ,cAAA;;;;;iBA0CW,kBAAA,CACpB,MAAA,EAAQ,MAAA,GACP,OAAA;EAAU,KAAA;EAAe,MAAA;AAAA;;;;;;AAnF5B;;;;;;;;;;;AAUA;;;;iBCFgB,gBAAA,CAAiB,WAAA,UAAqB,SAAA,UAAmB,MAAA"}
@@ -0,0 +1 @@
1
+ import{i as e,n as t,r as n,t as r}from"./variant-key-gVMhzKyv.mjs";export{r as deriveVariantKey,t as getImageDimensions,n as isProcessableImage,e as processImage};
@@ -1,19 +1,14 @@
1
- import { a as MediaRecord } from "./types-BV_pOm23.mjs";
2
- import { Logger } from "@murumets-ee/core";
3
- import { PostgresJsDatabase } from "drizzle-orm/postgres-js";
4
- import { CountOptions, FindByIdOptions, FindManyOptions } from "@murumets-ee/entity/query";
1
+ import { a as MediaRecord, l as Media } from "./types-BBAbJooM.mjs";
2
+ import { CountOptions, FindByIdOptions, FindManyOptions, QueryClient } from "@murumets-ee/entity/query";
5
3
 
6
4
  //#region src/query-client.d.ts
5
+ type MediaFields = typeof Media.allFields;
7
6
  interface MediaQueryClientConfig {
8
- db: PostgresJsDatabase;
9
- logger?: Logger;
7
+ query: QueryClient<MediaFields>;
10
8
  }
11
9
  declare class MediaQueryClient {
12
- private db;
13
- private logger?;
14
10
  private query;
15
11
  constructor(config: MediaQueryClientConfig);
16
- private getQuery;
17
12
  findById(id: string, options?: FindByIdOptions): Promise<MediaRecord | null>;
18
13
  findMany(options?: FindManyOptions): Promise<MediaRecord[]>;
19
14
  count(options?: CountOptions): Promise<number>;
@@ -1 +1 @@
1
- {"version":3,"file":"query-client.d.mts","names":[],"sources":["../src/query-client.ts"],"mappings":";;;;;;UAoBiB,sBAAA;EACf,EAAA,EAAI,kBAAA;EACJ,MAAA,GAAS,MAAA;AAAA;AAAA,cAGE,gBAAA;EAAA,QACH,EAAA;EAAA,QACA,MAAA;EAAA,QACA,KAAA;cAEI,MAAA,EAAQ,sBAAA;EAAA,QAKN,QAAA;EAYR,QAAA,CAAS,EAAA,UAAY,OAAA,GAAU,eAAA,GAAkB,OAAA,CAAQ,WAAA;EAMzD,QAAA,CAAS,OAAA,GAAU,eAAA,GAAkB,OAAA,CAAQ,WAAA;EAM7C,KAAA,CAAM,OAAA,GAAU,YAAA,GAAe,OAAA;AAAA;;;;;iBAUjB,sBAAA,CAAA,GAA0B,OAAA,CAAQ,gBAAA"}
1
+ {"version":3,"file":"query-client.d.mts","names":[],"sources":["../src/query-client.ts"],"mappings":";;;;KAkBK,WAAA,UAAqB,KAAA,CAAM,SAAA;AAAA,UAEf,sBAAA;EACf,KAAA,EAAO,WAAA,CAAY,WAAA;AAAA;AAAA,cAGR,gBAAA;EAAA,QACH,KAAA;cAEI,MAAA,EAAQ,sBAAA;EAId,QAAA,CAAS,EAAA,UAAY,OAAA,GAAU,eAAA,GAAkB,OAAA,CAAQ,WAAA;EAIzD,QAAA,CAAS,OAAA,GAAU,eAAA,GAAkB,OAAA,CAAQ,WAAA;EAI7C,KAAA,CAAM,OAAA,GAAU,YAAA,GAAe,OAAA;AAAA;;;;;iBASjB,sBAAA,CAAA,GAA0B,OAAA,CAAQ,gBAAA"}
@@ -1,2 +1,2 @@
1
- import{t as e}from"./entity-D5P2l05s.mjs";var t=class{db;logger;query=null;constructor(e){this.db=e.db,this.logger=e.logger}async getQuery(){if(!this.query){let{QueryClient:t}=await import(`@murumets-ee/entity/query`);this.query=new t({entity:e,db:this.db,logger:this.logger})}return this.query}async findById(e,t){return await(await this.getQuery()).findById(e,t)}async findMany(e){return await(await this.getQuery()).findMany(e)}async count(e){return(await this.getQuery()).count(e)}};async function n(){let{getApp:e}=await import(`@murumets-ee/core`),n=e();return new t({db:n.db.readOnly,logger:n.logger.child({mediaQuery:!0})})}export{t as MediaQueryClient,n as createMediaQueryClient};
1
+ var e=class{query;constructor(e){this.query=e.query}async findById(e,t){return this.query.findById(e,t)}async findMany(e){return this.query.findMany(e)}async count(e){return this.query.count(e)}};async function t(){let{createQueryClient:t}=await import(`@murumets-ee/core/clients`),{Media:n}=await import(`./entity-TVTU7wS3.mjs`).then(e=>e.n);return new e({query:t(n)})}export{e as MediaQueryClient,t as createMediaQueryClient};
2
2
  //# sourceMappingURL=query-client.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"query-client.mjs","names":[],"sources":["../src/query-client.ts"],"sourcesContent":["/**\n * MediaQueryClient — read-only media client for frontends.\n *\n * Usage:\n * import { createMediaQueryClient } from '@murumets-ee/media/query'\n * const media = await createMediaQueryClient()\n * const image = await media.findById(id)\n */\n\nimport type { Logger } from '@murumets-ee/core'\nimport type {\n CountOptions,\n FindByIdOptions,\n FindManyOptions,\n QueryClient,\n} from '@murumets-ee/entity/query'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { Media } from './entity.js'\nimport type { MediaRecord } from './types.js'\n\nexport interface MediaQueryClientConfig {\n db: PostgresJsDatabase\n logger?: Logger\n}\n\nexport class MediaQueryClient {\n private db: PostgresJsDatabase\n private logger?: Logger\n private query: QueryClient | null = null\n\n constructor(config: MediaQueryClientConfig) {\n this.db = config.db\n this.logger = config.logger\n }\n\n private async getQuery(): Promise<QueryClient> {\n if (!this.query) {\n const { QueryClient } = await import('@murumets-ee/entity/query')\n this.query = new QueryClient({\n entity: Media,\n db: this.db,\n logger: this.logger,\n })\n }\n return this.query\n }\n\n async findById(id: string, options?: FindByIdOptions): Promise<MediaRecord | null> {\n const query = await this.getQuery()\n const result = await query.findById(id, options)\n return result as unknown as MediaRecord | null\n }\n\n async findMany(options?: FindManyOptions): Promise<MediaRecord[]> {\n const query = await this.getQuery()\n const results = await query.findMany(options)\n return results as unknown as MediaRecord[]\n }\n\n async count(options?: CountOptions): Promise<number> {\n const query = await this.getQuery()\n return query.count(options)\n }\n}\n\n/**\n * Factory — creates a MediaQueryClient.\n * Must be called after createApp().\n */\nexport async function createMediaQueryClient(): Promise<MediaQueryClient> {\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n return new MediaQueryClient({\n db: app.db.readOnly,\n logger: app.logger.child({ mediaQuery: true }),\n })\n}\n"],"mappings":"0CAyBA,IAAa,EAAb,KAA8B,CAC5B,GACA,OACA,MAAoC,KAEpC,YAAY,EAAgC,CAC1C,KAAK,GAAK,EAAO,GACjB,KAAK,OAAS,EAAO,OAGvB,MAAc,UAAiC,CAC7C,GAAI,CAAC,KAAK,MAAO,CACf,GAAM,CAAE,eAAgB,MAAM,OAAO,6BACrC,KAAK,MAAQ,IAAI,EAAY,CAC3B,OAAQ,EACR,GAAI,KAAK,GACT,OAAQ,KAAK,OACd,CAAC,CAEJ,OAAO,KAAK,MAGd,MAAM,SAAS,EAAY,EAAwD,CAGjF,OADe,MADD,MAAM,KAAK,UAAU,EACR,SAAS,EAAI,EAAQ,CAIlD,MAAM,SAAS,EAAmD,CAGhE,OADgB,MADF,MAAM,KAAK,UAAU,EACP,SAAS,EAAQ,CAI/C,MAAM,MAAM,EAAyC,CAEnD,OADc,MAAM,KAAK,UAAU,EACtB,MAAM,EAAQ,GAQ/B,eAAsB,GAAoD,CACxE,GAAM,CAAE,UAAW,MAAM,OAAO,qBAC1B,EAAM,GAAQ,CACpB,OAAO,IAAI,EAAiB,CAC1B,GAAI,EAAI,GAAG,SACX,OAAQ,EAAI,OAAO,MAAM,CAAE,WAAY,GAAM,CAAC,CAC/C,CAAC"}
1
+ {"version":3,"file":"query-client.mjs","names":[],"sources":["../src/query-client.ts"],"sourcesContent":["/**\n * MediaQueryClient — read-only media client for frontends.\n *\n * Usage:\n * import { createMediaQueryClient } from '@murumets-ee/media/query'\n * const media = await createMediaQueryClient()\n * const image = await media.findById(id)\n */\n\nimport type {\n CountOptions,\n FindByIdOptions,\n FindManyOptions,\n QueryClient,\n} from '@murumets-ee/entity/query'\nimport type { Media } from './entity.js'\nimport type { MediaRecord } from './types.js'\n\ntype MediaFields = typeof Media.allFields\n\nexport interface MediaQueryClientConfig {\n query: QueryClient<MediaFields>\n}\n\nexport class MediaQueryClient {\n private query: QueryClient<MediaFields>\n\n constructor(config: MediaQueryClientConfig) {\n this.query = config.query\n }\n\n async findById(id: string, options?: FindByIdOptions): Promise<MediaRecord | null> {\n return this.query.findById(id, options)\n }\n\n async findMany(options?: FindManyOptions): Promise<MediaRecord[]> {\n return this.query.findMany(options)\n }\n\n async count(options?: CountOptions): Promise<number> {\n return this.query.count(options)\n }\n}\n\n/**\n * Factory — creates a MediaQueryClient.\n * Must be called after createApp().\n */\nexport async function createMediaQueryClient(): Promise<MediaQueryClient> {\n const { createQueryClient } = await import('@murumets-ee/core/clients')\n const { Media } = await import('./entity.js')\n const query = createQueryClient(Media)\n return new MediaQueryClient({ query })\n}\n"],"mappings":"AAwBA,IAAa,EAAb,KAA8B,CAC5B,MAEA,YAAY,EAAgC,CAC1C,KAAK,MAAQ,EAAO,MAGtB,MAAM,SAAS,EAAY,EAAwD,CACjF,OAAO,KAAK,MAAM,SAAS,EAAI,EAAQ,CAGzC,MAAM,SAAS,EAAmD,CAChE,OAAO,KAAK,MAAM,SAAS,EAAQ,CAGrC,MAAM,MAAM,EAAyC,CACnD,OAAO,KAAK,MAAM,MAAM,EAAQ,GAQpC,eAAsB,GAAoD,CACxE,GAAM,CAAE,qBAAsB,MAAM,OAAO,6BACrC,CAAE,SAAU,MAAM,OAAO,yBAAA,KAAA,GAAA,EAAA,EAAA,CAE/B,OAAO,IAAI,EAAiB,CAAE,MADhB,EAAkB,EAAM,CACD,CAAC"}
@@ -0,0 +1,2 @@
1
+ import{n as e,r as t,t as n}from"./variant-key-DZUYURS5.mjs";import"server-only";async function r(r){let{db:i,storage:a,logger:o,styles:s}=r,{AdminClient:c}=await import(`@murumets-ee/entity/admin`),{Media:l}=await import(`./entity-DZFku8b7.mjs`),{schemaRegistry:u}=await import(`@murumets-ee/db`),{eq:d}=await import(`drizzle-orm`),f=new c({entity:l,db:i,logger:o,contextResolver:r.contextResolver}),p=u.get(`media`);if(!p)throw Error(`Media schema not registered`);let m={total:0,processed:0,skipped:0,errors:0},h=0;for(o?.info({styles:Object.keys(s)},`Starting variant regeneration`);;){let r=await f.findMany({where:d(p.mediaType,`image`),limit:100,offset:h});if(r.length===0)break;m.total+=r.length;for(let i of r)try{if(!e(i.mimeType)){m.skipped++;continue}let r=await a.download(i.fileKey),c;if(Buffer.isBuffer(r.body))c=r.body;else{let e=[],t=r.body.getReader();for(;;){let{done:n,value:r}=await t.read();if(n)break;r&&e.push(r)}c=Buffer.concat(e)}let l=await t(c,s),u=await a.getMetadata(i.fileKey),d=u?.metadata?.variants;d&&await Promise.all(Object.values(d).map(e=>a.delete(e).catch(()=>{})));let f={},p=u?.visibility??`public`,h=await Promise.all([...l.variants].map(async([e,t])=>{let r=n(i.fileKey,e,t.format);try{return await a.upload(t.buffer,{key:r,filename:`${e}_${i.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:p,metadata:{variantOf:i.fileKey,style:e}}),{styleName:e,vKey:r,ok:!0}}catch(t){return o?.warn({style:e,key:r,error:t},`Failed to upload regenerated variant (non-fatal)`),{styleName:e,vKey:r,ok:!1}}}));for(let e of h)e.ok&&(f[e.styleName]=e.vKey);Object.keys(f).length>0&&await a.updateMetadata(i.fileKey,{metadata:{...u?.metadata??{},variants:f}}).catch(e=>{o?.warn({key:i.fileKey,error:e},`Failed to update variant metadata (non-fatal)`)}),m.processed++,o?.debug({id:i.id,variants:Object.keys(f)},`Regenerated variants`)}catch(e){m.errors++,o?.error({id:i.id,fileKey:i.fileKey,error:e},`Failed to regenerate variants for media record`)}if(h+=100,r.length<100)break}return o?.info(m,`Variant regeneration complete`),m}export{r as regenerateAllVariants};
2
+ //# sourceMappingURL=regenerate-variants-BFnFcnxm.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"regenerate-variants-BFnFcnxm.mjs","names":[],"sources":["../src/regenerate-variants.ts"],"sourcesContent":["/**\n * Batch variant regeneration — reprocesses all image media with updated styles.\n *\n * Downloads each original from storage, generates new variants via Sharp,\n * cleans up old variant files, and uploads new ones.\n *\n * Per-image errors are logged but don't stop the batch.\n */\n\nimport 'server-only'\n\nimport type { Logger } from '@murumets-ee/core'\nimport type { ContextResolver } from '@murumets-ee/entity'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type { ImageStyle } from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\nconst BATCH_SIZE = 100\n\nexport interface RegenerateOptions {\n db: PostgresJsDatabase\n storage: StorageClient\n logger?: Logger\n /** Current image styles to generate */\n styles: Record<string, ImageStyle>\n /** Security context resolver — passed through to AdminClient. */\n contextResolver?: ContextResolver\n}\n\nexport interface RegenerateResult {\n /** Total image media records found */\n total: number\n /** Successfully reprocessed */\n processed: number\n /** Skipped (non-processable mimeType, download failed, etc.) */\n skipped: number\n /** Failed with errors */\n errors: number\n}\n\n/**\n * Regenerate variants for all image media.\n * Processes in batches of 100 to avoid memory pressure.\n */\nexport async function regenerateAllVariants(options: RegenerateOptions): Promise<RegenerateResult> {\n const { db, storage, logger, styles } = options\n const { AdminClient } = await import('@murumets-ee/entity/admin')\n const { Media } = await import('./entity.js')\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { eq } = await import('drizzle-orm')\n\n const admin = new AdminClient<typeof Media.allFields>({\n entity: Media,\n db,\n logger,\n contextResolver: options.contextResolver,\n })\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered')\n\n const result: RegenerateResult = { total: 0, processed: 0, skipped: 0, errors: 0 }\n let offset = 0\n\n logger?.info({ styles: Object.keys(styles) }, 'Starting variant regeneration')\n\n // Process in batches\n while (true) {\n const batch = await admin.findMany({\n where: eq(table.mediaType, 'image'),\n limit: BATCH_SIZE,\n offset,\n })\n\n if (batch.length === 0) break\n result.total += batch.length\n\n for (const record of batch) {\n try {\n // Skip non-processable images (SVG, GIF)\n if (!isProcessableImage(record.mimeType)) {\n result.skipped++\n continue\n }\n\n // Download original from storage\n const downloaded = await storage.download(record.fileKey)\n let buffer: Buffer\n if (Buffer.isBuffer(downloaded.body)) {\n buffer = downloaded.body\n } else {\n // ReadableStream → Buffer\n const chunks: Uint8Array[] = []\n const reader = downloaded.body.getReader()\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n if (value) chunks.push(value)\n }\n buffer = Buffer.concat(chunks)\n }\n\n // Generate new variants\n const processed = await processImage(buffer, styles)\n\n // Delete old variant files (best-effort, parallel)\n const oldFileRecord = await storage.getMetadata(record.fileKey)\n const oldVariants = (oldFileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n if (oldVariants) {\n await Promise.all(\n Object.values(oldVariants).map((vKey) => storage.delete(vKey).catch(() => {})),\n )\n }\n\n // Upload new variants (parallel)\n const newVariantKeys: Record<string, string> = {}\n const visibility = oldFileRecord?.visibility ?? 'public'\n const uploadResults = await Promise.all(\n [...processed.variants].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(record.fileKey, styleName, variant.format)\n try {\n await storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${record.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: record.fileKey, style: styleName },\n })\n return { styleName, vKey, ok: true as const }\n } catch (uploadErr) {\n logger?.warn(\n { style: styleName, key: vKey, error: uploadErr },\n 'Failed to upload regenerated variant (non-fatal)',\n )\n return { styleName, vKey, ok: false as const }\n }\n }),\n )\n for (const r of uploadResults) {\n if (r.ok) newVariantKeys[r.styleName] = r.vKey\n }\n\n // Update original file's metadata with new variant keys\n if (Object.keys(newVariantKeys).length > 0) {\n await storage\n .updateMetadata(record.fileKey, {\n metadata: {\n ...(oldFileRecord?.metadata ?? {}),\n variants: newVariantKeys,\n },\n })\n .catch((metaErr: unknown) => {\n logger?.warn(\n { key: record.fileKey, error: metaErr },\n 'Failed to update variant metadata (non-fatal)',\n )\n })\n }\n\n result.processed++\n logger?.debug(\n { id: record.id, variants: Object.keys(newVariantKeys) },\n 'Regenerated variants',\n )\n } catch (err) {\n result.errors++\n logger?.error(\n { id: record.id, fileKey: record.fileKey, error: err },\n 'Failed to regenerate variants for media record',\n )\n }\n }\n\n offset += BATCH_SIZE\n if (batch.length < BATCH_SIZE) break\n }\n\n logger?.info(result, 'Variant regeneration complete')\n return result\n}\n"],"mappings":"iFA8CA,eAAsB,EAAsB,EAAuD,CACjG,GAAM,CAAE,KAAI,UAAS,SAAQ,UAAW,EAClC,CAAE,eAAgB,MAAM,OAAO,6BAC/B,CAAE,SAAU,MAAM,OAAO,yBACzB,CAAE,kBAAmB,MAAM,OAAO,mBAClC,CAAE,MAAO,MAAM,OAAO,eAEtB,EAAQ,IAAI,EAAoC,CACpD,OAAQ,EACR,KACA,SACA,gBAAiB,EAAQ,gBAC1B,CAAC,CACI,EAAQ,EAAe,IAAI,QAAQ,CACzC,GAAI,CAAC,EAAO,MAAU,MAAM,8BAA8B,CAE1D,IAAM,EAA2B,CAAE,MAAO,EAAG,UAAW,EAAG,QAAS,EAAG,OAAQ,EAAG,CAC9E,EAAS,EAKb,IAHA,GAAQ,KAAK,CAAE,OAAQ,OAAO,KAAK,EAAO,CAAE,CAAE,gCAAgC,GAGjE,CACX,IAAM,EAAQ,MAAM,EAAM,SAAS,CACjC,MAAO,EAAG,EAA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,MAAO,EAAW,IAAK,EAAM,MAAO,EAAW,CACjD,mDACD,CACM,CAAE,YAAW,OAAM,GAAI,GAAgB,GAEhD,CACH,CACD,IAAK,IAAM,KAAK,EACV,EAAE,KAAI,EAAe,EAAE,WAAa,EAAE,MAIxC,OAAO,KAAK,EAAe,CAAC,OAAS,GACvC,MAAM,EACH,eAAe,EAAO,QAAS,CAC9B,SAAU,CACR,GAAI,GAAe,UAAY,EAAE,CACjC,SAAU,EACX,CACF,CAAC,CACD,MAAO,GAAqB,CAC3B,GAAQ,KACN,CAAE,IAAK,EAAO,QAAS,MAAO,EAAS,CACvC,gDACD,EACD,CAGN,EAAO,YACP,GAAQ,MACN,CAAE,GAAI,EAAO,GAAI,SAAU,OAAO,KAAK,EAAe,CAAE,CACxD,uBACD,OACM,EAAK,CACZ,EAAO,SACP,GAAQ,MACN,CAAE,GAAI,EAAO,GAAI,QAAS,EAAO,QAAS,MAAO,EAAK,CACtD,iDACD,CAKL,GADA,GAAU,IACN,EAAM,OAAS,IAAY,MAIjC,OADA,GAAQ,KAAK,EAAQ,gCAAgC,CAC9C"}
@@ -1,5 +1,66 @@
1
+ import * as _$_murumets_ee_entity0 from "@murumets-ee/entity";
2
+ import { InferEntityDTO } from "@murumets-ee/entity";
1
3
  import { FileVisibility } from "@murumets-ee/storage";
2
4
 
5
+ //#region src/entity.d.ts
6
+ /**
7
+ * Pre-defined Media entity.
8
+ *
9
+ * Auto-registered by the media() plugin — users do NOT add this to their entities array.
10
+ * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * import { media } from '@murumets-ee/media/plugin'
15
+ *
16
+ * export default defineConfig({
17
+ * plugins: [storage(), media()],
18
+ * // Media entity is auto-added — no need to list it here
19
+ * })
20
+ * ```
21
+ */
22
+ declare const Media: _$_murumets_ee_entity0.Entity<{
23
+ id: _$_murumets_ee_entity0.IdField;
24
+ } & _$_murumets_ee_entity0.AuditableFields & {
25
+ title: _$_murumets_ee_entity0.TextField & {
26
+ readonly translatable: true;
27
+ };
28
+ alt: _$_murumets_ee_entity0.TextField & {
29
+ readonly translatable: true;
30
+ };
31
+ description: _$_murumets_ee_entity0.TextField & {
32
+ readonly translatable: true;
33
+ }; /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */
34
+ fileKey: _$_murumets_ee_entity0.TextField & {
35
+ readonly required: true;
36
+ readonly indexed: true;
37
+ };
38
+ filename: _$_murumets_ee_entity0.TextField & {
39
+ readonly required: true;
40
+ };
41
+ mimeType: _$_murumets_ee_entity0.TextField & {
42
+ readonly required: true;
43
+ readonly indexed: true;
44
+ };
45
+ size: _$_murumets_ee_entity0.NumberField & {
46
+ readonly required: true;
47
+ readonly integer: true;
48
+ };
49
+ width: _$_murumets_ee_entity0.NumberField & {
50
+ readonly integer: true;
51
+ };
52
+ height: _$_murumets_ee_entity0.NumberField & {
53
+ readonly integer: true;
54
+ };
55
+ mediaType: _$_murumets_ee_entity0.SelectField & {
56
+ options: readonly ["image", "video", "audio", "document", "other"];
57
+ } & {
58
+ readonly options: readonly ["image", "video", "audio", "document", "other"];
59
+ readonly required: true;
60
+ readonly indexed: true;
61
+ };
62
+ }>;
63
+ //#endregion
3
64
  //#region src/types.d.ts
4
65
  /** Media type derived from MIME type */
5
66
  type MediaType = 'image' | 'video' | 'audio' | 'document' | 'other';
@@ -33,23 +94,14 @@ interface MediaUploadResult {
33
94
  /** The file's public/signed URL */
34
95
  url: string;
35
96
  }
36
- /** Lightweight view of a media entity for API consumers */
37
- interface MediaRecord {
38
- id: string;
39
- title: string | null;
40
- alt: string | null;
41
- description: string | null;
42
- fileKey: string;
43
- filename: string;
44
- mimeType: string;
45
- size: number;
46
- width: number | null;
47
- height: number | null;
48
- mediaType: MediaType;
49
- createdBy: string | null;
50
- createdAt: Date | string;
51
- updatedAt: Date | string;
52
- }
97
+ /**
98
+ * Lightweight view of a media entity for API consumers.
99
+ *
100
+ * Structural alias of `InferEntityDTO<typeof Media.allFields>` — the Media entity
101
+ * is the source of truth. Use this alias instead of re-declaring the shape so
102
+ * schema changes propagate automatically.
103
+ */
104
+ type MediaRecord = InferEntityDTO<typeof Media.allFields>;
53
105
  /** Options for listing media */
54
106
  interface MediaListOptions {
55
107
  /** Filter by media type */
@@ -99,5 +151,5 @@ interface MediaPluginConfig {
99
151
  imageStyles?: Record<string, ImageStyle>;
100
152
  }
101
153
  //#endregion
102
- export { MediaRecord as a, MediaUploadResult as c, MediaPluginConfig as i, MediaListOptions as n, MediaType as o, MediaListResult as r, MediaUploadOptions as s, ImageStyle as t };
103
- //# sourceMappingURL=types-BV_pOm23.d.mts.map
154
+ export { MediaRecord as a, MediaUploadResult as c, MediaPluginConfig as i, Media as l, MediaListOptions as n, MediaType as o, MediaListResult as r, MediaUploadOptions as s, ImageStyle as t };
155
+ //# sourceMappingURL=types-BBAbJooM.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types-BBAbJooM.d.mts","names":[],"sources":["../src/entity.ts","../src/types.ts"],"mappings":";;;;;;;;;;AAmBA;;;;;;;;;;;cAAa,KAAA,yBAAK,MAAA;MAoChB,sBAAA,CAAA,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KClDU,SAAA;ADcZ;AAAA,UCXiB,kBAAA;;EAEf,QAAA;;EAEA,QAAA;;EAEA,IAAA;;EAEA,UAAA,GAAa,cAAA;;EAEb,KAAA;;EAEA,GAAA;;EAEA,WAAA;EDHgB;ECKhB,KAAA;EDLgB;ECOhB,MAAA;;EAEA,UAAA;AAAA;;UAIe,iBAAA;;EAEf,KAAA,EAAO,WAAA;;EAEP,GAAA;AAAA;;;;;;;;KAUU,WAAA,GAAc,cAAA,QAAsB,KAAA,CAAM,SAAA;;UAGrC,gBAAA;;EAEf,SAAA,GAAY,SAAA;;EAEZ,cAAA;;EAEA,MAAA;;EAEA,KAAA;;EAEA,MAAA;;EAEA,OAAA;;EAEA,cAAA;AAAA;;UAIe,eAAA;EACf,KAAA,EAAO,WAAA;EACP,KAAA;EACA,KAAA;EACA,MAAA;AAAA;;UAIe,UAAA;EAtEI;EAwEnB,KAAA;EAxEmB;EA0EnB,MAAA;EAvEe;EAyEf,GAAA;;EAEA,MAAA;EAzEA;EA2EA,OAAA;AAAA;;UAIe,iBAAA;EAvEf;EAyEA,aAAA;EArEA;EAuEA,aAAA;EAnEA;EAqEA,iBAAA,GAAoB,cAAA;EAnEV;EAqEV,WAAA,GAAc,MAAA,SAAe,UAAA;AAAA"}
@@ -0,0 +1,2 @@
1
+ import e from"sharp";const t=new Set([`image/svg+xml`,`image/gif`]);function n(e){return e.startsWith(`image/`)&&!t.has(e)}async function r(t,n){let r=await e(t).metadata(),i=r.width??0,a=r.height??0,o=new Map,s=Object.entries(n);return await Promise.all(s.map(async([n,r])=>{let i=r.format??`webp`,a=r.quality??80,s=r.fit??`cover`,{data:c,info:l}=await e(t).resize({width:r.width,height:r.height,fit:s,withoutEnlargement:!0})[i]({quality:a}).toBuffer({resolveWithObject:!0});o.set(n,{buffer:c,format:i,mimeType:`image/${i}`,width:l.width,height:l.height})})),{width:i,height:a,variants:o}}async function i(t){let n=await e(t).metadata();return{width:n.width??0,height:n.height??0}}function a(e,t,n=`webp`){let r=e.lastIndexOf(`/`);return`${e.substring(0,r)}/${t}_${e.substring(r+1).replace(/\.[^.]+$/,``)}.${n}`}export{r as i,i as n,n as r,a as t};
2
+ //# sourceMappingURL=variant-key-gVMhzKyv.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"variant-key-gVMhzKyv.mjs","names":[],"sources":["../src/process-image.ts","../src/variant-key.ts"],"sourcesContent":["/**\n * Server-side image processing via Sharp.\n *\n * Pure processing module — no DB or storage dependencies.\n * Takes a buffer and image styles, returns metadata + variant buffers.\n */\n\nimport sharp from 'sharp'\nimport type { ImageStyle } from './types.js'\n\n/** Result of processing an image through Sharp */\nexport interface ProcessedImage {\n /** Original image width in pixels */\n width: number\n /** Original image height in pixels */\n height: number\n /** Generated variant buffers keyed by style name */\n variants: Map<string, ProcessedVariant>\n}\n\n/** A single processed variant */\nexport interface ProcessedVariant {\n buffer: Buffer\n format: string\n mimeType: string\n width: number\n height: number\n}\n\n/** MIME types that should NOT be processed (vectors, animations) */\nconst SKIP_MIME_TYPES = new Set(['image/svg+xml', 'image/gif'])\n\n/**\n * Check if a MIME type is eligible for Sharp processing.\n * Returns false for SVG, GIF, and non-image types.\n */\nexport function isProcessableImage(mimeType: string): boolean {\n return mimeType.startsWith('image/') && !SKIP_MIME_TYPES.has(mimeType)\n}\n\n/**\n * Extract image dimensions and generate resized variants.\n *\n * @param buffer - Original image file as a Buffer\n * @param styles - Named image style presets to generate\n * @returns Metadata (width/height) and variant buffers\n */\nexport async function processImage(\n buffer: Buffer,\n styles: Record<string, ImageStyle>,\n): Promise<ProcessedImage> {\n const meta = await sharp(buffer).metadata()\n const width = meta.width ?? 0\n const height = meta.height ?? 0\n\n const variants = new Map<string, ProcessedVariant>()\n\n const entries = Object.entries(styles)\n await Promise.all(\n entries.map(async ([name, style]) => {\n const fmt = style.format ?? 'webp'\n const quality = style.quality ?? 80\n const fit = style.fit ?? 'cover'\n\n const resized = sharp(buffer).resize({\n width: style.width,\n height: style.height,\n fit,\n withoutEnlargement: true,\n })\n\n const { data: variantBuffer, info } = await resized[fmt]({ quality }).toBuffer({\n resolveWithObject: true,\n })\n\n variants.set(name, {\n buffer: variantBuffer,\n format: fmt,\n mimeType: `image/${fmt}`,\n width: info.width,\n height: info.height,\n })\n }),\n )\n\n return { width, height, variants }\n}\n\n/**\n * Extract only image dimensions (no variant generation).\n * Useful for getting width/height when styles are empty.\n */\nexport async function getImageDimensions(\n buffer: Buffer,\n): Promise<{ width: number; height: number }> {\n const meta = await sharp(buffer).metadata()\n return { width: meta.width ?? 0, height: meta.height ?? 0 }\n}\n","/**\n * Variant key derivation convention.\n *\n * Given an original storage key and a style name, produces a deterministic\n * variant key in the same directory.\n *\n * Convention:\n * Original: uploads/2026/02/{uuid}/photo.jpg\n * Variant: uploads/2026/02/{uuid}/thumbnail_photo.webp\n */\n\n/**\n * Derive a variant storage key from the original key + style name.\n *\n * @param originalKey - The original file's storage key\n * @param styleName - The image style name (e.g., 'thumbnail', 'medium')\n * @param format - The variant output format (default: 'webp')\n * @returns The derived variant key\n */\nexport function deriveVariantKey(originalKey: string, styleName: string, format = 'webp'): string {\n const lastSlash = originalKey.lastIndexOf('/')\n const dir = originalKey.substring(0, lastSlash)\n const filename = originalKey.substring(lastSlash + 1)\n const baseName = filename.replace(/\\.[^.]+$/, '')\n return `${dir}/${styleName}_${baseName}.${format}`\n}\n"],"mappings":"qBA8BA,MAAM,EAAkB,IAAI,IAAI,CAAC,gBAAiB,YAAY,CAAC,CAM/D,SAAgB,EAAmB,EAA2B,CAC5D,OAAO,EAAS,WAAW,SAAS,EAAI,CAAC,EAAgB,IAAI,EAAS,CAUxE,eAAsB,EACpB,EACA,EACyB,CACzB,IAAM,EAAO,MAAM,EAAM,EAAO,CAAC,UAAU,CACrC,EAAQ,EAAK,OAAS,EACtB,EAAS,EAAK,QAAU,EAExB,EAAW,IAAI,IAEf,EAAU,OAAO,QAAQ,EAAO,CA4BtC,OA3BA,MAAM,QAAQ,IACZ,EAAQ,IAAI,MAAO,CAAC,EAAM,KAAW,CACnC,IAAM,EAAM,EAAM,QAAU,OACtB,EAAU,EAAM,SAAW,GAC3B,EAAM,EAAM,KAAO,QASnB,CAAE,KAAM,EAAe,QAAS,MAPtB,EAAM,EAAO,CAAC,OAAO,CACnC,MAAO,EAAM,MACb,OAAQ,EAAM,OACd,MACA,mBAAoB,GACrB,CAAC,CAEkD,GAAK,CAAE,UAAS,CAAC,CAAC,SAAS,CAC7E,kBAAmB,GACpB,CAAC,CAEF,EAAS,IAAI,EAAM,CACjB,OAAQ,EACR,OAAQ,EACR,SAAU,SAAS,IACnB,MAAO,EAAK,MACZ,OAAQ,EAAK,OACd,CAAC,EACF,CACH,CAEM,CAAE,QAAO,SAAQ,WAAU,CAOpC,eAAsB,EACpB,EAC4C,CAC5C,IAAM,EAAO,MAAM,EAAM,EAAO,CAAC,UAAU,CAC3C,MAAO,CAAE,MAAO,EAAK,OAAS,EAAG,OAAQ,EAAK,QAAU,EAAG,CC7E7D,SAAgB,EAAiB,EAAqB,EAAmB,EAAS,OAAgB,CAChG,IAAM,EAAY,EAAY,YAAY,IAAI,CAI9C,MAAO,GAHK,EAAY,UAAU,EAAG,EAAU,CAGjC,GAAG,EAAU,GAFV,EAAY,UAAU,EAAY,EAAE,CAC3B,QAAQ,WAAY,GAAG,CACV,GAAG"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@murumets-ee/media",
3
- "version": "0.2.1",
3
+ "version": "0.4.0",
4
4
  "license": "Elastic-2.0",
5
5
  "type": "module",
6
6
  "exports": {
@@ -39,6 +39,10 @@
39
39
  "./image-styles": {
40
40
  "types": "./dist/image-styles.d.mts",
41
41
  "import": "./dist/image-styles.mjs"
42
+ },
43
+ "./processing": {
44
+ "types": "./dist/processing.d.mts",
45
+ "import": "./dist/processing.mjs"
42
46
  }
43
47
  },
44
48
  "files": [
@@ -52,18 +56,18 @@
52
56
  "server-only": "^0.0.1",
53
57
  "sharp": "^0.34.5",
54
58
  "tailwind-merge": "^2.6.0",
55
- "@murumets-ee/core": "0.2.0",
56
- "@murumets-ee/entity": "0.2.1",
57
- "@murumets-ee/db": "0.2.0",
58
- "@murumets-ee/logging": "0.2.0",
59
- "@murumets-ee/storage": "0.2.0",
60
- "@murumets-ee/settings": "0.2.0"
59
+ "@murumets-ee/db": "0.4.0",
60
+ "@murumets-ee/core": "0.4.0",
61
+ "@murumets-ee/entity": "0.4.0",
62
+ "@murumets-ee/logging": "0.4.0",
63
+ "@murumets-ee/settings": "0.4.0",
64
+ "@murumets-ee/storage": "0.4.0"
61
65
  },
62
66
  "peerDependencies": {
63
67
  "lucide-react": ">=0.400.0",
64
68
  "react": ">=19.0.0",
65
69
  "react-dom": ">=19.0.0",
66
- "@murumets-ee/ui": "0.1.5"
70
+ "@murumets-ee/ui": "0.4.0"
67
71
  },
68
72
  "peerDependenciesMeta": {
69
73
  "@murumets-ee/ui": {
@@ -1,2 +0,0 @@
1
- import{Media as e}from"./entity-DZFku8b7.mjs";import{n as t,r as n,t as r}from"./variant-key-DZUYURS5.mjs";import"server-only";var i=class{db;storage;logger;admin=null;imageStyles;constructor(e){this.db=e.db,this.storage=e.storage,this.logger=e.logger,this.imageStyles=e.imageStyles??null}async getAdmin(){if(!this.admin){let{AdminClient:t}=await import(`@murumets-ee/entity/admin`);this.admin=new t({entity:e,db:this.db,logger:this.logger})}return this.admin}async resolveImageStyles(){if(this.imageStyles)return this.imageStyles;try{let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:t}=await import(`./image-styles-settings-5zpGEfKG.mjs`),{getApp:n}=await import(`@murumets-ee/core`),r=await e(t,{app:n()}).get(`imageStyles`);if(r&&Object.keys(r).length>0)return this.imageStyles=r,r}catch{}try{let{getMediaConfig:e}=await import(`./plugin-DV7lvImm.mjs`);return this.imageStyles=e().imageStyles,this.imageStyles}catch{let e={thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}};return this.imageStyles=e,e}}invalidateImageStylesCache(){this.imageStyles=null}async upload(e,i){let s=await this.storage.upload(e,{filename:i.filename,mimeType:i.mimeType,size:i.size,visibility:i.visibility,uploadedBy:i.uploadedBy}),c=i.width??null,l=i.height??null,u={};if(e instanceof Buffer&&t(i.mimeType))try{let t=await n(e,await this.resolveImageStyles());c=t.width,l=t.height;let a=s.visibility;await Promise.all([...t.variants.entries()].map(async([e,t])=>{let n=r(s.key,e,t.format);try{await this.storage.upload(t.buffer,{key:n,filename:`${e}_${i.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:a,metadata:{variantOf:s.key,style:e},uploadedBy:i.uploadedBy}),u[e]=n,this.logger?.debug({style:e,key:n,width:t.width,height:t.height},`Variant uploaded`)}catch(t){this.logger?.warn({style:e,key:n,error:t},`Failed to upload variant (non-fatal)`)}})),Object.keys(u).length>0&&await this.storage.updateMetadata(s.key,{metadata:{...s.metadata??{},variants:u}}).catch(e=>{this.logger?.warn({key:s.key,error:e},`Failed to update original file metadata with variant keys (non-fatal)`)}),this.logger?.info({width:c,height:l,variants:Object.keys(u)},`Image processed`)}catch(e){this.logger?.warn({filename:i.filename,error:e},`Image processing failed (non-fatal, original saved)`)}let d=a(i.mimeType),f=await this.getAdmin();try{let e=await f.create({title:i.title??o(i.filename),alt:i.alt??null,description:i.description??null,fileKey:s.key,filename:i.filename,mimeType:i.mimeType,size:i.size,width:c,height:l,mediaType:d}),t=await this.storage.getUrl(s.key);return this.logger?.info({id:e.id,fileKey:s.key,mediaType:d},`Media uploaded`),{media:e,url:t}}catch(e){this.logger?.error({fileKey:s.key,error:e},`Media entity creation failed, rolling back storage uploads`);for(let e of Object.values(u))await this.storage.delete(e).catch(()=>{});throw await this.storage.delete(s.key).catch(e=>{this.logger?.error({fileKey:s.key,error:e},`Storage rollback also failed`)}),e}}async findById(e,t){return await(await this.getAdmin()).findById(e,t)}async findMany(e){let t=await this.getAdmin(),{schemaRegistry:n}=await import(`@murumets-ee/db`),{and:r,asc:i,desc:a,eq:o,ilike:s,sql:c}=await import(`drizzle-orm`),l=n.get(`media`);if(!l)throw Error(`Media schema not registered. Is the media() plugin loaded?`);let u=[];if(e?.mediaType&&u.push(o(l.mediaType,e.mediaType)),e?.mimeTypePrefix&&u.push(s(l.mimeType,`${e.mimeTypePrefix}%`)),e?.search){let t=`%${e.search}%`;u.push(c`(${s(l.filename,t)} OR ${l.fields} ->> 'title' ILIKE ${t})`)}let d=e?.limit??50,f=e?.offset??0,p=u.length>0?r(...u):void 0,[m]=await this.db.select({count:c`count(*)::int`}).from(l).where(p),h=e?.orderBy===`filename`?l.filename:l.createdAt,g=(e?.orderDirection??`desc`)===`asc`?i:a;return{items:await t.findMany({where:p,limit:d,offset:f,orderBy:g(h)}),total:m?.count??0,limit:d,offset:f}}async update(e,t){return await(await this.getAdmin()).update(e,t)}async delete(e){let t=await this.getAdmin(),n=await t.findById(e);if(!n)throw Error(`Media not found: ${e}`);let r=n.fileKey;await t.delete(e);let i=(await this.storage.getMetadata(r))?.metadata?.variants;if(i)for(let e of Object.values(i))await this.storage.delete(e).catch(t=>{this.logger?.warn({variantKey:e,error:t},`Failed to delete variant file`)});await this.storage.delete(r).catch(t=>{this.logger?.error({id:e,fileKey:r,error:t},`Failed to delete file from storage after entity deletion`)}),this.logger?.info({id:e,fileKey:r,deletedVariants:i?Object.keys(i).length:0},`Media deleted`)}async getUrl(e){let t=await(await this.getAdmin()).findById(e);if(!t)throw Error(`Media not found: ${e}`);return this.storage.getUrl(t.fileKey)}async getUrls(e){if(e.length===0)return new Map;let t=await this.getAdmin(),{schemaRegistry:n}=await import(`@murumets-ee/db`),{inArray:r}=await import(`drizzle-orm`),i=n.get(`media`);if(!i)return new Map;let a=await t.findMany({where:r(i.id,e),limit:e.length}),o=new Map;return await Promise.all(a.map(async e=>{let t=await this.storage.getUrl(e.fileKey);o.set(e.id,t)})),o}async getVariantUrl(e,t){let n=await(await this.getAdmin()).findById(e);if(!n)return null;let i=n.fileKey,a=(await this.resolveImageStyles())[t];if(a){let e=r(i,t,a.format??`webp`);try{return await this.storage.getUrl(e)}catch{}}try{return await this.storage.getUrl(i)}catch{return null}}async getVariantUrls(e,t){if(e.length===0)return new Map;let n=await this.getAdmin(),{schemaRegistry:i}=await import(`@murumets-ee/db`),{inArray:a}=await import(`drizzle-orm`),o=i.get(`media`);if(!o)return new Map;let s=await n.findMany({where:a(o.id,e),limit:e.length}),c=(await this.resolveImageStyles())[t],l=new Map;return await Promise.all(s.map(async e=>{if(c){let n=r(e.fileKey,t,c.format??`webp`);try{let t=await this.storage.getUrl(n);l.set(e.id,t);return}catch{}}try{let t=await this.storage.getUrl(e.fileKey);l.set(e.id,t)}catch{}})),l}};function a(e){return e.startsWith(`image/`)?`image`:e.startsWith(`video/`)?`video`:e.startsWith(`audio/`)?`audio`:e===`application/pdf`||e.startsWith(`application/msword`)||e.startsWith(`application/vnd.`)?`document`:`other`}function o(e){return e.replace(/\.[^.]+$/,``).replace(/[-_]/g,` `)}export{i as MediaClient};
2
- //# sourceMappingURL=client-DNTvmvYS.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"client-DNTvmvYS.mjs","names":[],"sources":["../src/client.ts"],"sourcesContent":["/**\n * MediaClient — wraps AdminClient + StorageClient for media management.\n *\n * Usage:\n * import { createMediaClient } from '@murumets-ee/media/client'\n * const media = await createMediaClient(storageClient)\n * const result = await media.upload(buffer, { filename: 'photo.jpg', mimeType: 'image/jpeg', size: 12345 })\n */\n\nimport 'server-only'\n\nimport type { Logger } from '@murumets-ee/core'\nimport type { FieldConfig, FieldToTS } from '@murumets-ee/entity'\nimport type { AdminClient } from '@murumets-ee/entity/admin'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { Media } from './entity.js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type {\n ImageStyle,\n MediaListOptions,\n MediaListResult,\n MediaRecord,\n MediaType,\n MediaUploadOptions,\n MediaUploadResult,\n} from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\n/** Value type that any entity field can hold */\ntype EntityFieldValue = FieldToTS<FieldConfig> | null | undefined\n\nexport interface MediaClientConfig {\n db: PostgresJsDatabase\n storage: StorageClient\n logger?: Logger\n /** Image styles to generate on upload. Loaded from plugin config if not provided. */\n imageStyles?: Record<string, ImageStyle>\n}\n\nexport class MediaClient {\n private db: PostgresJsDatabase\n private storage: StorageClient\n private logger?: Logger\n private admin: AdminClient | null = null\n private imageStyles: Record<string, ImageStyle> | null\n\n constructor(config: MediaClientConfig) {\n this.db = config.db\n this.storage = config.storage\n this.logger = config.logger\n this.imageStyles = config.imageStyles ?? null\n }\n\n private async getAdmin(): Promise<AdminClient> {\n if (!this.admin) {\n const { AdminClient } = await import('@murumets-ee/entity/admin')\n this.admin = new AdminClient({\n entity: Media,\n db: this.db,\n logger: this.logger,\n })\n }\n return this.admin\n }\n\n /**\n * Resolve image styles: settings DB → plugin config → hardcoded defaults.\n * Result is cached; call `invalidateImageStylesCache()` after settings update.\n */\n private async resolveImageStyles(): Promise<Record<string, ImageStyle>> {\n if (this.imageStyles) return this.imageStyles\n\n // 1. Try settings DB (source of truth after first boot)\n try {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('./image-styles-settings.js')\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const stored = await settingsClient.get('imageStyles')\n if (stored && Object.keys(stored).length > 0) {\n this.imageStyles = stored\n return stored\n }\n } catch {\n // Settings not available — fall through to plugin config\n }\n\n // 2. Fall back to plugin config singleton\n try {\n const { getMediaConfig } = await import('./plugin.js')\n const config = getMediaConfig()\n this.imageStyles = config.imageStyles\n return this.imageStyles\n } catch {\n // Plugin not initialized — hardcoded defaults\n const defaults: Record<string, ImageStyle> = {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n }\n this.imageStyles = defaults\n return defaults\n }\n }\n\n /** Clear cached styles so next access re-reads from settings DB. */\n invalidateImageStylesCache(): void {\n this.imageStyles = null\n }\n\n // ---------------------------------------------------------------\n // Upload — the key convenience method\n // ---------------------------------------------------------------\n\n /**\n * Upload a file and create a media entity record in one step.\n * 1. Uploads original to storage (StorageClient)\n * 2. If image: extracts dimensions via Sharp + generates variants\n * 3. Creates media entity record (AdminClient)\n * 4. Returns the media record + URL\n *\n * Rolls back storage upload if entity creation fails.\n * Variant generation failures are logged but don't fail the upload.\n */\n async upload(\n data: Buffer | ReadableStream<Uint8Array>,\n options: MediaUploadOptions,\n ): Promise<MediaUploadResult> {\n // 1. Upload original file to storage\n const fileRecord = await this.storage.upload(data, {\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n visibility: options.visibility,\n uploadedBy: options.uploadedBy,\n })\n\n // 2. Image processing — extract dimensions + generate variants\n let width = options.width ?? null\n let height = options.height ?? null\n const variantKeys: Record<string, string> = {}\n\n if (data instanceof Buffer && isProcessableImage(options.mimeType)) {\n try {\n const styles = await this.resolveImageStyles()\n const processed = await processImage(data, styles)\n\n // Set dimensions from Sharp metadata\n width = processed.width\n height = processed.height\n\n // Upload each variant to storage\n const visibility = fileRecord.visibility\n await Promise.all(\n [...processed.variants.entries()].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(fileRecord.key, styleName, variant.format)\n try {\n await this.storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${options.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: fileRecord.key, style: styleName },\n uploadedBy: options.uploadedBy,\n })\n variantKeys[styleName] = vKey\n this.logger?.debug(\n { style: styleName, key: vKey, width: variant.width, height: variant.height },\n 'Variant uploaded',\n )\n } catch (variantErr: unknown) {\n this.logger?.warn(\n { style: styleName, key: vKey, error: variantErr },\n 'Failed to upload variant (non-fatal)',\n )\n }\n }),\n )\n\n // Store variant keys in original file's metadata\n if (Object.keys(variantKeys).length > 0) {\n await this.storage\n .updateMetadata(fileRecord.key, {\n metadata: {\n ...(fileRecord.metadata ?? {}),\n variants: variantKeys,\n },\n })\n .catch((metaErr: unknown) => {\n this.logger?.warn(\n { key: fileRecord.key, error: metaErr },\n 'Failed to update original file metadata with variant keys (non-fatal)',\n )\n })\n }\n\n this.logger?.info({ width, height, variants: Object.keys(variantKeys) }, 'Image processed')\n } catch (processErr: unknown) {\n this.logger?.warn(\n { filename: options.filename, error: processErr },\n 'Image processing failed (non-fatal, original saved)',\n )\n }\n }\n\n // 3. Create media entity record\n const mediaType = deriveMediaType(options.mimeType)\n const admin = await this.getAdmin()\n\n try {\n const entityRecord = await admin.create({\n title: options.title ?? deriveTitle(options.filename),\n alt: options.alt ?? null,\n description: options.description ?? null,\n fileKey: fileRecord.key,\n filename: options.filename,\n mimeType: options.mimeType,\n size: options.size,\n width,\n height,\n mediaType,\n } as Record<string, EntityFieldValue>)\n\n // 4. Get URL\n const url = await this.storage.getUrl(fileRecord.key)\n\n this.logger?.info(\n { id: entityRecord.id, fileKey: fileRecord.key, mediaType },\n 'Media uploaded',\n )\n\n return {\n media: entityRecord as unknown as MediaRecord,\n url,\n }\n } catch (error) {\n // Rollback: delete variants + original if entity creation fails\n this.logger?.error(\n { fileKey: fileRecord.key, error },\n 'Media entity creation failed, rolling back storage uploads',\n )\n\n // Delete variants first (best-effort)\n for (const vKey of Object.values(variantKeys)) {\n await this.storage.delete(vKey).catch(() => {})\n }\n\n // Delete original\n await this.storage.delete(fileRecord.key).catch((rollbackErr: unknown) => {\n this.logger?.error(\n { fileKey: fileRecord.key, error: rollbackErr },\n 'Storage rollback also failed',\n )\n })\n throw error\n }\n }\n\n // ---------------------------------------------------------------\n // CRUD delegation\n // ---------------------------------------------------------------\n\n async findById(id: string, options?: { locale?: string }): Promise<MediaRecord | null> {\n const admin = await this.getAdmin()\n const result = await admin.findById(id, options)\n return result as unknown as MediaRecord | null\n }\n\n async findMany(options?: MediaListOptions): Promise<MediaListResult> {\n const admin = await this.getAdmin()\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { and, asc, desc, eq, ilike, sql } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered. Is the media() plugin loaded?')\n\n // Build where conditions\n const conditions = []\n\n if (options?.mediaType) {\n conditions.push(eq(table.mediaType, options.mediaType))\n }\n if (options?.mimeTypePrefix) {\n conditions.push(ilike(table.mimeType, `${options.mimeTypePrefix}%`))\n }\n if (options?.search) {\n const pattern = `%${options.search}%`\n conditions.push(\n sql`(${ilike(table.filename, pattern)} OR ${table.fields} ->> 'title' ILIKE ${pattern})`,\n )\n }\n\n const limit = options?.limit ?? 50\n const offset = options?.offset ?? 0\n const whereClause = conditions.length > 0 ? and(...conditions) : undefined\n\n // Count total\n const [countResult] = await this.db\n .select({ count: sql<number>`count(*)::int` })\n .from(table)\n .where(whereClause)\n\n // Fetch items via AdminClient for proper DTO shaping\n const orderField = options?.orderBy === 'filename' ? table.filename : table.createdAt\n const orderFn = (options?.orderDirection ?? 'desc') === 'asc' ? asc : desc\n\n const items = await admin.findMany({\n where: whereClause,\n limit,\n offset,\n orderBy: orderFn(orderField),\n })\n\n return {\n items: items as unknown as MediaRecord[],\n total: countResult?.count ?? 0,\n limit,\n offset,\n }\n }\n\n async update(\n id: string,\n data: { title?: string; alt?: string; description?: string },\n ): Promise<MediaRecord> {\n const admin = await this.getAdmin()\n const result = await admin.update(id, data as Record<string, EntityFieldValue>)\n return result as unknown as MediaRecord\n }\n\n /**\n * Delete a media entity, its variants, and its original file in storage.\n */\n async delete(id: string): Promise<void> {\n const admin = await this.getAdmin()\n const record = await admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n const fileKey = (record as unknown as MediaRecord).fileKey\n\n // 1. Delete entity first (ref checking via entity_refs happens here)\n await admin.delete(id)\n\n // 2. Look up original file record for variant metadata\n const fileRecord = await this.storage.getMetadata(fileKey)\n const variants = (fileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n\n // 3. Delete variants (best-effort)\n if (variants) {\n for (const vKey of Object.values(variants)) {\n await this.storage.delete(vKey).catch((err: unknown) => {\n this.logger?.warn({ variantKey: vKey, error: err }, 'Failed to delete variant file')\n })\n }\n }\n\n // 4. Delete original file (best-effort)\n await this.storage.delete(fileKey).catch((err: unknown) => {\n this.logger?.error(\n { id, fileKey, error: err },\n 'Failed to delete file from storage after entity deletion',\n )\n })\n\n this.logger?.info(\n { id, fileKey, deletedVariants: variants ? Object.keys(variants).length : 0 },\n 'Media deleted',\n )\n }\n\n // ---------------------------------------------------------------\n // URL resolution\n // ---------------------------------------------------------------\n\n /**\n * Get URL for a media entity by its ID.\n * Resolves entity -> fileKey -> storage URL.\n */\n async getUrl(id: string): Promise<string> {\n const admin = await this.getAdmin()\n const record = await admin.findById(id)\n if (!record) throw new Error(`Media not found: ${id}`)\n\n return this.storage.getUrl((record as unknown as MediaRecord).fileKey)\n }\n\n /**\n * Get URLs for multiple media entities (batch).\n * Returns a Map of mediaId -> url.\n */\n async getUrls(ids: string[]): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const admin = await this.getAdmin()\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n (records as unknown as MediaRecord[]).map(async (record) => {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n }),\n )\n\n return urlMap\n }\n\n /**\n * Get variant URL for a specific image style.\n * Falls back to original URL if the variant doesn't exist.\n *\n * @param id - Media entity ID\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns The variant URL, or original URL as fallback, or null if media not found\n */\n async getVariantUrl(id: string, styleName: string): Promise<string | null> {\n const admin = await this.getAdmin()\n const record = await admin.findById(id)\n if (!record) return null\n\n const fileKey = (record as unknown as MediaRecord).fileKey\n\n // Try variant key first\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n if (style) {\n const vKey = deriveVariantKey(fileKey, styleName, style.format ?? 'webp')\n try {\n return await this.storage.getUrl(vKey)\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n return await this.storage.getUrl(fileKey)\n } catch {\n return null\n }\n }\n\n /**\n * Get variant URLs for multiple media entities (batch).\n * Falls back to original URL per item if the variant doesn't exist.\n *\n * @param ids - Media entity IDs\n * @param styleName - Image style name (e.g., 'thumbnail')\n * @returns Map of mediaId -> variant URL (or original URL as fallback)\n */\n async getVariantUrls(ids: string[], styleName: string): Promise<Map<string, string>> {\n if (ids.length === 0) return new Map()\n\n const admin = await this.getAdmin()\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { inArray } = await import('drizzle-orm')\n\n const table = schemaRegistry.get('media')\n if (!table) return new Map()\n\n const records = await admin.findMany({\n where: inArray(table.id, ids),\n limit: ids.length,\n })\n\n const styles = await this.resolveImageStyles()\n const style = styles[styleName]\n const urlMap = new Map<string, string>()\n\n await Promise.all(\n (records as unknown as MediaRecord[]).map(async (record) => {\n // Try variant URL\n if (style) {\n const vKey = deriveVariantKey(record.fileKey, styleName, style.format ?? 'webp')\n try {\n const url = await this.storage.getUrl(vKey)\n urlMap.set(record.id, url)\n return\n } catch {\n // Variant doesn't exist — fall back to original\n }\n }\n\n // Fallback to original\n try {\n const url = await this.storage.getUrl(record.fileKey)\n urlMap.set(record.id, url)\n } catch {\n // Skip — no URL available\n }\n }),\n )\n\n return urlMap\n }\n}\n\n// ---------------------------------------------------------------\n// Helpers\n// ---------------------------------------------------------------\n\nfunction deriveMediaType(mimeType: string): MediaType {\n if (mimeType.startsWith('image/')) return 'image'\n if (mimeType.startsWith('video/')) return 'video'\n if (mimeType.startsWith('audio/')) return 'audio'\n if (\n mimeType === 'application/pdf' ||\n mimeType.startsWith('application/msword') ||\n mimeType.startsWith('application/vnd.')\n ) {\n return 'document'\n }\n return 'other'\n}\n\nfunction deriveTitle(filename: string): string {\n const withoutExt = filename.replace(/\\.[^.]+$/, '')\n return withoutExt.replace(/[-_]/g, ' ')\n}\n\n/**\n * Factory — creates a MediaClient with an explicit StorageClient.\n * Must be called after createApp().\n */\nexport async function createMediaClient(storage: StorageClient): Promise<MediaClient> {\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n return new MediaClient({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n })\n}\n\n// ---------------------------------------------------------------------------\n// Lazy singleton — auto-discovers storage config\n// ---------------------------------------------------------------------------\n\nlet _clientPromise: Promise<MediaClient> | null = null\n\n/**\n * Lazy singleton — returns a shared MediaClient instance.\n * Auto-discovers storage configuration from the storage plugin.\n * Must be called after createApp().\n */\nexport function getMediaClient(): Promise<MediaClient> {\n if (!_clientPromise) {\n _clientPromise = (async () => {\n const { getApp } = await import('@murumets-ee/core')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n\n const app = getApp()\n const config = getStorageConfig()\n const storage = createStorageClient(config, { app })\n\n return new MediaClient({\n db: app.db.readWrite,\n storage,\n logger: app.logger.child({ media: true }),\n })\n })()\n }\n return _clientPromise\n}\n"],"mappings":"+HAwCA,IAAa,EAAb,KAAyB,CACvB,GACA,QACA,OACA,MAAoC,KACpC,YAEA,YAAY,EAA2B,CACrC,KAAK,GAAK,EAAO,GACjB,KAAK,QAAU,EAAO,QACtB,KAAK,OAAS,EAAO,OACrB,KAAK,YAAc,EAAO,aAAe,KAG3C,MAAc,UAAiC,CAC7C,GAAI,CAAC,KAAK,MAAO,CACf,GAAM,CAAE,eAAgB,MAAM,OAAO,6BACrC,KAAK,MAAQ,IAAI,EAAY,CAC3B,OAAQ,EACR,GAAI,KAAK,GACT,OAAQ,KAAK,OACd,CAAC,CAEJ,OAAO,KAAK,MAOd,MAAc,oBAA0D,CACtE,GAAI,KAAK,YAAa,OAAO,KAAK,YAGlC,GAAI,CACF,GAAM,CAAE,wBAAyB,MAAM,OAAO,yBACxC,CAAE,uBAAwB,MAAM,OAAO,wCACvC,CAAE,UAAW,MAAM,OAAO,qBAG1B,EAAS,MADQ,EAAqB,EAAqB,CAAE,IADvD,GAAQ,CACoD,CAAC,CACrC,IAAI,cAAc,CACtD,GAAI,GAAU,OAAO,KAAK,EAAO,CAAC,OAAS,EAEzC,MADA,MAAK,YAAc,EACZ,OAEH,EAKR,GAAI,CACF,GAAM,CAAE,kBAAmB,MAAM,OAAO,yBAGxC,MADA,MAAK,YADU,GAAgB,CACL,YACnB,KAAK,iBACN,CAEN,IAAM,EAAuC,CAC3C,UAAW,CAAE,MAAO,IAAK,OAAQ,IAAK,IAAK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GAAI,EAAI,MACpB,IAGR,CACH,CAEM,IAQX,SAAS,EAAgB,EAA6B,CAWpD,OAVI,EAAS,WAAW,SAAS,CAAS,QACtC,EAAS,WAAW,SAAS,CAAS,QACtC,EAAS,WAAW,SAAS,CAAS,QAExC,IAAa,mBACb,EAAS,WAAW,qBAAqB,EACzC,EAAS,WAAW,mBAAmB,CAEhC,WAEF,QAGT,SAAS,EAAY,EAA0B,CAE7C,OADmB,EAAS,QAAQ,WAAY,GAAG,CACjC,QAAQ,QAAS,IAAI"}
@@ -1,2 +0,0 @@
1
- import{behavior as e,defineEntity as t,field as n}from"@murumets-ee/entity";const r=t({name:`media`,fields:{title:n.text({translatable:!0}),alt:n.text({translatable:!0}),description:n.text({translatable:!0}),fileKey:n.text({required:!0,indexed:!0}),filename:n.text({required:!0}),mimeType:n.text({required:!0,indexed:!0}),size:n.number({required:!0,integer:!0}),width:n.number({integer:!0}),height:n.number({integer:!0}),mediaType:n.select({options:[`image`,`video`,`audio`,`document`,`other`],required:!0,indexed:!0})},behaviors:[e.auditable()],scope:`global`,access:{view:`public`,create:`group.editor`,update:`group.editor`,delete:`group.admin`}});export{r as t};
2
- //# sourceMappingURL=entity-D5P2l05s.mjs.map
@@ -1,2 +0,0 @@
1
- import{n as e,r as t,t as n}from"./variant-key-DZUYURS5.mjs";import"server-only";async function r(r){let{db:i,storage:a,logger:o,styles:s}=r,{AdminClient:c}=await import(`@murumets-ee/entity/admin`),{Media:l}=await import(`./entity-DZFku8b7.mjs`),{schemaRegistry:u}=await import(`@murumets-ee/db`),{eq:d}=await import(`drizzle-orm`),f=new c({entity:l,db:i,logger:o}),p=u.get(`media`);if(!p)throw Error(`Media schema not registered`);let m={total:0,processed:0,skipped:0,errors:0},h=0;for(o?.info({styles:Object.keys(s)},`Starting variant regeneration`);;){let r=await f.findMany({where:d(p.mediaType,`image`),limit:100,offset:h});if(r.length===0)break;m.total+=r.length;for(let i of r)try{if(!e(i.mimeType)){m.skipped++;continue}let r=await a.download(i.fileKey),c;if(Buffer.isBuffer(r.body))c=r.body;else{let e=[],t=r.body.getReader();for(;;){let{done:n,value:r}=await t.read();if(n)break;r&&e.push(r)}c=Buffer.concat(e)}let l=await t(c,s),u=await a.getMetadata(i.fileKey),d=u?.metadata?.variants;if(d)for(let e of Object.values(d))await a.delete(e).catch(()=>{});let f={},p=u?.visibility??`public`;for(let[e,t]of l.variants.entries()){let r=n(i.fileKey,e,t.format);try{await a.upload(t.buffer,{key:r,filename:`${e}_${i.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:p,metadata:{variantOf:i.fileKey,style:e}}),f[e]=r}catch(t){o?.warn({style:e,key:r,error:t},`Failed to upload regenerated variant (non-fatal)`)}}Object.keys(f).length>0&&await a.updateMetadata(i.fileKey,{metadata:{...u?.metadata??{},variants:f}}).catch(e=>{o?.warn({key:i.fileKey,error:e},`Failed to update variant metadata (non-fatal)`)}),m.processed++,o?.debug({id:i.id,variants:Object.keys(f)},`Regenerated variants`)}catch(e){m.errors++,o?.error({id:i.id,fileKey:i.fileKey,error:e},`Failed to regenerate variants for media record`)}if(h+=100,r.length<100)break}return o?.info(m,`Variant regeneration complete`),m}export{r as regenerateAllVariants};
2
- //# sourceMappingURL=regenerate-variants-DY7D4Ky3.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"regenerate-variants-DY7D4Ky3.mjs","names":[],"sources":["../src/regenerate-variants.ts"],"sourcesContent":["/**\n * Batch variant regeneration — reprocesses all image media with updated styles.\n *\n * Downloads each original from storage, generates new variants via Sharp,\n * cleans up old variant files, and uploads new ones.\n *\n * Per-image errors are logged but don't stop the batch.\n */\n\nimport 'server-only'\n\nimport type { Logger } from '@murumets-ee/core'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type { ImageStyle, MediaRecord } from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\nconst BATCH_SIZE = 100\n\nexport interface RegenerateOptions {\n db: PostgresJsDatabase\n storage: StorageClient\n logger?: Logger\n /** Current image styles to generate */\n styles: Record<string, ImageStyle>\n}\n\nexport interface RegenerateResult {\n /** Total image media records found */\n total: number\n /** Successfully reprocessed */\n processed: number\n /** Skipped (non-processable mimeType, download failed, etc.) */\n skipped: number\n /** Failed with errors */\n errors: number\n}\n\n/**\n * Regenerate variants for all image media.\n * Processes in batches of 100 to avoid memory pressure.\n */\nexport async function regenerateAllVariants(options: RegenerateOptions): Promise<RegenerateResult> {\n const { db, storage, logger, styles } = options\n const { AdminClient } = await import('@murumets-ee/entity/admin')\n const { Media } = await import('./entity.js')\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { eq } = await import('drizzle-orm')\n\n const admin = new AdminClient({ entity: Media, db, logger })\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered')\n\n const result: RegenerateResult = { total: 0, processed: 0, skipped: 0, errors: 0 }\n let offset = 0\n\n logger?.info({ styles: Object.keys(styles) }, 'Starting variant regeneration')\n\n // Process in batches\n while (true) {\n const batch = (await admin.findMany({\n where: eq(table.mediaType, 'image'),\n limit: BATCH_SIZE,\n offset,\n })) as unknown as MediaRecord[]\n\n if (batch.length === 0) break\n result.total += batch.length\n\n for (const record of batch) {\n try {\n // Skip non-processable images (SVG, GIF)\n if (!isProcessableImage(record.mimeType)) {\n result.skipped++\n continue\n }\n\n // Download original from storage\n const downloaded = await storage.download(record.fileKey)\n let buffer: Buffer\n if (Buffer.isBuffer(downloaded.body)) {\n buffer = downloaded.body\n } else {\n // ReadableStream → Buffer\n const chunks: Uint8Array[] = []\n const reader = downloaded.body.getReader()\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n if (value) chunks.push(value)\n }\n buffer = Buffer.concat(chunks)\n }\n\n // Generate new variants\n const processed = await processImage(buffer, styles)\n\n // Delete old variant files (best-effort)\n const oldFileRecord = await storage.getMetadata(record.fileKey)\n const oldVariants = (oldFileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n if (oldVariants) {\n for (const vKey of Object.values(oldVariants)) {\n await storage.delete(vKey).catch(() => {})\n }\n }\n\n // Upload new variants\n const newVariantKeys: Record<string, string> = {}\n const visibility = oldFileRecord?.visibility ?? 'public'\n for (const [styleName, variant] of processed.variants.entries()) {\n const vKey = deriveVariantKey(record.fileKey, styleName, variant.format)\n try {\n await storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${record.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: record.fileKey, style: styleName },\n })\n newVariantKeys[styleName] = vKey\n } catch (uploadErr) {\n logger?.warn(\n { style: styleName, key: vKey, error: uploadErr },\n 'Failed to upload regenerated variant (non-fatal)',\n )\n }\n }\n\n // Update original file's metadata with new variant keys\n if (Object.keys(newVariantKeys).length > 0) {\n await storage\n .updateMetadata(record.fileKey, {\n metadata: {\n ...(oldFileRecord?.metadata ?? {}),\n variants: newVariantKeys,\n },\n })\n .catch((metaErr: unknown) => {\n logger?.warn(\n { key: record.fileKey, error: metaErr },\n 'Failed to update variant metadata (non-fatal)',\n )\n })\n }\n\n result.processed++\n logger?.debug(\n { id: record.id, variants: Object.keys(newVariantKeys) },\n 'Regenerated variants',\n )\n } catch (err) {\n result.errors++\n logger?.error(\n { id: record.id, fileKey: record.fileKey, error: err },\n 'Failed to regenerate variants for media record',\n )\n }\n }\n\n offset += BATCH_SIZE\n if (batch.length < BATCH_SIZE) break\n }\n\n logger?.info(result, 'Variant regeneration complete')\n return result\n}\n"],"mappings":"iFA2CA,eAAsB,EAAsB,EAAuD,CACjG,GAAM,CAAE,KAAI,UAAS,SAAQ,UAAW,EAClC,CAAE,eAAgB,MAAM,OAAO,6BAC/B,CAAE,SAAU,MAAM,OAAO,yBACzB,CAAE,kBAAmB,MAAM,OAAO,mBAClC,CAAE,MAAO,MAAM,OAAO,eAEtB,EAAQ,IAAI,EAAY,CAAE,OAAQ,EAA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