@murumets-ee/media 0.15.3 → 0.16.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/admin.mjs +1 -1
- package/dist/{client-COKATA-9.mjs → client-CHGSsUxS.mjs} +2 -2
- package/dist/{client-COKATA-9.mjs.map → client-CHGSsUxS.mjs.map} +1 -1
- package/dist/client.mjs +1 -1
- package/dist/entity-B75A4L3p.mjs +2 -0
- package/dist/entity-B75A4L3p.mjs.map +1 -0
- package/dist/entity-D1YaFEh8.mjs +2 -0
- package/dist/entity-D1YaFEh8.mjs.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/{plugin-DIpXKYNZ.mjs → plugin-DZaF1BHS.mjs} +2 -2
- package/dist/{plugin-DIpXKYNZ.mjs.map → plugin-DZaF1BHS.mjs.map} +1 -1
- package/dist/plugin.mjs +1 -1
- package/dist/query-client.mjs +1 -1
- package/dist/{regenerate-variants-yX-66vqe.mjs → regenerate-variants-CuHnSFS5.mjs} +2 -2
- package/dist/{regenerate-variants-yX-66vqe.mjs.map → regenerate-variants-CuHnSFS5.mjs.map} +1 -1
- package/dist/{regenerate-variants-Dm3KCvDF.mjs → regenerate-variants-rNGOuUJc.mjs} +2 -2
- package/dist/{regenerate-variants-Dm3KCvDF.mjs.map → regenerate-variants-rNGOuUJc.mjs.map} +1 -1
- package/dist/{resolve-image-styles-DPelxnhW.mjs → resolve-image-styles-2sa0O7wU.mjs} +2 -2
- package/dist/{resolve-image-styles-DPelxnhW.mjs.map → resolve-image-styles-2sa0O7wU.mjs.map} +1 -1
- package/dist/{routes-DL5PZ3nJ.mjs → routes-BFRoQhZB.mjs} +2 -2
- package/dist/{routes-DL5PZ3nJ.mjs.map → routes-BFRoQhZB.mjs.map} +1 -1
- package/dist/types-BMW3aeEB.d.mts.map +1 -1
- package/package.json +8 -8
- package/dist/entity-B7zgx4yx.mjs +0 -2
- package/dist/entity-B7zgx4yx.mjs.map +0 -1
- package/dist/entity-TVTU7wS3.mjs +0 -2
- package/dist/entity-TVTU7wS3.mjs.map +0 -1
package/dist/admin.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{t as e}from"./routes-
|
|
1
|
+
import{t as e}from"./routes-BFRoQhZB.mjs";export{e as mediaRoutes};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import"./entity-
|
|
2
|
-
//# sourceMappingURL=client-
|
|
1
|
+
import"./entity-B75A4L3p.mjs";import{n as e,r as t,t as n}from"./variant-key-CFr3fR-n.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-2sa0O7wU.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-CHGSsUxS.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"client-COKATA-9.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,MAAM,KAAK,QAAQ,OAAO,EAAM,CACjD,SAAU,EAAQ,SAClB,SAAU,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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,GAC/B,CAAC,QAAQ,QAAS,IAAI"}
|
|
1
|
+
{"version":3,"file":"client-CHGSsUxS.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,MAAM,KAAK,QAAQ,OAAO,EAAM,CACjD,SAAU,EAAQ,SAClB,SAAU,EAAQ,SAClB,KAAM,EAAQ,KACd,WAAY,EAAQ,WACpB,WAAY,EAAQ,WACrB,CAAC,CAGE,EAAQ,EAAQ,OAAS,KACzB,EAAS,EAAQ,QAAU,KACzB,EAAsC,EAAE,CAE9C,GAAI,aAAgB,QAAU,EAAmB,EAAQ,SAAS,CAChE,GAAI,CAEF,IAAM,EAAY,MAAM,EAAa,EAAM,MADtB,KAAK,oBAAoB,CACI,CAGlD,EAAQ,EAAU,MAClB,EAAS,EAAU,OAGnB,IAAM,EAAa,EAAW,WAC9B,MAAM,QAAQ,IACZ,CAAC,GAAG,EAAU,SAAS,SAAS,CAAC,CAAC,IAA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zKyv.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-BI3pvJBZ.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
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{behavior as e,defineEntity as t,field as n,searchable as r}from"@murumets-ee/entity";const i=t({name:`media`,fields:{title:n.text({translatable:!0}),alt:n.text({translatable:!0}),description:n.text({translatable:!0}),fileKey:n.text({required:!0,indexed:!0}),filename:n.text({required:!0}),mimeType:n.text({required:!0,indexed:!0}),size:n.number({required:!0,integer:!0}),width:n.number({integer:!0}),height:n.number({integer:!0}),mediaType:n.select({options:[`image`,`video`,`audio`,`document`,`other`],required:!0,indexed:!0})},behaviors:[e.auditable(),r({fields:[`filename`,`fileKey`,`mimeType`,`title`,`alt`,`description`],fts:{fields:[`filename`,`title`,`alt`,`description`],language:`simple`},projection:e=>({id:e.id,label:e.title??e.filename,description:e.mimeType})})],scope:`global`,access:{view:`public`,create:`group.editor`,update:`group.editor`,delete:`group.admin`}});export{i as Media};
|
|
2
|
+
//# sourceMappingURL=entity-B75A4L3p.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-B75A4L3p.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * Pre-defined Media entity.\n *\n * Auto-registered by the media() plugin — users do NOT add this to their entities array.\n * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineConfig({\n * plugins: [storage(), media()],\n * // Media entity is auto-added — no need to list it here\n * })\n * ```\n */\n\nimport { behavior, defineEntity, field, searchable } from '@murumets-ee/entity'\n\nexport const Media = defineEntity({\n name: 'media',\n fields: {\n // --- Display metadata (editable, translatable) ---\n title: field.text({ translatable: true }),\n alt: field.text({ translatable: true }),\n description: field.text({ translatable: true }),\n\n // --- File linkage (set on upload, immutable in practice) ---\n /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */\n fileKey: field.text({ required: true, indexed: true }),\n\n // --- File metadata (set on upload) ---\n filename: field.text({ required: true }),\n mimeType: field.text({ required: true, indexed: true }),\n size: field.number({ required: true, integer: true }),\n\n // --- Image-specific metadata (client-measured on upload) ---\n width: field.number({ integer: true }),\n height: field.number({ integer: true }),\n\n // --- Classification (derived from mimeType on upload) ---\n mediaType: field.select({\n options: ['image', 'video', 'audio', 'document', 'other'] as const,\n required: true,\n indexed: true,\n }),\n },\n behaviors: [\n behavior.auditable(),\n // filename / fileKey / mimeType live on the main table.\n // title/alt/description are translatable — their tsvector goes on\n // media_translations so non-default-locale callers still get FTS.\n searchable({\n fields: ['filename', 'fileKey', 'mimeType', 'title', 'alt', 'description'],\n fts: {\n fields: ['filename', 'title', 'alt', 'description'],\n language: 'simple',\n },\n projection: (row) => ({\n id: row.id as string,\n label: (row.title as string | undefined) ?? (row.filename as string),\n description: row.mimeType as string,\n }),\n }),\n ],\n scope: 'global',\n access: {\n view: 'public',\n create: 'group.editor',\n update: 'group.editor',\n delete: 'group.admin',\n },\n})\n"],"mappings":"4FAmBA,MAAa,EAAQ,EAAa,CAChC,KAAM,QACN,OAAQ,CAEN,MAAO,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACzC,IAAK,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACvC,YAAa,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CAI/C,QAAS,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGtD,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,CAAC,CACxC,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CACvD,KAAM,EAAM,OAAO,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGrD,MAAO,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CACtC,OAAQ,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CAGvC,UAAW,EAAM,OAAO,CACtB,QAAS,CAAC,QAAS,QAAS,QAAS,WAAY,QAAQ,CACzD,SAAU,GACV,QAAS,GACV,CAAC,CACH,CACD,UAAW,CACT,EAAS,WAAW,CAIpB,EAAW,CACT,OAAQ,CAAC,WAAY,UAAW,WAAY,QAAS,MAAO,cAAc,CAC1E,IAAK,CACH,OAAQ,CAAC,WAAY,QAAS,MAAO,cAAc,CACnD,SAAU,SACX,CACD,WAAa,IAAS,CACpB,GAAI,EAAI,GACR,MAAQ,EAAI,OAAiC,EAAI,SACjD,YAAa,EAAI,SAClB,EACF,CAAC,CACH,CACD,MAAO,SACP,OAAQ,CACN,KAAM,SACN,OAAQ,eACR,OAAQ,eACR,OAAQ,cACT,CACF,CAAC"}
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
import{behavior as e,defineEntity as t,field as n,searchable as r}from"@murumets-ee/entity";var i=Object.defineProperty,a=((e,t)=>{let n={};for(var r in e)i(n,r,{get:e[r],enumerable:!0});return t||i(n,Symbol.toStringTag,{value:`Module`}),n})({Media:()=>o});const o=t({name:`media`,fields:{title:n.text({translatable:!0}),alt:n.text({translatable:!0}),description:n.text({translatable:!0}),fileKey:n.text({required:!0,indexed:!0}),filename:n.text({required:!0}),mimeType:n.text({required:!0,indexed:!0}),size:n.number({required:!0,integer:!0}),width:n.number({integer:!0}),height:n.number({integer:!0}),mediaType:n.select({options:[`image`,`video`,`audio`,`document`,`other`],required:!0,indexed:!0})},behaviors:[e.auditable(),r({fields:[`filename`,`fileKey`,`mimeType`,`title`,`alt`,`description`],fts:{fields:[`filename`,`title`,`alt`,`description`],language:`simple`},projection:e=>({id:e.id,label:e.title??e.filename,description:e.mimeType})})],scope:`global`,access:{view:`public`,create:`group.editor`,update:`group.editor`,delete:`group.admin`}});export{a as n,o as t};
|
|
2
|
+
//# sourceMappingURL=entity-D1YaFEh8.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"entity-D1YaFEh8.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * Pre-defined Media entity.\n *\n * Auto-registered by the media() plugin — users do NOT add this to their entities array.\n * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineConfig({\n * plugins: [storage(), media()],\n * // Media entity is auto-added — no need to list it here\n * })\n * ```\n */\n\nimport { behavior, defineEntity, field, searchable } from '@murumets-ee/entity'\n\nexport const Media = defineEntity({\n name: 'media',\n fields: {\n // --- Display metadata (editable, translatable) ---\n title: field.text({ translatable: true }),\n alt: field.text({ translatable: true }),\n description: field.text({ translatable: true }),\n\n // --- File linkage (set on upload, immutable in practice) ---\n /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */\n fileKey: field.text({ required: true, indexed: true }),\n\n // --- File metadata (set on upload) ---\n filename: field.text({ required: true }),\n mimeType: field.text({ required: true, indexed: true }),\n size: field.number({ required: true, integer: true }),\n\n // --- Image-specific metadata (client-measured on upload) ---\n width: field.number({ integer: true }),\n height: field.number({ integer: true }),\n\n // --- Classification (derived from mimeType on upload) ---\n mediaType: field.select({\n options: ['image', 'video', 'audio', 'document', 'other'] as const,\n required: true,\n indexed: true,\n }),\n },\n behaviors: [\n behavior.auditable(),\n // filename / fileKey / mimeType live on the main table.\n // title/alt/description are translatable — their tsvector goes on\n // media_translations so non-default-locale callers still get FTS.\n searchable({\n fields: ['filename', 'fileKey', 'mimeType', 'title', 'alt', 'description'],\n fts: {\n fields: ['filename', 'title', 'alt', 'description'],\n language: 'simple',\n },\n projection: (row) => ({\n id: row.id as string,\n label: (row.title as string | undefined) ?? (row.filename as string),\n description: row.mimeType as string,\n }),\n }),\n ],\n scope: 'global',\n access: {\n view: 'public',\n create: 'group.editor',\n update: 'group.editor',\n delete: 'group.admin',\n },\n})\n"],"mappings":"iQAmBA,MAAa,EAAQ,EAAa,CAChC,KAAM,QACN,OAAQ,CAEN,MAAO,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACzC,IAAK,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACvC,YAAa,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CAI/C,QAAS,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGtD,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,CAAC,CACxC,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CACvD,KAAM,EAAM,OAAO,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGrD,MAAO,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CACtC,OAAQ,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CAGvC,UAAW,EAAM,OAAO,CACtB,QAAS,CAAC,QAAS,QAAS,QAAS,WAAY,QAAQ,CACzD,SAAU,GACV,QAAS,GACV,CAAC,CACH,CACD,UAAW,CACT,EAAS,WAAW,CAIpB,EAAW,CACT,OAAQ,CAAC,WAAY,UAAW,WAAY,QAAS,MAAO,cAAc,CAC1E,IAAK,CACH,OAAQ,CAAC,WAAY,QAAS,MAAO,cAAc,CACnD,SAAU,SACX,CACD,WAAa,IAAS,CACpB,GAAI,EAAI,GACR,MAAQ,EAAI,OAAiC,EAAI,SACjD,YAAa,EAAI,SAClB,EACF,CAAC,CACH,CACD,MAAO,SACP,OAAQ,CACN,KAAM,SACN,OAAQ,eACR,OAAQ,eACR,OAAQ,cACT,CACF,CAAC"}
|
package/dist/index.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{t as e}from"./entity-
|
|
1
|
+
import{t as e}from"./entity-D1YaFEh8.mjs";import{defaultImageStyles as t,imageStylesSettings as n}from"./image-styles-settings.mjs";import"server-only";async function r(e,t,n=`thumbnail`){let r=Object.entries(e.allFields).filter(([,e])=>e.type===`media`).map(([e])=>e);if(r.length===0)return;let i=new Set;for(let e of t)for(let t of r){let n=e[t];typeof n==`string`&&n.length>0&&i.add(n)}if(i.size===0)return;let{getMediaClient:a}=await import(`./client.mjs`),o=await(await a()).getVariantUrls([...i],n);for(let e of t)for(let t of r){let n=e[t];typeof n==`string`&&o.has(n)&&(e[`${t}Url`]=o.get(n))}}export{e as Media,t as defaultImageStyles,r as enrichWithMediaUrls,n as imageStylesSettings};
|
|
2
2
|
//# sourceMappingURL=index.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import"./routes-
|
|
2
|
-
//# sourceMappingURL=plugin-
|
|
1
|
+
import"./routes-BFRoQhZB.mjs";import"./entity-B75A4L3p.mjs";import"./image-styles-settings-B4QOt7Ve.mjs";import"@murumets-ee/media/image-styles";function e(){throw Error(`@murumets-ee/media plugin not initialized. Add media() to your plugins array.`)}export{e as getMediaConfig};
|
|
2
|
+
//# sourceMappingURL=plugin-DZaF1BHS.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"plugin-
|
|
1
|
+
{"version":3,"file":"plugin-DZaF1BHS.mjs","names":[],"sources":["../src/plugin.ts"],"sourcesContent":["/**\n * Media plugin — auto-registers the Media entity, mounts admin routes, and\n * contributes sidebar + default-route scaffolding for the admin shell.\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineLumiConfig({\n * plugins: [\n * storage(),\n * media({ maxUploadSize: 10 * 1024 * 1024 }),\n * ],\n * })\n * ```\n */\n\nimport type { Plugin, PluginSettingsDefinition } from '@murumets-ee/core'\n// Self-reference the `./image-styles` subpath (not the internal source\n// files) so the plugin bundle externalizes the React components instead\n// of inlining them — the subpath is built separately with a `'use client'`\n// banner, and Next.js needs to see that boundary preserved.\nimport { ImageStylesManager, RegenerateVariantsAction } from '@murumets-ee/media/image-styles'\nimport { mediaRoutes } from './admin/routes.js'\nimport { Media } from './entity.js'\nimport { imageStylesSettings } from './image-styles-settings.js'\nimport type { MediaPluginConfig } from './types.js'\n\nlet _mediaConfig: Required<MediaPluginConfig> | null = null\n\n/**\n * Get the resolved media plugin configuration.\n * Throws if plugin not initialized.\n */\nexport function getMediaConfig(): Required<MediaPluginConfig> {\n if (!_mediaConfig) {\n throw new Error('@murumets-ee/media plugin not initialized. Add media() to your plugins array.')\n }\n return _mediaConfig\n}\n\n/**\n * Media plugin factory.\n *\n * - Registers the Media entity\n * - Mounts the media admin API routes\n * - Contributes sidebar + default-route metadata\n * - Validates that @murumets-ee/storage + @murumets-ee/settings are registered at init\n * - Captures resolved configuration for `getMediaConfig()`\n */\nexport function media(config?: MediaPluginConfig): Plugin {\n const resolvedConfig: Required<MediaPluginConfig> = {\n acceptedTypes: config?.acceptedTypes ?? ['image/*', 'video/*', 'audio/*', 'application/pdf'],\n maxUploadSize: config?.maxUploadSize ?? 50 * 1024 * 1024,\n defaultVisibility: config?.defaultVisibility ?? 'public',\n imageStyles: config?.imageStyles ?? {\n thumbnail: { width: 200, height: 200, fit: 'cover', format: 'webp', quality: 80 },\n },\n }\n\n return {\n name: '@murumets-ee/media',\n shared: {\n // Self-contributes the media.imageStyles namespace. The merge engine\n // auto-derives the permission resource (`settings:media.imageStyles`)\n // and the sidebar entry under \"Settings\" group; the settings plugin's\n // init hook picks it up for route dispatch. Apps don't have to wire\n // anything — adding `media()` to plugins is enough.\n settings: [imageStylesSettings as PluginSettingsDefinition],\n },\n server: {\n entities: [Media],\n routes: [mediaRoutes()],\n init: async (app) => {\n if (!app.plugins.has('@murumets-ee/storage')) {\n throw new Error(\n '@murumets-ee/media requires @murumets-ee/storage plugin. ' +\n 'Add storage() before media() in your plugins array.',\n )\n }\n\n if (!app.plugins.has('@murumets-ee/settings')) {\n throw new Error(\n '@murumets-ee/media requires @murumets-ee/settings plugin. ' +\n 'Add settings() before media() in your plugins array.',\n )\n }\n\n _mediaConfig = resolvedConfig\n\n // Image style defaults live in the plugin config and are exposed via\n // `getMediaConfig()`. Persistent overrides go to the settings DB\n // through the generic settings API at `/api/admin/settings/media.imageStyles`\n // — see `image-styles-settings.ts`. `resolveImageStyles` reads\n // DB-first, config-fallback.\n //\n // We deliberately do NOT seed the DB here. Init must stay lightweight:\n // it runs in every context (Next.js server, Next.js build, CLI commands).\n // The prior eager-seed path pulled in `@murumets-ee/settings`'s main entry —\n // which carries `import 'server-only'` — and crashed non-RSC Node bootstrap.\n\n app.logger.info(\n {\n acceptedTypes: resolvedConfig.acceptedTypes,\n maxUploadSize: resolvedConfig.maxUploadSize,\n defaultVisibility: resolvedConfig.defaultVisibility,\n },\n 'Media plugin initialized',\n )\n },\n },\n adminUi: {\n sidebar: [\n {\n id: 'media',\n group: 'Library',\n label: 'Media',\n href: '/admin/media',\n iconName: 'image',\n },\n ],\n defaultRoutes: [\n {\n path: 'media',\n factory: 'MediaListPage',\n nav: { label: 'Media', iconName: 'image', group: 'Library' },\n },\n {\n path: 'media/[id]',\n factory: 'MediaEditPage',\n },\n ],\n // Self-contributes the rich image-styles editor and the\n // \"Regenerate All Variants\" action. Apps don't have to wire either\n // — they appear automatically on /admin/settings/media.imageStyles\n // (route + sidebar entry auto-derived from `shared.settings`).\n settingRenderers: {\n 'media.imageStyles': ImageStylesManager,\n },\n settingsActions: {\n 'media.imageStyles': RegenerateVariantsAction,\n },\n },\n }\n}\n"],"mappings":"iJAkCA,SAAgB,GAA8C,CAE1D,MAAU,MAAM,gFAAgF"}
|
package/dist/plugin.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{t as e}from"./entity-
|
|
1
|
+
import{t as e}from"./entity-D1YaFEh8.mjs";import{imageStylesSettings as t}from"./image-styles-settings.mjs";import{ImageStylesManager as n,RegenerateVariantsAction as r}from"@murumets-ee/media/image-styles";const i=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function a(e){return e!==void 0&&i.test(e)}let o=null;async function s(){return o||=(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})})(),o}async function c(){let{createAdminClient:e}=await import(`@murumets-ee/core/clients`),{MediaClient:t}=await import(`./client.mjs`),{Media:n}=await import(`./entity-D1YaFEh8.mjs`).then(e=>e.n),r=await s();return new t({admin:e(n),storage:r})}function l(e,t=200){return new Response(JSON.stringify(e),{status:t,headers:{"Content-Type":`application/json`}})}function u(e,t){return l({error:e},t)}async function d(e,{segments:t}){let{isStorageConfigured:n,getStorageConfigReason:r}=await import(`@murumets-ee/storage`);if(t.length===2&&t[1]===`usage`){let e=t[0];if(!a(e))return u(`Invalid media ID format`,400);let{findMediaUsages:n}=await import(`./usage.mjs`),{getApp:r}=await import(`@murumets-ee/core`);return l({usages:await n(e,r().db.readWrite)})}let i=t.length>0?t[0]:void 0;if(i!==void 0&&!a(i))return u(`Invalid media ID format`,400);if(!n()){let e=r()??`Storage not configured`;return t.length>0?l({error:e,configured:!1,reason:e},503):l({items:[],total:0,configured:!1,reason:e})}let o=await c();if(i!==void 0){let e=await o.findById(i);if(!e)return u(`Media not found`,404);let t=await o.getUrl(i);return l({...e,url:t})}let s=new URL(e.url),d=s.searchParams.get(`search`)??void 0,f=s.searchParams.get(`mediaType`)??void 0,p=Math.min(Math.max(Number(s.searchParams.get(`limit`))||24,1),100),m=Math.max(Number(s.searchParams.get(`offset`))||0,0),h=await o.findMany({...d!==void 0&&{search:d},...f!==void 0&&{mediaType:f},limit:p,offset:m}),g=h.items.map(e=>e.id),[_,v]=await Promise.all([o.getUrls(g),o.getVariantUrls(g,`thumbnail`)]);return l({items:h.items.map(e=>{let t=v.get(e.id);return{id:e.id,title:e.title??null,alt:e.alt??null,filename:e.filename,mimeType:e.mimeType,size:e.size,mediaType:e.mediaType,url:_.get(e.id)??``,...t!==void 0&&{thumbnailUrl:t},width:e.width??null,height:e.height??null}}),total:h.total})}async function f(e,{segments:t,user:n,audit:r,checkPermission:i}){let{isStorageConfigured:a,getStorageConfigReason:o}=await import(`@murumets-ee/storage`);if(t.length===1&&t[0]===`regenerate-variants`){if(!i(`media`,`create`))return u(`Forbidden: media create permission required for variant regeneration`,403);if(!a())return u(o()??`Storage not configured`,503);let{regenerateAllVariants:e}=await import(`./regenerate-variants-rNGOuUJc.mjs`),{getApp:t,getContext:s}=await import(`@murumets-ee/core`),{resolveImageStyles:c}=await import(`./resolve-image-styles-BI3pvJBZ.mjs`),{createStorageClient:d}=await import(`@murumets-ee/storage`),{getStorageConfig:f}=await import(`@murumets-ee/storage/plugin`),p=t(),m=await c(p,p.logger);if(!m||Object.keys(m).length===0)return u(`No image styles configured`,400);let h=d(f(),{app:p}),g=await e({db:p.db.readWrite,storage:h,logger:p.logger.child({media:!0}),styles:m,contextResolver:()=>{let e=s();if(!(!e?.user||!e?.checker))return{user:e.user,checker:e.checker,...e.scope!==void 0&&{scope:e.scope}}}});return r?.({action:`media.regenerate_variants`,userId:n.id,...n.name!==void 0&&{userName:n.name},metadata:{total:g.total,processed:g.processed,errors:g.errors}}),l(g)}if(!i(`media`,`create`))return u(`Forbidden: media create permission required for upload`,403);if(!a())return u(o()??`Storage not configured`,503);let s=await c(),d=(await e.formData()).get(`file`);if(!d||d.size===0)return u(`No file provided`,400);if(d.size>50*1024*1024)return u(`File too large: ${(d.size/1024/1024).toFixed(1)} MB exceeds 50 MB limit`,400);let f=Buffer.from(await d.arrayBuffer()),{detectMimeType:p}=await import(`@murumets-ee/storage`),{mimeType:m,mismatch:h}=await p(f,d.type||`application/octet-stream`);if(h)return u(`File content doesn't match declared type: claimed ${d.type}, detected ${m}`,400);let g=await s.upload(f,{filename:d.name,mimeType:m,size:d.size,uploadedBy:n.id}),_={id:g.media.id,title:g.media.title??null,alt:g.media.alt??null,filename:g.media.filename,mimeType:g.media.mimeType,size:g.media.size,mediaType:g.media.mediaType,url:g.url,width:g.media.width??null,height:g.media.height??null};return r?.({action:`media.upload`,entityType:`media`,entityId:g.media.id,userId:n.id,...n.name!==void 0&&{userName:n.name},changes:{filename:g.media.filename,mimeType:g.media.mimeType,size:g.media.size,mediaType:g.media.mediaType}}),l(_,201)}async function p(e,{segments:t,user:n,audit:r,checkPermission:i}){if(!i(`media`,`delete`))return u(`Forbidden: media delete permission required`,403);if(t.length===0)return u(`Media ID required`,400);let o=t[0];if(!a(o))return u(`Invalid media ID format`,400);let{isStorageConfigured:s,getStorageConfigReason:d}=await import(`@murumets-ee/storage`);return s()?(await(await c()).delete(o),r?.({action:`media.delete`,entityType:`media`,entityId:o,userId:n.id,...n.name!==void 0&&{userName:n.name}}),l({deleted:1})):u(d()??`Storage not configured`,503)}function m(){return{prefix:`media`,resource:`media`,actions:[`view`,`create`,`update`,`delete`],handlers:{GET:d,POST:f,DELETE:p}}}let h=null;function g(){if(!h)throw Error(`@murumets-ee/media plugin not initialized. Add media() to your plugins array.`);return h}function _(i){let a={acceptedTypes:i?.acceptedTypes??[`image/*`,`video/*`,`audio/*`,`application/pdf`],maxUploadSize:i?.maxUploadSize??50*1024*1024,defaultVisibility:i?.defaultVisibility??`public`,imageStyles:i?.imageStyles??{thumbnail:{width:200,height:200,fit:`cover`,format:`webp`,quality:80}}};return{name:`@murumets-ee/media`,shared:{settings:[t]},server:{entities:[e],routes:[m()],init:async e=>{if(!e.plugins.has(`@murumets-ee/storage`))throw Error(`@murumets-ee/media requires @murumets-ee/storage plugin. Add storage() before media() in your plugins array.`);if(!e.plugins.has(`@murumets-ee/settings`))throw Error(`@murumets-ee/media requires @murumets-ee/settings plugin. Add settings() before media() in your plugins array.`);h=a,e.logger.info({acceptedTypes:a.acceptedTypes,maxUploadSize:a.maxUploadSize,defaultVisibility:a.defaultVisibility},`Media plugin initialized`)}},adminUi:{sidebar:[{id:`media`,group:`Library`,label:`Media`,href:`/admin/media`,iconName:`image`}],defaultRoutes:[{path:`media`,factory:`MediaListPage`,nav:{label:`Media`,iconName:`image`,group:`Library`}},{path:`media/[id]`,factory:`MediaEditPage`}],settingRenderers:{"media.imageStyles":n},settingsActions:{"media.imageStyles":r}}}}export{g as getMediaConfig,_ as media};
|
|
2
2
|
//# sourceMappingURL=plugin.mjs.map
|
package/dist/query-client.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
var e=class{query;constructor(e){this.query=e.query}async findById(e,t){return this.query.findById(e,t)}async findMany(e){return this.query.findMany(e)}async count(e){return this.query.count(e)}};async function t(){let{createQueryClient:t}=await import(`@murumets-ee/core/clients`),{Media:n}=await import(`./entity-
|
|
1
|
+
var e=class{query;constructor(e){this.query=e.query}async findById(e,t){return this.query.findById(e,t)}async findMany(e){return this.query.findMany(e)}async count(e){return this.query.count(e)}};async function t(){let{createQueryClient:t}=await import(`@murumets-ee/core/clients`),{Media:n}=await import(`./entity-D1YaFEh8.mjs`).then(e=>e.n);return new e({query:t(n)})}export{e as MediaQueryClient,t as createMediaQueryClient};
|
|
2
2
|
//# sourceMappingURL=query-client.mjs.map
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{n as e,r as t,t as n}from"./variant-key-CFr3fR-n.mjs";import"server-only";async function r(r){let{db:i,storage:a,logger:o,styles:s}=r,{AdminClient:c}=await import(`@murumets-ee/entity/admin`),{Media:l}=await import(`./entity-
|
|
2
|
-
//# sourceMappingURL=regenerate-variants-
|
|
1
|
+
import{n as e,r as t,t as n}from"./variant-key-CFr3fR-n.mjs";import"server-only";async function r(r){let{db:i,storage:a,logger:o,styles:s}=r,{AdminClient:c}=await import(`@murumets-ee/entity/admin`),{Media:l}=await import(`./entity-B75A4L3p.mjs`),{schemaRegistry:u}=await import(`@murumets-ee/db`),{eq:d}=await import(`drizzle-orm`),f=new c({entity:l,db:i,logger:o,contextResolver:r.contextResolver}),p=u.get(`media`);if(!p)throw Error(`Media schema not registered`);let m={total:0,processed:0,skipped:0,errors:0},h=0;for(o?.info({styles:Object.keys(s)},`Starting variant regeneration`);;){let r=await f.findMany({where:d(p.mediaType,`image`),limit:100,offset:h});if(r.length===0)break;m.total+=r.length;for(let i of r)try{if(!e(i.mimeType)){m.skipped++;continue}let r=await a.download(i.fileKey),c;if(Buffer.isBuffer(r.body))c=r.body;else{let e=[],t=r.body.getReader();for(;;){let{done:n,value:r}=await t.read();if(n)break;r&&e.push(r)}c=Buffer.concat(e)}let l=await t(c,s),u=await a.getMetadata(i.fileKey),d=u?.metadata?.variants;d&&await Promise.all(Object.values(d).map(e=>a.delete(e).catch(()=>{})));let f={},p=u?.visibility??`public`,h=await Promise.all([...l.variants].map(async([e,t])=>{let r=n(i.fileKey,e,t.format);try{return await a.upload(t.buffer,{key:r,filename:`${e}_${i.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:p,metadata:{variantOf:i.fileKey,style:e}}),{styleName:e,vKey:r,ok:!0}}catch(t){return o?.warn({style:e,key:r,error:t},`Failed to upload regenerated variant (non-fatal)`),{styleName:e,vKey:r,ok:!1}}}));for(let e of h)e.ok&&(f[e.styleName]=e.vKey);Object.keys(f).length>0&&await a.updateMetadata(i.fileKey,{metadata:{...u?.metadata??{},variants:f}}).catch(e=>{o?.warn({key:i.fileKey,error:e},`Failed to update variant metadata (non-fatal)`)}),m.processed++,o?.debug({id:i.id,variants:Object.keys(f)},`Regenerated variants`)}catch(e){m.errors++,o?.error({id:i.id,fileKey:i.fileKey,error:e},`Failed to regenerate variants for media record`)}if(h+=100,r.length<100)break}return o?.info(m,`Variant regeneration complete`),m}export{r as regenerateAllVariants};
|
|
2
|
+
//# sourceMappingURL=regenerate-variants-CuHnSFS5.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regenerate-variants-
|
|
1
|
+
{"version":3,"file":"regenerate-variants-CuHnSFS5.mjs","names":[],"sources":["../src/regenerate-variants.ts"],"sourcesContent":["/**\n * Batch variant regeneration — reprocesses all image media with updated styles.\n *\n * Downloads each original from storage, generates new variants via Sharp,\n * cleans up old variant files, and uploads new ones.\n *\n * Per-image errors are logged but don't stop the batch.\n */\n\nimport 'server-only'\n\nimport type { Logger } from '@murumets-ee/core'\nimport type { ContextResolver } from '@murumets-ee/entity'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type { ImageStyle } from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\nconst BATCH_SIZE = 100\n\nexport interface RegenerateOptions {\n db: PostgresJsDatabase\n storage: StorageClient\n logger?: Logger\n /** Current image styles to generate */\n styles: Record<string, ImageStyle>\n /** Security context resolver — passed through to AdminClient. */\n contextResolver?: ContextResolver\n}\n\nexport interface RegenerateResult {\n /** Total image media records found */\n total: number\n /** Successfully reprocessed */\n processed: number\n /** Skipped (non-processable mimeType, download failed, etc.) */\n skipped: number\n /** Failed with errors */\n errors: number\n}\n\n/**\n * Regenerate variants for all image media.\n * Processes in batches of 100 to avoid memory pressure.\n */\nexport async function regenerateAllVariants(options: RegenerateOptions): Promise<RegenerateResult> {\n const { db, storage, logger, styles } = options\n const { AdminClient } = await import('@murumets-ee/entity/admin')\n const { Media } = await import('./entity.js')\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { eq } = await import('drizzle-orm')\n\n const admin = new AdminClient<typeof Media.allFields>({\n entity: Media,\n db,\n logger,\n contextResolver: options.contextResolver,\n })\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered')\n\n const result: RegenerateResult = { total: 0, processed: 0, skipped: 0, errors: 0 }\n let offset = 0\n\n logger?.info({ styles: Object.keys(styles) }, 'Starting variant regeneration')\n\n // Process in batches\n while (true) {\n const batch = await admin.findMany({\n where: eq(table.mediaType, 'image'),\n limit: BATCH_SIZE,\n offset,\n })\n\n if (batch.length === 0) break\n result.total += batch.length\n\n for (const record of batch) {\n try {\n // Skip non-processable images (SVG, GIF)\n if (!isProcessableImage(record.mimeType)) {\n result.skipped++\n continue\n }\n\n // Download original from storage\n const downloaded = await storage.download(record.fileKey)\n let buffer: Buffer\n if (Buffer.isBuffer(downloaded.body)) {\n buffer = downloaded.body\n } else {\n // ReadableStream → Buffer\n const chunks: Uint8Array[] = []\n const reader = downloaded.body.getReader()\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n if (value) chunks.push(value)\n }\n buffer = Buffer.concat(chunks)\n }\n\n // Generate new variants\n const processed = await processImage(buffer, styles)\n\n // Delete old variant files (best-effort, parallel)\n const oldFileRecord = await storage.getMetadata(record.fileKey)\n const oldVariants = (oldFileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n if (oldVariants) {\n await Promise.all(\n Object.values(oldVariants).map((vKey) => storage.delete(vKey).catch(() => {})),\n )\n }\n\n // Upload new variants (parallel)\n const newVariantKeys: Record<string, string> = {}\n const visibility = oldFileRecord?.visibility ?? 'public'\n const uploadResults = await Promise.all(\n [...processed.variants].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(record.fileKey, styleName, variant.format)\n try {\n await storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${record.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: record.fileKey, style: styleName },\n })\n return { styleName, vKey, ok: true as const }\n } catch (uploadErr) {\n logger?.warn(\n { style: styleName, key: vKey, error: uploadErr },\n 'Failed to upload regenerated variant (non-fatal)',\n )\n return { styleName, vKey, ok: false as const }\n }\n }),\n )\n for (const r of uploadResults) {\n if (r.ok) newVariantKeys[r.styleName] = r.vKey\n }\n\n // Update original file's metadata with new variant keys\n if (Object.keys(newVariantKeys).length > 0) {\n await storage\n .updateMetadata(record.fileKey, {\n metadata: {\n ...(oldFileRecord?.metadata ?? {}),\n variants: newVariantKeys,\n },\n })\n .catch((metaErr: unknown) => {\n logger?.warn(\n { key: record.fileKey, error: metaErr },\n 'Failed to update variant metadata (non-fatal)',\n )\n })\n }\n\n result.processed++\n logger?.debug(\n { id: record.id, variants: Object.keys(newVariantKeys) },\n 'Regenerated variants',\n )\n } catch (err) {\n result.errors++\n logger?.error(\n { id: record.id, fileKey: record.fileKey, error: err },\n 'Failed to regenerate variants for media record',\n )\n }\n }\n\n offset += BATCH_SIZE\n if (batch.length < BATCH_SIZE) break\n }\n\n logger?.info(result, 'Variant regeneration complete')\n return result\n}\n"],"mappings":"iFA8CA,eAAsB,EAAsB,EAAuD,CACjG,GAAM,CAAE,KAAI,UAAS,SAAQ,UAAW,EAClC,CAAE,eAAgB,MAAM,OAAO,6BAC/B,CAAE,SAAU,MAAM,OAAO,yBACzB,CAAE,kBAAmB,MAAM,OAAO,mBAClC,CAAE,MAAO,MAAM,OAAO,eAEtB,EAAQ,IAAI,EAAoC,CACpD,OAAQ,EACR,KACA,SACA,gBAAiB,EAAQ,gBAC1B,CAAC,CACI,EAAQ,EAAe,IAAI,QAAQ,CACzC,GAAI,CAAC,EAAO,MAAU,MAAM,8BAA8B,CAE1D,IAAM,EAA2B,CAAE,MAAO,EAAG,UAAW,EAAG,QAAS,EAAG,OAAQ,EAAG,CAC9E,EAAS,EAKb,IAHA,GAAQ,KAAK,CAAE,OAAQ,OAAO,KAAK,EAAO,CAAE,CAAE,gCAAgC,GAGjE,CACX,IAAM,EAAQ,MAAM,EAAM,SAAS,CACjC,MAAO,EAAG,EAAM,UAAW,QAAQ,CACnC,MAAO,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AO,EAAW,CACjD,mDACD,CACM,CAAE,YAAW,OAAM,GAAI,GAAgB,GAEhD,CACH,CACD,IAAK,IAAM,KAAK,EACV,EAAE,KAAI,EAAe,EAAE,WAAa,EAAE,MAIxC,OAAO,KAAK,EAAe,CAAC,OAAS,GACvC,MAAM,EACH,eAAe,EAAO,QAAS,CAC9B,SAAU,CACR,GAAI,GAAe,UAAY,EAAE,CACjC,SAAU,EACX,CACF,CAAC,CACD,MAAO,GAAqB,CAC3B,GAAQ,KACN,CAAE,IAAK,EAAO,QAAS,MAAO,EAAS,CACvC,gDACD,EACD,CAGN,EAAO,YACP,GAAQ,MACN,CAAE,GAAI,EAAO,GAAI,SAAU,OAAO,KAAK,EAAe,CAAE,CACxD,uBACD,OACM,EAAK,CACZ,EAAO,SACP,GAAQ,MACN,CAAE,GAAI,EAAO,GAAI,QAAS,EAAO,QAAS,MAAO,EAAK,CACtD,iDACD,CAKL,GADA,GAAU,IACN,EAAM,OAAS,IAAY,MAIjC,OADA,GAAQ,KAAK,EAAQ,gCAAgC,CAC9C"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import{i as e,r as t,t as n}from"./variant-key-gVMhzKyv.mjs";import"server-only";async function r(r){let{db:i,storage:a,logger:o,styles:s}=r,{AdminClient:c}=await import(`@murumets-ee/entity/admin`),{Media:l}=await import(`./entity-
|
|
2
|
-
//# sourceMappingURL=regenerate-variants-
|
|
1
|
+
import{i as e,r as t,t as n}from"./variant-key-gVMhzKyv.mjs";import"server-only";async function r(r){let{db:i,storage:a,logger:o,styles:s}=r,{AdminClient:c}=await import(`@murumets-ee/entity/admin`),{Media:l}=await import(`./entity-D1YaFEh8.mjs`).then(e=>e.n),{schemaRegistry:u}=await import(`@murumets-ee/db`),{eq:d}=await import(`drizzle-orm`),f=new c({entity:l,db:i,logger:o,contextResolver:r.contextResolver}),p=u.get(`media`);if(!p)throw Error(`Media schema not registered`);let m={total:0,processed:0,skipped:0,errors:0},h=0;for(o?.info({styles:Object.keys(s)},`Starting variant regeneration`);;){let r=await f.findMany({where:d(p.mediaType,`image`),limit:100,offset:h});if(r.length===0)break;m.total+=r.length;for(let i of r)try{if(!t(i.mimeType)){m.skipped++;continue}let r=await a.download(i.fileKey),c;if(Buffer.isBuffer(r.body))c=r.body;else{let e=[],t=r.body.getReader();for(;;){let{done:n,value:r}=await t.read();if(n)break;r&&e.push(r)}c=Buffer.concat(e)}let l=await e(c,s),u=await a.getMetadata(i.fileKey),d=u?.metadata?.variants;d&&await Promise.all(Object.values(d).map(e=>a.delete(e).catch(()=>{})));let f={},p=u?.visibility??`public`,h=await Promise.all([...l.variants].map(async([e,t])=>{let r=n(i.fileKey,e,t.format);try{return await a.upload(t.buffer,{key:r,filename:`${e}_${i.filename}`,mimeType:t.mimeType,size:t.buffer.byteLength,visibility:p,metadata:{variantOf:i.fileKey,style:e}}),{styleName:e,vKey:r,ok:!0}}catch(t){return o?.warn({style:e,key:r,error:t},`Failed to upload regenerated variant (non-fatal)`),{styleName:e,vKey:r,ok:!1}}}));for(let e of h)e.ok&&(f[e.styleName]=e.vKey);Object.keys(f).length>0&&await a.updateMetadata(i.fileKey,{metadata:{...u?.metadata??{},variants:f}}).catch(e=>{o?.warn({key:i.fileKey,error:e},`Failed to update variant metadata (non-fatal)`)}),m.processed++,o?.debug({id:i.id,variants:Object.keys(f)},`Regenerated variants`)}catch(e){m.errors++,o?.error({id:i.id,fileKey:i.fileKey,error:e},`Failed to regenerate variants for media record`)}if(h+=100,r.length<100)break}return o?.info(m,`Variant regeneration complete`),m}export{r as regenerateAllVariants};
|
|
2
|
+
//# sourceMappingURL=regenerate-variants-rNGOuUJc.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"regenerate-variants-
|
|
1
|
+
{"version":3,"file":"regenerate-variants-rNGOuUJc.mjs","names":[],"sources":["../src/regenerate-variants.ts"],"sourcesContent":["/**\n * Batch variant regeneration — reprocesses all image media with updated styles.\n *\n * Downloads each original from storage, generates new variants via Sharp,\n * cleans up old variant files, and uploads new ones.\n *\n * Per-image errors are logged but don't stop the batch.\n */\n\nimport 'server-only'\n\nimport type { Logger } from '@murumets-ee/core'\nimport type { ContextResolver } from '@murumets-ee/entity'\nimport type { StorageClient } from '@murumets-ee/storage'\nimport type { PostgresJsDatabase } from 'drizzle-orm/postgres-js'\nimport { isProcessableImage, processImage } from './process-image.js'\nimport type { ImageStyle } from './types.js'\nimport { deriveVariantKey } from './variant-key.js'\n\nconst BATCH_SIZE = 100\n\nexport interface RegenerateOptions {\n db: PostgresJsDatabase\n storage: StorageClient\n logger?: Logger\n /** Current image styles to generate */\n styles: Record<string, ImageStyle>\n /** Security context resolver — passed through to AdminClient. */\n contextResolver?: ContextResolver\n}\n\nexport interface RegenerateResult {\n /** Total image media records found */\n total: number\n /** Successfully reprocessed */\n processed: number\n /** Skipped (non-processable mimeType, download failed, etc.) */\n skipped: number\n /** Failed with errors */\n errors: number\n}\n\n/**\n * Regenerate variants for all image media.\n * Processes in batches of 100 to avoid memory pressure.\n */\nexport async function regenerateAllVariants(options: RegenerateOptions): Promise<RegenerateResult> {\n const { db, storage, logger, styles } = options\n const { AdminClient } = await import('@murumets-ee/entity/admin')\n const { Media } = await import('./entity.js')\n const { schemaRegistry } = await import('@murumets-ee/db')\n const { eq } = await import('drizzle-orm')\n\n const admin = new AdminClient<typeof Media.allFields>({\n entity: Media,\n db,\n logger,\n contextResolver: options.contextResolver,\n })\n const table = schemaRegistry.get('media')\n if (!table) throw new Error('Media schema not registered')\n\n const result: RegenerateResult = { total: 0, processed: 0, skipped: 0, errors: 0 }\n let offset = 0\n\n logger?.info({ styles: Object.keys(styles) }, 'Starting variant regeneration')\n\n // Process in batches\n while (true) {\n const batch = await admin.findMany({\n where: eq(table.mediaType, 'image'),\n limit: BATCH_SIZE,\n offset,\n })\n\n if (batch.length === 0) break\n result.total += batch.length\n\n for (const record of batch) {\n try {\n // Skip non-processable images (SVG, GIF)\n if (!isProcessableImage(record.mimeType)) {\n result.skipped++\n continue\n }\n\n // Download original from storage\n const downloaded = await storage.download(record.fileKey)\n let buffer: Buffer\n if (Buffer.isBuffer(downloaded.body)) {\n buffer = downloaded.body\n } else {\n // ReadableStream → Buffer\n const chunks: Uint8Array[] = []\n const reader = downloaded.body.getReader()\n while (true) {\n const { done, value } = await reader.read()\n if (done) break\n if (value) chunks.push(value)\n }\n buffer = Buffer.concat(chunks)\n }\n\n // Generate new variants\n const processed = await processImage(buffer, styles)\n\n // Delete old variant files (best-effort, parallel)\n const oldFileRecord = await storage.getMetadata(record.fileKey)\n const oldVariants = (oldFileRecord?.metadata as Record<string, unknown> | null)?.variants as\n | Record<string, string>\n | undefined\n if (oldVariants) {\n await Promise.all(\n Object.values(oldVariants).map((vKey) => storage.delete(vKey).catch(() => {})),\n )\n }\n\n // Upload new variants (parallel)\n const newVariantKeys: Record<string, string> = {}\n const visibility = oldFileRecord?.visibility ?? 'public'\n const uploadResults = await Promise.all(\n [...processed.variants].map(async ([styleName, variant]) => {\n const vKey = deriveVariantKey(record.fileKey, styleName, variant.format)\n try {\n await storage.upload(variant.buffer, {\n key: vKey,\n filename: `${styleName}_${record.filename}`,\n mimeType: variant.mimeType,\n size: variant.buffer.byteLength,\n visibility,\n metadata: { variantOf: record.fileKey, style: styleName },\n })\n return { styleName, vKey, ok: true as const }\n } catch (uploadErr) {\n logger?.warn(\n { style: styleName, key: vKey, error: uploadErr },\n 'Failed to upload regenerated variant (non-fatal)',\n )\n return { styleName, vKey, ok: false as const }\n }\n }),\n )\n for (const r of uploadResults) {\n if (r.ok) newVariantKeys[r.styleName] = r.vKey\n }\n\n // Update original file's metadata with new variant keys\n if (Object.keys(newVariantKeys).length > 0) {\n await storage\n .updateMetadata(record.fileKey, {\n metadata: {\n ...(oldFileRecord?.metadata ?? {}),\n variants: newVariantKeys,\n },\n })\n .catch((metaErr: unknown) => {\n logger?.warn(\n { key: record.fileKey, error: metaErr },\n 'Failed to update variant metadata (non-fatal)',\n )\n })\n }\n\n result.processed++\n logger?.debug(\n { id: record.id, variants: Object.keys(newVariantKeys) },\n 'Regenerated variants',\n )\n } catch (err) {\n result.errors++\n logger?.error(\n { id: record.id, fileKey: record.fileKey, error: err },\n 'Failed to regenerate variants for media record',\n )\n }\n }\n\n offset += BATCH_SIZE\n if (batch.length < BATCH_SIZE) break\n }\n\n logger?.info(result, 'Variant regeneration complete')\n return result\n}\n"],"mappings":"iFA8CA,eAAsB,EAAsB,EAAuD,CACjG,GAAM,CAAE,KAAI,UAAS,SAAQ,UAAW,EAClC,CAAE,eAAgB,MAAM,OAAO,6BAC/B,CAAE,SAAU,MAAM,OAAO,yBAAA,KAAA,GAAA,EAAA,EAAA,CACzB,CAAE,kBAAmB,MAAM,OAAO,mBAClC,CAAE,MAAO,MAAM,OAAO,eAEtB,EAAQ,IAAI,EAAoC,CACpD,OAAQ,EACR,KACA,SACA,gBAAiB,EAAQ,gBAC1B,CAAC,CACI,EAAQ,EAAe,IAAI,QAAQ,CACzC,GAAI,CAAC,EAAO,MAAU,MAAM,8BAA8B,CAE1D,IAAM,EAA2B,CAAE,MAAO,EAAG,UAAW,EAAG,QAAS,EAAG,OAAQ,EAAG,CAC9E,EAAS,EAKb,IAHA,GAAQ,KAAK,CAAE,OAAQ,OAAO,KAAK,EAAO,CAAE,CAAE,gCAAgC,GAGjE,CACX,IAAM,EAAQ,MAAM,EAAM,SAAS,CACjC,MAAO,EAAG,EAAM,UAAW,QAAQ,CACnC,MAAO,IACP,SACD,CAAC,CAEF,GAAI,EAAM,SAAW,EAAG,MACxB,EAAO,OAAS,EAAM,OAEtB,IAAK,IAAM,KAAU,EACnB,GAAI,CAEF,GAAI,CAAC,EAAmB,EAAO,SAAS,CAAE,CACxC,EAAO,UACP,SAIF,IAAM,EAAa,MAAM,EAAQ,SAAS,EAAO,QAAQ,CACrD,EACJ,GAAI,OAAO,SAAS,EAAW,KAAK,CAClC,EAAS,EAAW,SACf,CAEL,IAAM,EAAuB,EAAE,CACzB,EAAS,EAAW,KA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O,QAAS,MAAO,EAAS,CACvC,gDACD,EACD,CAGN,EAAO,YACP,GAAQ,MACN,CAAE,GAAI,EAAO,GAAI,SAAU,OAAO,KAAK,EAAe,CAAE,CACxD,uBACD,OACM,EAAK,CACZ,EAAO,SACP,GAAQ,MACN,CAAE,GAAI,EAAO,GAAI,QAAS,EAAO,QAAS,MAAO,EAAK,CACtD,iDACD,CAKL,GADA,GAAU,IACN,EAAM,OAAS,IAAY,MAIjC,OADA,GAAQ,KAAK,EAAQ,gCAAgC,CAC9C"}
|
|
@@ -1,2 +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-B4QOt7Ve.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-
|
|
2
|
-
//# sourceMappingURL=resolve-image-styles-
|
|
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-B4QOt7Ve.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-DZaF1BHS.mjs`);return e().imageStyles}catch{}return e}export{t as resolveImageStyles};
|
|
2
|
+
//# sourceMappingURL=resolve-image-styles-2sa0O7wU.mjs.map
|
package/dist/{resolve-image-styles-DPelxnhW.mjs.map → resolve-image-styles-2sa0O7wU.mjs.map}
RENAMED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"resolve-image-styles-
|
|
1
|
+
{"version":3,"file":"resolve-image-styles-2sa0O7wU.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 lumi.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,CACrC,CAAC,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"}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
const e=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function t(t){return t!==void 0&&e.test(t)}let n=null;async function r(){return n||=(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})})(),n}async function i(){let{createAdminClient:e}=await import(`@murumets-ee/core/clients`),{MediaClient:t}=await import(`./client-
|
|
2
|
-
//# sourceMappingURL=routes-
|
|
1
|
+
const e=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;function t(t){return t!==void 0&&e.test(t)}let n=null;async function r(){return n||=(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})})(),n}async function i(){let{createAdminClient:e}=await import(`@murumets-ee/core/clients`),{MediaClient:t}=await import(`./client-CHGSsUxS.mjs`),{Media:n}=await import(`./entity-B75A4L3p.mjs`),i=await r();return new t({admin:e(n),storage:i})}function a(e,t=200){return new Response(JSON.stringify(e),{status:t,headers:{"Content-Type":`application/json`}})}function o(e,t){return a({error:e},t)}async function s(e,{segments:n}){let{isStorageConfigured:r,getStorageConfigReason:s}=await import(`@murumets-ee/storage`);if(n.length===2&&n[1]===`usage`){let e=n[0];if(!t(e))return o(`Invalid media ID format`,400);let{findMediaUsages:r}=await import(`./usage-aqV6Z9d_.mjs`),{getApp:i}=await import(`@murumets-ee/core`);return a({usages:await r(e,i().db.readWrite)})}let c=n.length>0?n[0]:void 0;if(c!==void 0&&!t(c))return o(`Invalid media ID format`,400);if(!r()){let e=s()??`Storage not configured`;return n.length>0?a({error:e,configured:!1,reason:e},503):a({items:[],total:0,configured:!1,reason:e})}let l=await i();if(c!==void 0){let e=await l.findById(c);if(!e)return o(`Media not found`,404);let t=await l.getUrl(c);return a({...e,url:t})}let u=new URL(e.url),d=u.searchParams.get(`search`)??void 0,f=u.searchParams.get(`mediaType`)??void 0,p=Math.min(Math.max(Number(u.searchParams.get(`limit`))||24,1),100),m=Math.max(Number(u.searchParams.get(`offset`))||0,0),h=await l.findMany({...d!==void 0&&{search:d},...f!==void 0&&{mediaType:f},limit:p,offset:m}),g=h.items.map(e=>e.id),[_,v]=await Promise.all([l.getUrls(g),l.getVariantUrls(g,`thumbnail`)]);return a({items:h.items.map(e=>{let t=v.get(e.id);return{id:e.id,title:e.title??null,alt:e.alt??null,filename:e.filename,mimeType:e.mimeType,size:e.size,mediaType:e.mediaType,url:_.get(e.id)??``,...t!==void 0&&{thumbnailUrl:t},width:e.width??null,height:e.height??null}}),total:h.total})}async function c(e,{segments:t,user:n,audit:r,checkPermission:s}){let{isStorageConfigured:c,getStorageConfigReason:l}=await import(`@murumets-ee/storage`);if(t.length===1&&t[0]===`regenerate-variants`){if(!s(`media`,`create`))return o(`Forbidden: media create permission required for variant regeneration`,403);if(!c())return o(l()??`Storage not configured`,503);let{regenerateAllVariants:e}=await import(`./regenerate-variants-CuHnSFS5.mjs`),{getApp:t,getContext:i}=await import(`@murumets-ee/core`),{resolveImageStyles:u}=await import(`./resolve-image-styles-2sa0O7wU.mjs`),{createStorageClient:d}=await import(`@murumets-ee/storage`),{getStorageConfig:f}=await import(`@murumets-ee/storage/plugin`),p=t(),m=await u(p,p.logger);if(!m||Object.keys(m).length===0)return o(`No image styles configured`,400);let h=d(f(),{app:p}),g=await e({db:p.db.readWrite,storage:h,logger:p.logger.child({media:!0}),styles:m,contextResolver:()=>{let e=i();if(!(!e?.user||!e?.checker))return{user:e.user,checker:e.checker,...e.scope!==void 0&&{scope:e.scope}}}});return r?.({action:`media.regenerate_variants`,userId:n.id,...n.name!==void 0&&{userName:n.name},metadata:{total:g.total,processed:g.processed,errors:g.errors}}),a(g)}if(!s(`media`,`create`))return o(`Forbidden: media create permission required for upload`,403);if(!c())return o(l()??`Storage not configured`,503);let u=await i(),d=(await e.formData()).get(`file`);if(!d||d.size===0)return o(`No file provided`,400);if(d.size>50*1024*1024)return o(`File too large: ${(d.size/1024/1024).toFixed(1)} MB exceeds 50 MB limit`,400);let f=Buffer.from(await d.arrayBuffer()),{detectMimeType:p}=await import(`@murumets-ee/storage`),{mimeType:m,mismatch:h}=await p(f,d.type||`application/octet-stream`);if(h)return o(`File content doesn't match declared type: claimed ${d.type}, detected ${m}`,400);let g=await u.upload(f,{filename:d.name,mimeType:m,size:d.size,uploadedBy:n.id}),_={id:g.media.id,title:g.media.title??null,alt:g.media.alt??null,filename:g.media.filename,mimeType:g.media.mimeType,size:g.media.size,mediaType:g.media.mediaType,url:g.url,width:g.media.width??null,height:g.media.height??null};return r?.({action:`media.upload`,entityType:`media`,entityId:g.media.id,userId:n.id,...n.name!==void 0&&{userName:n.name},changes:{filename:g.media.filename,mimeType:g.media.mimeType,size:g.media.size,mediaType:g.media.mediaType}}),a(_,201)}async function l(e,{segments:n,user:r,audit:s,checkPermission:c}){if(!c(`media`,`delete`))return o(`Forbidden: media delete permission required`,403);if(n.length===0)return o(`Media ID required`,400);let l=n[0];if(!t(l))return o(`Invalid media ID format`,400);let{isStorageConfigured:u,getStorageConfigReason:d}=await import(`@murumets-ee/storage`);return u()?(await(await i()).delete(l),s?.({action:`media.delete`,entityType:`media`,entityId:l,userId:r.id,...r.name!==void 0&&{userName:r.name}}),a({deleted:1})):o(d()??`Storage not configured`,503)}function u(){return{prefix:`media`,resource:`media`,actions:[`view`,`create`,`update`,`delete`],handlers:{GET:s,POST:c,DELETE:l}}}export{u as t};
|
|
2
|
+
//# sourceMappingURL=routes-BFRoQhZB.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"routes-DL5PZ3nJ.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'\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 | undefined): value is string {\n return value !== undefined && 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 { isStorageConfigured, getStorageConfigReason } = await import('@murumets-ee/storage')\n\n // GET /media/:id/usage — DB-only lookup, also safe without storage.\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 — validate UUID up front so a bad request still 400s\n // when storage happens to be unconfigured (matches the DELETE handler).\n const singleId = segments.length > 0 ? segments[0] : undefined\n if (singleId !== undefined && !isValidUuid(singleId)) {\n return errorJson('Invalid media ID format', 400)\n }\n\n // Everything below needs a working storage client. If env isn't wired,\n // return a structured \"disabled\" response rather than a 500 — the\n // media list page renders a banner and new admins can finish onboarding\n // without being blocked on a crash loop.\n if (!isStorageConfigured()) {\n const reason = getStorageConfigReason() ?? 'Storage not configured'\n if (segments.length > 0) {\n // Single item / other sub-paths — treat as not-found-ish to avoid\n // leaking existence; include reason so the UI can surface it.\n return json(\n { error: reason, configured: false, reason },\n 503,\n )\n }\n // GET /media — empty list + disabled flag for the picker's banner.\n const response: MediaPickerListResult & { configured: false; reason: string } = {\n items: [],\n total: 0,\n configured: false,\n reason,\n }\n return json(response)\n }\n\n const client = await getClient()\n\n // GET /media/:id — single item with URL (full record for EntityForm + picker)\n if (singleId !== undefined) {\n const record = await client.findById(singleId)\n if (!record) return errorJson('Media not found', 404)\n\n const url = await client.getUrl(singleId)\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 !== undefined && { search }),\n ...(mediaType !== undefined && {\n mediaType: mediaType as 'image' | 'video' | 'audio' | 'document' | 'other',\n }),\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 const thumbnailUrl = thumbMap.get(item.id)\n return {\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 !== undefined && { thumbnailUrl }),\n width: item.width ?? null,\n height: item.height ?? null,\n }\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 const { isStorageConfigured, getStorageConfigReason } = await import('@murumets-ee/storage')\n\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 if (!isStorageConfigured()) {\n return errorJson(\n getStorageConfigReason() ?? 'Storage not configured',\n 503,\n )\n }\n\n const { regenerateAllVariants } = await import('../regenerate-variants.js')\n const { getApp, getContext } = 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 contextResolver: () => {\n const ctx = getContext()\n if (!ctx?.user || !ctx?.checker) return undefined\n return {\n user: ctx.user,\n checker: ctx.checker,\n ...(ctx.scope !== undefined && { scope: ctx.scope }),\n }\n },\n })\n\n audit?.({\n action: 'media.regenerate_variants',\n userId: user.id,\n ...(user.name !== undefined && { 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 — upload file (requires media create permission)\n if (!checkPermission('media', 'create')) {\n return errorJson('Forbidden: media create permission required for upload', 403)\n }\n if (!isStorageConfigured()) {\n return errorJson(\n getStorageConfigReason() ?? 'Storage not configured',\n 503,\n )\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 ...(user.name !== undefined && { 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 const { isStorageConfigured, getStorageConfigReason } = await import('@murumets-ee/storage')\n if (!isStorageConfigured()) {\n // Can't safely delete — MediaClient needs storage to remove the\n // actual object, and partial delete (DB row gone, object orphaned)\n // would leak storage. Surface the reason so the UI can display it.\n return errorJson(getStorageConfigReason() ?? 'Storage not configured', 503)\n }\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 ...(user.name !== undefined && { 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/: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/regenerate-variants` — Regenerate all image variants (admin only)\n * - `DELETE /api/admin/media/:id` — Delete media + storage file (blocked if in use)\n *\n * Image style settings live under the generic settings API:\n * - `GET /api/admin/settings/media.imageStyles` — Read current styles\n * - `PATCH /api/admin/settings/media.imageStyles` — Update styles (Zod-validated)\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":"AA+BA,MAAM,EAAU,kEAEhB,SAAS,EAAY,EAA4C,CAC/D,OAAO,IAAU,IAAA,IAAa,EAAQ,KAAK,EAAM,CAenD,IAAI,EAAiH,KAErH,eAAe,GAAa,CAU1B,MATA,CACE,KAAkB,SAAY,CAC5B,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,EAGT,eAAe,GAAkC,CAC/C,GAAM,CAAE,qBAAsB,MAAM,OAAO,6BACrC,CAAE,eAAgB,MAAM,OAAO,yBAC/B,CAAE,SAAU,MAAM,OAAO,yBACzB,EAAU,MAAM,GAAY,CAElC,OAAO,IAAI,EAAY,CAAE,MADX,EAAkB,EACF,CAAE,UAAS,CAAC,CAO5C,SAAS,EAAK,EAAe,EAAS,IAAK,CACzC,OAAO,IAAI,SAAS,KAAK,UAAU,EAAK,CAAE,CACxC,SACA,QAAS,CAAE,eAAgB,mBAAoB,CAChD,CAAC,CAGJ,SAAS,EAAU,EAAiB,EAAgB,CAClD,OAAO,EAAK,CAAE,MAAO,EAAS,CAAE,EAAO,CAOzC,eAAe,EACb,EACA,CAAE,YACiB,CACnB,GAAM,CAAE,sBAAqB,0BAA2B,MAAM,OAAO,wBAGrE,GAAI,EAAS,SAAW,GAAK,EAAS,KAAO,QAAS,CACpD,IAAM,EAAK,EAAS,GACpB,GAAI,CAAC,EAAY,EAAG,CAAE,OAAO,EAAU,0BAA2B,IAAI,CAEtE,GAAM,C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CACxC,MAAO,CACL,OAAQ,QACR,SAAU,QACV,QAAS,CAAC,OAAQ,SAAU,SAAU,SAAS,CAC/C,SAAU,CACR,IAAK,EACL,KAAM,EACN,OAAQ,EACT,CACF"}
|
|
1
|
+
{"version":3,"file":"routes-BFRoQhZB.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'\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 | undefined): value is string {\n return value !== undefined && 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 { isStorageConfigured, getStorageConfigReason } = await import('@murumets-ee/storage')\n\n // GET /media/:id/usage — DB-only lookup, also safe without storage.\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 — validate UUID up front so a bad request still 400s\n // when storage happens to be unconfigured (matches the DELETE handler).\n const singleId = segments.length > 0 ? segments[0] : undefined\n if (singleId !== undefined && !isValidUuid(singleId)) {\n return errorJson('Invalid media ID format', 400)\n }\n\n // Everything below needs a working storage client. If env isn't wired,\n // return a structured \"disabled\" response rather than a 500 — the\n // media list page renders a banner and new admins can finish onboarding\n // without being blocked on a crash loop.\n if (!isStorageConfigured()) {\n const reason = getStorageConfigReason() ?? 'Storage not configured'\n if (segments.length > 0) {\n // Single item / other sub-paths — treat as not-found-ish to avoid\n // leaking existence; include reason so the UI can surface it.\n return json(\n { error: reason, configured: false, reason },\n 503,\n )\n }\n // GET /media — empty list + disabled flag for the picker's banner.\n const response: MediaPickerListResult & { configured: false; reason: string } = {\n items: [],\n total: 0,\n configured: false,\n reason,\n }\n return json(response)\n }\n\n const client = await getClient()\n\n // GET /media/:id — single item with URL (full record for EntityForm + picker)\n if (singleId !== undefined) {\n const record = await client.findById(singleId)\n if (!record) return errorJson('Media not found', 404)\n\n const url = await client.getUrl(singleId)\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 !== undefined && { search }),\n ...(mediaType !== undefined && {\n mediaType: mediaType as 'image' | 'video' | 'audio' | 'document' | 'other',\n }),\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 const thumbnailUrl = thumbMap.get(item.id)\n return {\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 !== undefined && { thumbnailUrl }),\n width: item.width ?? null,\n height: item.height ?? null,\n }\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 const { isStorageConfigured, getStorageConfigReason } = await import('@murumets-ee/storage')\n\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 if (!isStorageConfigured()) {\n return errorJson(\n getStorageConfigReason() ?? 'Storage not configured',\n 503,\n )\n }\n\n const { regenerateAllVariants } = await import('../regenerate-variants.js')\n const { getApp, getContext } = 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 contextResolver: () => {\n const ctx = getContext()\n if (!ctx?.user || !ctx?.checker) return undefined\n return {\n user: ctx.user,\n checker: ctx.checker,\n ...(ctx.scope !== undefined && { scope: ctx.scope }),\n }\n },\n })\n\n audit?.({\n action: 'media.regenerate_variants',\n userId: user.id,\n ...(user.name !== undefined && { 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 — upload file (requires media create permission)\n if (!checkPermission('media', 'create')) {\n return errorJson('Forbidden: media create permission required for upload', 403)\n }\n if (!isStorageConfigured()) {\n return errorJson(\n getStorageConfigReason() ?? 'Storage not configured',\n 503,\n )\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 ...(user.name !== undefined && { 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 const { isStorageConfigured, getStorageConfigReason } = await import('@murumets-ee/storage')\n if (!isStorageConfigured()) {\n // Can't safely delete — MediaClient needs storage to remove the\n // actual object, and partial delete (DB row gone, object orphaned)\n // would leak storage. Surface the reason so the UI can display it.\n return errorJson(getStorageConfigReason() ?? 'Storage not configured', 503)\n }\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 ...(user.name !== undefined && { 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/: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/regenerate-variants` — Regenerate all image variants (admin only)\n * - `DELETE /api/admin/media/:id` — Delete media + storage file (blocked if in use)\n *\n * Image style settings live under the generic settings API:\n * - `GET /api/admin/settings/media.imageStyles` — Read current styles\n * - `PATCH /api/admin/settings/media.imageStyles` — Update styles (Zod-validated)\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":"AA+BA,MAAM,EAAU,kEAEhB,SAAS,EAAY,EAA4C,CAC/D,OAAO,IAAU,IAAA,IAAa,EAAQ,KAAK,EAAM,CAenD,IAAI,EAAiH,KAErH,eAAe,GAAa,C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Q,eACR,WAAY,QACZ,SAAU,EAAO,MAAM,GACvB,OAAQ,EAAK,GACb,GAAI,EAAK,OAAS,IAAA,IAAa,CAAE,SAAU,EAAK,KAAM,CACtD,QAAS,CACP,SAAU,EAAO,MAAM,SACvB,SAAU,EAAO,MAAM,SACvB,KAAM,EAAO,MAAM,KACnB,UAAW,EAAO,MAAM,UACzB,CACF,CAAC,CAEK,EAAK,EAAM,IAAI,CAGxB,eAAe,EACb,EACA,CACE,WACA,OACA,QACA,mBAOiB,CACnB,GAAI,CAAC,EAAgB,QAAS,SAAS,CACrC,OAAO,EAAU,8CAA+C,IAAI,CAGtE,GAAI,EAAS,SAAW,EACtB,OAAO,EAAU,oBAAqB,IAAI,CAG5C,IAAM,EAAK,EAAS,GACpB,GAAI,CAAC,EAAY,EAAG,CAAE,OAAO,EAAU,0BAA2B,IAAI,CAEtE,GAAM,CAAE,sBAAqB,0BAA2B,MAAM,OAAO,wBAsBrE,OArBK,GAAqB,EAW1B,MAAM,MADe,GAAW,EACnB,OAAO,EAAG,CAEvB,IAAQ,CACN,OAAQ,eACR,WAAY,QACZ,SAAU,EACV,OAAQ,EAAK,GACb,GAAI,EAAK,OAAS,IAAA,IAAa,CAAE,SAAU,EAAK,KAAM,CACvD,CAAC,CAEK,EAAK,CAAE,QAAS,EAAG,CAAC,EAjBlB,EAAU,GAAwB,EAAI,yBAA0B,IAAI,CA6C/E,SAAgB,GAA0B,CACxC,MAAO,CACL,OAAQ,QACR,SAAU,QACV,QAAS,CAAC,OAAQ,SAAU,SAAU,SAAS,CAC/C,SAAU,CACR,IAAK,EACL,KAAM,EACN,OAAQ,EACT,CACF"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types-BMW3aeEB.d.mts","names":[],"sources":["../src/entity.ts","../src/types.ts"],"mappings":";;;;;;;;;;AAmBA;;;;;;;;;;;cAAa,KAAA,yBAAK,MAAA;
|
|
1
|
+
{"version":3,"file":"types-BMW3aeEB.d.mts","names":[],"sources":["../src/entity.ts","../src/types.ts"],"mappings":";;;;;;;;;;AAmBA;;;;;;;;;;;cAAa,KAAA,yBAAK,MAAA;MAqDhB,sBAAA,CAAA,OAAA;AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;KCnEU,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"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@murumets-ee/media",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.16.0",
|
|
4
4
|
"license": "Elastic-2.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"exports": {
|
|
@@ -57,13 +57,13 @@
|
|
|
57
57
|
"sharp": "^0.34.5",
|
|
58
58
|
"tailwind-merge": "^2.6.0",
|
|
59
59
|
"zod": "^3.24.1",
|
|
60
|
-
"@murumets-ee/core": "0.
|
|
61
|
-
"@murumets-ee/db": "0.
|
|
62
|
-
"@murumets-ee/entity": "0.
|
|
63
|
-
"@murumets-ee/logging": "0.
|
|
64
|
-
"@murumets-ee/settings": "0.
|
|
65
|
-
"@murumets-ee/storage": "0.
|
|
66
|
-
"@murumets-ee/ui": "0.
|
|
60
|
+
"@murumets-ee/core": "0.16.0",
|
|
61
|
+
"@murumets-ee/db": "0.16.0",
|
|
62
|
+
"@murumets-ee/entity": "0.16.0",
|
|
63
|
+
"@murumets-ee/logging": "0.16.0",
|
|
64
|
+
"@murumets-ee/settings": "0.16.0",
|
|
65
|
+
"@murumets-ee/storage": "0.16.0",
|
|
66
|
+
"@murumets-ee/ui": "0.16.0"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"lucide-react": ">=0.400.0",
|
package/dist/entity-B7zgx4yx.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{behavior as e,defineEntity as t,field as n}from"@murumets-ee/entity";const r=t({name:`media`,fields:{title:n.text({translatable:!0}),alt:n.text({translatable:!0}),description:n.text({translatable:!0}),fileKey:n.text({required:!0,indexed:!0}),filename:n.text({required:!0}),mimeType:n.text({required:!0,indexed:!0}),size:n.number({required:!0,integer:!0}),width:n.number({integer:!0}),height:n.number({integer:!0}),mediaType:n.select({options:[`image`,`video`,`audio`,`document`,`other`],required:!0,indexed:!0})},behaviors:[e.auditable()],scope:`global`,access:{view:`public`,create:`group.editor`,update:`group.editor`,delete:`group.admin`}});export{r as Media};
|
|
2
|
-
//# sourceMappingURL=entity-B7zgx4yx.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"entity-B7zgx4yx.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * Pre-defined Media entity.\n *\n * Auto-registered by the media() plugin — users do NOT add this to their entities array.\n * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineConfig({\n * plugins: [storage(), media()],\n * // Media entity is auto-added — no need to list it here\n * })\n * ```\n */\n\nimport { behavior, defineEntity, field } from '@murumets-ee/entity'\n\nexport const Media = defineEntity({\n name: 'media',\n fields: {\n // --- Display metadata (editable, translatable) ---\n title: field.text({ translatable: true }),\n alt: field.text({ translatable: true }),\n description: field.text({ translatable: true }),\n\n // --- File linkage (set on upload, immutable in practice) ---\n /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */\n fileKey: field.text({ required: true, indexed: true }),\n\n // --- File metadata (set on upload) ---\n filename: field.text({ required: true }),\n mimeType: field.text({ required: true, indexed: true }),\n size: field.number({ required: true, integer: true }),\n\n // --- Image-specific metadata (client-measured on upload) ---\n width: field.number({ integer: true }),\n height: field.number({ integer: true }),\n\n // --- Classification (derived from mimeType on upload) ---\n mediaType: field.select({\n options: ['image', 'video', 'audio', 'document', 'other'] as const,\n required: true,\n indexed: true,\n }),\n },\n behaviors: [behavior.auditable()],\n scope: 'global',\n access: {\n view: 'public',\n create: 'group.editor',\n update: 'group.editor',\n delete: 'group.admin',\n },\n})\n"],"mappings":"4EAmBA,MAAa,EAAQ,EAAa,CAChC,KAAM,QACN,OAAQ,CAEN,MAAO,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACzC,IAAK,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACvC,YAAa,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CAI/C,QAAS,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGtD,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,CAAC,CACxC,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CACvD,KAAM,EAAM,OAAO,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGrD,MAAO,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CACtC,OAAQ,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CAGvC,UAAW,EAAM,OAAO,CACtB,QAAS,CAAC,QAAS,QAAS,QAAS,WAAY,QAAQ,CACzD,SAAU,GACV,QAAS,GACV,CAAC,CACH,CACD,UAAW,CAAC,EAAS,WAAW,CAAC,CACjC,MAAO,SACP,OAAQ,CACN,KAAM,SACN,OAAQ,eACR,OAAQ,eACR,OAAQ,cACT,CACF,CAAC"}
|
package/dist/entity-TVTU7wS3.mjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
import{behavior as e,defineEntity as t,field as n}from"@murumets-ee/entity";var r=Object.defineProperty,i=((e,t)=>{let n={};for(var i in e)r(n,i,{get:e[i],enumerable:!0});return t||r(n,Symbol.toStringTag,{value:`Module`}),n})({Media:()=>a});const a=t({name:`media`,fields:{title:n.text({translatable:!0}),alt:n.text({translatable:!0}),description:n.text({translatable:!0}),fileKey:n.text({required:!0,indexed:!0}),filename:n.text({required:!0}),mimeType:n.text({required:!0,indexed:!0}),size:n.number({required:!0,integer:!0}),width:n.number({integer:!0}),height:n.number({integer:!0}),mediaType:n.select({options:[`image`,`video`,`audio`,`document`,`other`],required:!0,indexed:!0})},behaviors:[e.auditable()],scope:`global`,access:{view:`public`,create:`group.editor`,update:`group.editor`,delete:`group.admin`}});export{i as n,a as t};
|
|
2
|
-
//# sourceMappingURL=entity-TVTU7wS3.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"entity-TVTU7wS3.mjs","names":[],"sources":["../src/entity.ts"],"sourcesContent":["/**\n * Pre-defined Media entity.\n *\n * Auto-registered by the media() plugin — users do NOT add this to their entities array.\n * Connected to toolkit_files via the fileKey field (stores the StorageClient file key).\n *\n * @example\n * ```typescript\n * import { media } from '@murumets-ee/media/plugin'\n *\n * export default defineConfig({\n * plugins: [storage(), media()],\n * // Media entity is auto-added — no need to list it here\n * })\n * ```\n */\n\nimport { behavior, defineEntity, field } from '@murumets-ee/entity'\n\nexport const Media = defineEntity({\n name: 'media',\n fields: {\n // --- Display metadata (editable, translatable) ---\n title: field.text({ translatable: true }),\n alt: field.text({ translatable: true }),\n description: field.text({ translatable: true }),\n\n // --- File linkage (set on upload, immutable in practice) ---\n /** Key into toolkit_files table (e.g., 'uploads/2026/02/uuid/photo.jpg') */\n fileKey: field.text({ required: true, indexed: true }),\n\n // --- File metadata (set on upload) ---\n filename: field.text({ required: true }),\n mimeType: field.text({ required: true, indexed: true }),\n size: field.number({ required: true, integer: true }),\n\n // --- Image-specific metadata (client-measured on upload) ---\n width: field.number({ integer: true }),\n height: field.number({ integer: true }),\n\n // --- Classification (derived from mimeType on upload) ---\n mediaType: field.select({\n options: ['image', 'video', 'audio', 'document', 'other'] as const,\n required: true,\n indexed: true,\n }),\n },\n behaviors: [behavior.auditable()],\n scope: 'global',\n access: {\n view: 'public',\n create: 'group.editor',\n update: 'group.editor',\n delete: 'group.admin',\n },\n})\n"],"mappings":"iPAmBA,MAAa,EAAQ,EAAa,CAChC,KAAM,QACN,OAAQ,CAEN,MAAO,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACzC,IAAK,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CACvC,YAAa,EAAM,KAAK,CAAE,aAAc,GAAM,CAAC,CAI/C,QAAS,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGtD,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,CAAC,CACxC,SAAU,EAAM,KAAK,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CACvD,KAAM,EAAM,OAAO,CAAE,SAAU,GAAM,QAAS,GAAM,CAAC,CAGrD,MAAO,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CACtC,OAAQ,EAAM,OAAO,CAAE,QAAS,GAAM,CAAC,CAGvC,UAAW,EAAM,OAAO,CACtB,QAAS,CAAC,QAAS,QAAS,QAAS,WAAY,QAAQ,CACzD,SAAU,GACV,QAAS,GACV,CAAC,CACH,CACD,UAAW,CAAC,EAAS,WAAW,CAAC,CACjC,MAAO,SACP,OAAQ,CACN,KAAM,SACN,OAAQ,eACR,OAAQ,eACR,OAAQ,cACT,CACF,CAAC"}
|