@murumets-ee/media 0.4.3 → 0.4.4
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.
- package/dist/admin.d.mts.map +1 -1
- package/dist/admin.mjs +1 -1
- package/dist/admin.mjs.map +1 -1
- package/dist/client-B5gkUbdQ.mjs +2 -0
- package/dist/client-B5gkUbdQ.mjs.map +1 -0
- package/dist/client.d.mts +4 -3
- package/dist/client.d.mts.map +1 -1
- package/dist/client.mjs +1 -1
- package/dist/client.mjs.map +1 -1
- package/dist/image-styles-settings.d.mts +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/plugin.d.mts +1 -1
- package/dist/processing.d.mts +1 -1
- package/dist/query-client.d.mts +1 -1
- package/dist/resolve-image-styles-4f6J73QN.mjs +2 -0
- package/dist/resolve-image-styles-4f6J73QN.mjs.map +1 -0
- package/dist/resolve-image-styles-DBOs7I7d.mjs +2 -0
- package/dist/resolve-image-styles-DBOs7I7d.mjs.map +1 -0
- package/dist/{types-BBAbJooM.d.mts → types-BAbtNERx.d.mts} +1 -1
- package/dist/{types-BBAbJooM.d.mts.map → types-BAbtNERx.d.mts.map} +1 -1
- package/dist/{usage-D7Bn7Vvv.mjs → usage-DIuiD9UM.mjs} +1 -1
- package/dist/{usage-D7Bn7Vvv.mjs.map → usage-DIuiD9UM.mjs.map} +1 -1
- package/package.json +5 -5
- package/dist/client-B__UIAjI.mjs +0 -2
- package/dist/client-B__UIAjI.mjs.map +0 -1
package/dist/admin.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"admin.d.mts","names":[],"sources":["../src/admin/routes.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"admin.d.mts","names":[],"sources":["../src/admin/routes.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;iBAmdgB,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;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-
|
|
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-B5gkUbdQ.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{getApp:e}=await import(`@murumets-ee/core`),{resolveImageStyles:t}=await import(`./resolve-image-styles-4f6J73QN.mjs`),n=e();return c({imageStyles:await t(n,n.logger)})}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-DIuiD9UM.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{regenerateAllVariants:e}=await import(`./regenerate-variants-BFnFcnxm.mjs`),{getApp:t}=await import(`@murumets-ee/core`),{resolveImageStyles:n}=await import(`./resolve-image-styles-4f6J73QN.mjs`),{createStorageClient:o}=await import(`@murumets-ee/storage`),{getStorageConfig:s}=await import(`@murumets-ee/storage/plugin`),u=t(),d=await n(u,u.logger);if(!d||Object.keys(d).length===0)return l(`No image styles configured`,400);let f=o(s(),{app:u}),p=await e({db:u.db.readWrite,storage:f,logger:u.logger.child({media:!0}),styles:d});return i?.({action:`media.regenerate_variants`,userId:r.id,userName:r.name,metadata:{total:p.total,processed:p.processed,errors:p.errors}}),c(p)}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
|
package/dist/admin.mjs.map
CHANGED
|
@@ -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// 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 //\n // DB-first, plugin-config-fallback: we used to seed the settings DB from\n // the plugin config on plugin init, but that pulled in @murumets-ee/settings'\n // main entry (`server-only`) during any non-RSC bootstrap. Now the plugin\n // just holds the defaults in memory; the admin UI reads them here and\n // persists overrides via PUT.\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 { getMediaConfig } = await import('../plugin.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 let styles: Record<string, ImageStyle> = stored ?? {}\n if (!stored) {\n try {\n styles = getMediaConfig().imageStyles\n } catch {\n styles = {}\n }\n }\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 { getMediaConfig } = await import('../plugin.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 // DB overrides take priority; fall back to the plugin config's defaults.\n // `getMediaConfig()` throws if the plugin isn't initialized — treat that\n // as \"no config fallback available\" and rely solely on stored.\n const stored = await settingsClient.get('imageStyles')\n let styles: Record<string, ImageStyle> | null | undefined = stored\n if (!styles) {\n try {\n styles = getMediaConfig().imageStyles\n } catch {\n styles = null\n }\n }\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,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Q,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 the effective image styles, applying the\n // same DB → plugin-config → hardcoded-defaults waterfall that MediaClient\n // uses during uploads. See ../resolve-image-styles.ts for the rationale.\n if (segments.length === 1 && segments[0] === 'settings') {\n const { getApp } = await import('@murumets-ee/core')\n const { resolveImageStyles } = await import('../resolve-image-styles.js')\n const app = getApp()\n const styles = await resolveImageStyles(app, app.logger)\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 { regenerateAllVariants } = await import('../regenerate-variants.js')\n const { getApp } = await import('@murumets-ee/core')\n const { resolveImageStyles } = await import('../resolve-image-styles.js')\n const { createStorageClient } = await import('@murumets-ee/storage')\n const { getStorageConfig } = await import('@murumets-ee/storage/plugin')\n\n const app = getApp()\n const styles = await resolveImageStyles(app, app.logger)\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,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,OAAS,GAClF,MAAO,CAAE,MAAO,GAAO,MAAO,4BAA4B,EAAK,8BAA+B,CAEhG,GAAI,EAAM,SAAW,IAAA,KAAc,OAAO,EAAM,QAAW,UAAY,EAAM,QAAU,GACrF,MAAO,CACL,MAAO,GACP,MAAO,6BAA6B,EAAK,8BAC1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ACR,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;let{getApp:e}=await import(`@murumets-ee/core`),{resolveImageStyles:t}=await import(`./resolve-image-styles-4f6J73QN.mjs`),n=e();return this.imageStyles=await t(n,n.logger),this.imageStyles}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-B5gkUbdQ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"client-B5gkUbdQ.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 via the shared waterfall (settings DB → plugin\n * config → hardcoded defaults) and cache the result on this instance.\n * Call `invalidateImageStylesCache()` after a settings update.\n */\n private async resolveImageStyles(): Promise<Record<string, ImageStyle>> {\n if (this.imageStyles) return this.imageStyles\n const { getApp } = await import('@murumets-ee/core')\n const { resolveImageStyles } = await import('./resolve-image-styles.js')\n const app = getApp()\n this.imageStyles = await resolveImageStyles(app, app.logger)\n return this.imageStyles\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,KAQ3C,MAAc,oBAA0D,CACtE,GAAI,KAAK,YAAa,OAAO,KAAK,YAClC,GAAM,CAAE,UAAW,MAAM,OAAO,qBAC1B,CAAE,sBAAuB,MAAM,OAAO,uCACtC,EAAM,GAAQ,CAEpB,MADA,MAAK,YAAc,MAAM,EAAmB,EAAK,EAAI,OAAO,CACrD,KAAK,YAId,4BAAmC,CACjC,KAAK,YAAc,KAiBrB,MAAM,OACJ,EACA,EAC4B,CAE5B,IAAM,EAAa,MA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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,4 +1,4 @@
|
|
|
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-
|
|
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-BAbtNERx.mjs";
|
|
2
2
|
import { AdminClient } from "@murumets-ee/entity/admin";
|
|
3
3
|
import { StorageClient } from "@murumets-ee/storage";
|
|
4
4
|
|
|
@@ -16,8 +16,9 @@ declare class MediaClient {
|
|
|
16
16
|
private imageStyles;
|
|
17
17
|
constructor(config: MediaClientConfig);
|
|
18
18
|
/**
|
|
19
|
-
* Resolve image styles
|
|
20
|
-
*
|
|
19
|
+
* Resolve image styles via the shared waterfall (settings DB → plugin
|
|
20
|
+
* config → hardcoded defaults) and cache the result on this instance.
|
|
21
|
+
* Call `invalidateImageStylesCache()` after a settings update.
|
|
21
22
|
*/
|
|
22
23
|
private resolveImageStyles;
|
|
23
24
|
/** Clear cached styles so next access re-reads from settings DB. */
|
package/dist/client.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client.d.mts","names":[],"sources":["../src/client.ts"],"mappings":";;;;;KA0BK,WAAA,UAAqB,KAAA,CAAM,SAAA;AAAA,UAEf,iBAAA;EACf,KAAA,EAAO,WAAA,CAAY,WAAA;EACnB,OAAA,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,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-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;
|
|
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;let{getApp:e}=await import(`@murumets-ee/core`),{resolveImageStyles:t}=await import(`./resolve-image-styles-DBOs7I7d.mjs`),n=e();return this.imageStyles=await t(n,n.logger),this.imageStyles}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
|
package/dist/client.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
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":"2HAmCA,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,+BACvC,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,gBAGxC,MADA,MAAK,YADU,GAAgB,CACL,YACnB,KAAK,iBACN,CAEN,IAAM,EAAuC,CAC3C,UAAW,CAAE,MAAO,IAAK,OAAQ,IAAK,IAAK,QAAS,OAAQ,OAAQ,QAAS,GAAI,CAClF,CAED,MADA,MAAK,YAAc,EACZ,GAKX,4BAAmC,CACjC,KAAK,YAAc,KAiBrB,MAAM,OACJ,EACA,EAC4B,CAE5B,IAAM,EAAa,MAAM,KAAK,QAAQ,OAAO,EAAM,CACjD,SAAU,EAAQ,SAClB,SAAU,EAAQ,SAClB,KAAM,EAAQ,KACd,WAA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AW,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,CAOzC,eAAsB,EAAkB,EAA8C,CACpF,GAAM,CAAE,qBAAsB,MAAM,OAAO,6BAE3C,OAAO,IAAI,EAAY,CAAE,MADX,EAAkB,EAAM,CACN,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"}
|
|
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 via the shared waterfall (settings DB → plugin\n * config → hardcoded defaults) and cache the result on this instance.\n * Call `invalidateImageStylesCache()` after a settings update.\n */\n private async resolveImageStyles(): Promise<Record<string, ImageStyle>> {\n if (this.imageStyles) return this.imageStyles\n const { getApp } = await import('@murumets-ee/core')\n const { resolveImageStyles } = await import('./resolve-image-styles.js')\n const app = getApp()\n this.imageStyles = await resolveImageStyles(app, app.logger)\n return this.imageStyles\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":"2HAmCA,IAAa,EAAb,KAAyB,CACvB,MACA,QACA,YAEA,YAAY,EAA2B,CACrC,KAAK,MAAQ,EAAO,MACpB,KAAK,QAAU,EAAO,QACtB,KAAK,YAAc,EAAO,aAAe,KAQ3C,MAAc,oBAA0D,CACtE,GAAI,KAAK,YAAa,OAAO,KAAK,YAClC,GAAM,CAAE,UAAW,MAAM,OAAO,qBAC1B,CAAE,sBAAuB,MAAM,OAAO,uCACtC,EAAM,GAAQ,CAEpB,MADA,MAAK,YAAc,MAAM,EAAmB,EAAK,EAAI,OAAO,CACrD,KAAK,YAId,4BAAmC,CACjC,KAAK,YAAc,KAiBrB,MAAM,OACJ,E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O,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,CAOzC,eAAsB,EAAkB,EAA8C,CACpF,GAAM,CAAE,qBAAsB,MAAM,OAAO,6BAE3C,OAAO,IAAI,EAAY,CAAE,MADX,EAAkB,EAAM,CACN,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"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
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-
|
|
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-BAbtNERx.mjs";
|
|
2
2
|
import { defaultImageStyles, imageStylesSettings } from "./image-styles-settings.mjs";
|
|
3
3
|
//#region src/enrich.d.ts
|
|
4
4
|
/**
|
package/dist/plugin.d.mts
CHANGED
package/dist/processing.d.mts
CHANGED
package/dist/query-client.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { a as MediaRecord, l as Media } from "./types-
|
|
1
|
+
import { a as MediaRecord, l as Media } from "./types-BAbtNERx.mjs";
|
|
2
2
|
import { CountOptions, FindByIdOptions, FindManyOptions, QueryClient } from "@murumets-ee/entity/query";
|
|
3
3
|
|
|
4
4
|
//#region src/query-client.d.ts
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e={thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}};async function t(t,n){try{let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:n}=await import(`./image-styles-settings-5zpGEfKG.mjs`),r=await e(n,{app:t}).get(`imageStyles`);if(r&&Object.keys(r).length>0)return r}catch(e){n?.warn({err:e},`resolveImageStyles: settings DB read failed — falling back`)}try{let{getMediaConfig:e}=await import(`./plugin-DV7lvImm.mjs`);return e().imageStyles}catch{}return e}export{t as resolveImageStyles};
|
|
2
|
+
//# sourceMappingURL=resolve-image-styles-4f6J73QN.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-image-styles-4f6J73QN.mjs","names":[],"sources":["../src/resolve-image-styles.ts"],"sourcesContent":["/**\n * Image style resolution waterfall — single source of truth for the\n * \"which styles should we use?\" question.\n *\n * Priority: settings DB (user-configured) → plugin config (from `media()`\n * factory call) → hardcoded defaults (thumbnail only).\n *\n * This replaces three near-duplicate blocks (MediaClient's private\n * resolveImageStyles + two admin-route branches) that had drifted — the\n * routes didn't treat an empty stored object as \"fall back to config\"\n * while MediaClient did. Consolidating here keeps the behavior consistent\n * and logs the DB read failure so operators can diagnose issues instead\n * of silently getting defaults.\n *\n * Not cached. MediaClient wraps this with its own per-instance cache.\n */\n\nimport type { Logger, ToolkitApp } from '@murumets-ee/core'\nimport type { ImageStyle } from './types.js'\n\nconst HARDCODED_DEFAULTS: Record<string, ImageStyle> = {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n}\n\nexport async function resolveImageStyles(\n app: ToolkitApp,\n logger?: Logger,\n): Promise<Record<string, ImageStyle>> {\n // 1. Settings DB (source of truth once the admin UI has saved anything).\n // An empty record counts as \"nothing configured\" — fall through. This\n // matches the pre-extraction behavior of MediaClient and fixes the\n // routes' prior behavior of showing an empty UI in that case.\n try {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('./image-styles-settings.js')\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const stored = await settingsClient.get('imageStyles')\n if (stored && Object.keys(stored).length > 0) return stored\n } catch (err) {\n // Logging matters: prior silent catches masked real issues (e.g. DB\n // unavailable) as \"no styles\" and routed admins toward config fallback\n // when the real failure was infrastructure.\n logger?.warn({ err }, 'resolveImageStyles: settings DB read failed — falling back')\n }\n\n // 2. Plugin config (from the `media()` factory call in toolkit.config.ts).\n try {\n const { getMediaConfig } = await import('./plugin.js')\n return getMediaConfig().imageStyles\n } catch {\n // Plugin not initialized (e.g. unit tests that don't call media()) —\n // fall through to hardcoded defaults. No log: this is a valid mode.\n }\n\n // 3. Hardcoded baseline — a single thumbnail style so uploads don't fail\n // in a toolkit that never registered the plugin.\n return HARDCODED_DEFAULTS\n}\n"],"mappings":"AAoBA,MAAM,EAAiD,CACrD,UAAW,CAAE,MAAO,IAAK,OAAQ,IAAK,IAAK,QAAS,OAAQ,OAAQ,QAAS,GAAI,CAClF,CAED,eAAsB,EACpB,EACA,EACqC,CAKrC,GAAI,CACF,GAAM,CAAE,wBAAyB,MAAM,OAAO,yBACxC,CAAE,uBAAwB,MAAM,OAAO,wCAEvC,EAAS,MADQ,EAAqB,EAAqB,CAAE,MAAK,CAAC,CACrC,IAAI,cAAc,CACtD,GAAI,GAAU,OAAO,KAAK,EAAO,CAAC,OAAS,EAAG,OAAO,QAC9C,EAAK,CAIZ,GAAQ,KAAK,CAAE,MAAK,CAAE,6DAA6D,CAIrF,GAAI,CACF,GAAM,CAAE,kBAAmB,MAAM,OAAO,yBACxC,OAAO,GAAgB,CAAC,iBAClB,EAOR,OAAO"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
const e={thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}};async function t(t,n){try{let{createSettingsClient:e}=await import(`@murumets-ee/settings`),{imageStylesSettings:n}=await import(`./image-styles-settings.mjs`),r=await e(n,{app:t}).get(`imageStyles`);if(r&&Object.keys(r).length>0)return r}catch(e){n?.warn({err:e},`resolveImageStyles: settings DB read failed — falling back`)}try{let{getMediaConfig:e}=await import(`./plugin.mjs`);return e().imageStyles}catch{}return e}export{t as resolveImageStyles};
|
|
2
|
+
//# sourceMappingURL=resolve-image-styles-DBOs7I7d.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"resolve-image-styles-DBOs7I7d.mjs","names":[],"sources":["../src/resolve-image-styles.ts"],"sourcesContent":["/**\n * Image style resolution waterfall — single source of truth for the\n * \"which styles should we use?\" question.\n *\n * Priority: settings DB (user-configured) → plugin config (from `media()`\n * factory call) → hardcoded defaults (thumbnail only).\n *\n * This replaces three near-duplicate blocks (MediaClient's private\n * resolveImageStyles + two admin-route branches) that had drifted — the\n * routes didn't treat an empty stored object as \"fall back to config\"\n * while MediaClient did. Consolidating here keeps the behavior consistent\n * and logs the DB read failure so operators can diagnose issues instead\n * of silently getting defaults.\n *\n * Not cached. MediaClient wraps this with its own per-instance cache.\n */\n\nimport type { Logger, ToolkitApp } from '@murumets-ee/core'\nimport type { ImageStyle } from './types.js'\n\nconst HARDCODED_DEFAULTS: Record<string, ImageStyle> = {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n}\n\nexport async function resolveImageStyles(\n app: ToolkitApp,\n logger?: Logger,\n): Promise<Record<string, ImageStyle>> {\n // 1. Settings DB (source of truth once the admin UI has saved anything).\n // An empty record counts as \"nothing configured\" — fall through. This\n // matches the pre-extraction behavior of MediaClient and fixes the\n // routes' prior behavior of showing an empty UI in that case.\n try {\n const { createSettingsClient } = await import('@murumets-ee/settings')\n const { imageStylesSettings } = await import('./image-styles-settings.js')\n const settingsClient = createSettingsClient(imageStylesSettings, { app })\n const stored = await settingsClient.get('imageStyles')\n if (stored && Object.keys(stored).length > 0) return stored\n } catch (err) {\n // Logging matters: prior silent catches masked real issues (e.g. DB\n // unavailable) as \"no styles\" and routed admins toward config fallback\n // when the real failure was infrastructure.\n logger?.warn({ err }, 'resolveImageStyles: settings DB read failed — falling back')\n }\n\n // 2. Plugin config (from the `media()` factory call in toolkit.config.ts).\n try {\n const { getMediaConfig } = await import('./plugin.js')\n return getMediaConfig().imageStyles\n } catch {\n // Plugin not initialized (e.g. unit tests that don't call media()) —\n // fall through to hardcoded defaults. No log: this is a valid mode.\n }\n\n // 3. Hardcoded baseline — a single thumbnail style so uploads don't fail\n // in a toolkit that never registered the plugin.\n return HARDCODED_DEFAULTS\n}\n"],"mappings":"AAoBA,MAAM,EAAiD,CACrD,UAAW,CAAE,MAAO,IAAK,OAAQ,IAAK,IAAK,QAAS,OAAQ,OAAQ,QAAS,GAAI,CAClF,CAED,eAAsB,EACpB,EACA,EACqC,CAKrC,GAAI,CACF,GAAM,CAAE,wBAAyB,MAAM,OAAO,yBACxC,CAAE,uBAAwB,MAAM,OAAO,+BAEvC,EAAS,MADQ,EAAqB,EAAqB,CAAE,MAAK,CAAC,CACrC,IAAI,cAAc,CACtD,GAAI,GAAU,OAAO,KAAK,EAAO,CAAC,OAAS,EAAG,OAAO,QAC9C,EAAK,CAIZ,GAAQ,KAAK,CAAE,MAAK,CAAE,6DAA6D,CAIrF,GAAI,CACF,GAAM,CAAE,kBAAmB,MAAM,OAAO,gBACxC,OAAO,GAAgB,CAAC,iBAClB,EAOR,OAAO"}
|
|
@@ -152,4 +152,4 @@ interface MediaPluginConfig {
|
|
|
152
152
|
}
|
|
153
153
|
//#endregion
|
|
154
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-
|
|
155
|
+
//# sourceMappingURL=types-BAbtNERx.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types-
|
|
1
|
+
{"version":3,"file":"types-BAbtNERx.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"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import"server-only";import{findEntityUsages as e}from"@murumets-ee/entity/refs";async function t(t,n){let r=await e(`media`,t,n),{getApp:i}=await import(`@murumets-ee/core`),a=i();return r.map(e=>{let t=`field`,n=a.entities.get(e.sourceEntity);if(n){let r=n.allFields[e.sourceField];r&&r.type===`blocks`&&(t=`block`)}return{entityName:e.sourceEntity,entityId:e.sourceId,fieldName:e.sourceField,context:t}})}export{t as findMediaUsages};
|
|
2
|
-
//# sourceMappingURL=usage-
|
|
2
|
+
//# sourceMappingURL=usage-DIuiD9UM.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usage-
|
|
1
|
+
{"version":3,"file":"usage-DIuiD9UM.mjs","names":[],"sources":["../src/usage.ts"],"sourcesContent":["/**\n * Media usage lookup — finds all entity references to a media item.\n *\n * Delegates to the universal entity_refs tracking table (populated at write time\n * by AdminClient). One indexed query instead of scanning every table.\n *\n * Used by delete protection (409 Conflict) and the usage API endpoint.\n */\n\nimport 'server-only'\n\nimport { findEntityUsages } from '@murumets-ee/entity/refs'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\n\nexport interface MediaUsage {\n entityName: string\n entityId: string\n fieldName: string\n /** 'field' = entity column, 'block' = layout/blocks field */\n context: 'field' | 'block'\n}\n\n/**\n * Find all entities that reference a given media ID.\n *\n * @param mediaId - UUID of the media item to check\n * @param db - Database connection\n * @returns Array of usage records (empty if unreferenced)\n */\nexport async function findMediaUsages(\n mediaId: string,\n db: PostgresJsDatabase,\n): Promise<MediaUsage[]> {\n const usages = await findEntityUsages('media', mediaId, db)\n\n // Derive context from field name — blocks fields are tracked with their field name\n // which we can check against entity definitions. For simplicity, we check if the\n // source entity has a blocks-type field matching the source field name.\n const { getApp } = await import('@murumets-ee/core')\n const app = getApp()\n\n return usages.map((usage) => {\n let context: 'field' | 'block' = 'field'\n const entity = app.entities.get(usage.sourceEntity)\n if (entity) {\n const fieldConfig = entity.allFields[usage.sourceField]\n if (fieldConfig && fieldConfig.type === 'blocks') {\n context = 'block'\n }\n }\n\n return {\n entityName: usage.sourceEntity,\n entityId: usage.sourceId,\n fieldName: usage.sourceField,\n context,\n }\n })\n}\n"],"mappings":"gFA6BA,eAAsB,EACpB,EACA,EACuB,CACvB,IAAM,EAAS,MAAM,EAAiB,QAAS,EAAS,EAAG,CAKrD,CAAE,UAAW,MAAM,OAAO,qBAC1B,EAAM,GAAQ,CAEpB,OAAO,EAAO,IAAK,GAAU,CAC3B,IAAI,EAA6B,QAC3B,EAAS,EAAI,SAAS,IAAI,EAAM,aAAa,CACnD,GAAI,EAAQ,CACV,IAAM,EAAc,EAAO,UAAU,EAAM,aACvC,GAAe,EAAY,OAAS,WACtC,EAAU,SAId,MAAO,CACL,WAAY,EAAM,aAClB,SAAU,EAAM,SAChB,UAAW,EAAM,YACjB,UACD,EACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murumets-ee/media",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.4",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -56,12 +56,12 @@
|
|
|
56
56
|
"server-only": "^0.0.1",
|
|
57
57
|
"sharp": "^0.34.5",
|
|
58
58
|
"tailwind-merge": "^2.6.0",
|
|
59
|
-
"@murumets-ee/entity": "0.4.0",
|
|
60
|
-
"@murumets-ee/settings": "0.4.0",
|
|
61
|
-
"@murumets-ee/storage": "0.4.0",
|
|
62
59
|
"@murumets-ee/core": "0.4.0",
|
|
60
|
+
"@murumets-ee/settings": "0.4.0",
|
|
63
61
|
"@murumets-ee/db": "0.4.0",
|
|
64
|
-
"@murumets-ee/logging": "0.4.0"
|
|
62
|
+
"@murumets-ee/logging": "0.4.0",
|
|
63
|
+
"@murumets-ee/entity": "0.4.0",
|
|
64
|
+
"@murumets-ee/storage": "0.4.0"
|
|
65
65
|
},
|
|
66
66
|
"peerDependencies": {
|
|
67
67
|
"lucide-react": ">=0.400.0",
|
package/dist/client-B__UIAjI.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
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
|
|
@@ -1 +0,0 @@
|
|
|
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 Media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