{"version":3,"file":"local-runtime.mjs","names":[],"sources":["../../src/media/local-runtime.ts"],"sourcesContent":["/**\n * Local Media Provider Runtime\n *\n * This is the runtime implementation loaded by the entrypoint.\n * It wraps the existing MediaRepository and storage adapter.\n *\n * Note: This provider is special because it needs access to the database\n * and storage adapter. The createMediaProvider function receives these\n * via the config object, injected by the runtime.\n */\n\nimport type { Kysely } from \"kysely\";\n\nimport { MediaRepository } from \"../database/repositories/media.js\";\nimport type { Database } from \"../database/types.js\";\nimport type { Storage } from \"../index.js\";\nimport { invalidateSiteSettingsCache } from \"../settings/index.js\";\nimport type {\n\tCreateMediaProviderFn,\n\tMediaProvider,\n\tMediaListOptions,\n\tMediaProviderItem,\n\tMediaValue,\n\tEmbedResult,\n\tEmbedOptions,\n} from \"./types.js\";\n\nexport interface LocalMediaRuntimeConfig {\n\tenabled?: boolean;\n\t// These are injected by the runtime, not from user config\n\tdb?: Kysely<Database>;\n\t/**\n\t * Resolver for the live connection, preferred over `db`. The runtime\n\t * injects it so a connection-backed adapter (Postgres over Hyperdrive)\n\t * serves provider queries from the current request-scoped connection in ALS\n\t * rather than a snapshot of the per-isolate singleton. Omitted for stateless\n\t * adapters (D1, Node SQLite), where `db` is used directly.\n\t */\n\tgetDb?: () => Kysely<Database>;\n\tstorage?: Storage;\n}\n\n/**\n * Create the local media provider\n */\nexport const createMediaProvider: CreateMediaProviderFn<LocalMediaRuntimeConfig> = (config) => {\n\tconst { db, storage } = config;\n\n\tif (!db) {\n\t\tthrow new Error(\"Local media provider requires database connection\");\n\t}\n\n\t// Resolve the connection per operation (not captured once) so a\n\t// connection-backed adapter uses the current event-scoped connection; falls\n\t// back to the injected `db` for stateless adapters.\n\tconst resolveDb = config.getDb ?? (() => db);\n\tconst repo = () => new MediaRepository(resolveDb());\n\n\tconst provider: MediaProvider = {\n\t\tasync list(options: MediaListOptions) {\n\t\t\tconst result = await repo().findMany({\n\t\t\t\tcursor: options.cursor,\n\t\t\t\tlimit: options.limit,\n\t\t\t\tmimeType: options.mimeType,\n\t\t\t\t// TODO: Add search support when capabilities.search is true\n\t\t\t});\n\n\t\t\treturn {\n\t\t\t\titems: result.items.map((item) => ({\n\t\t\t\t\tid: item.id,\n\t\t\t\t\tfilename: item.filename,\n\t\t\t\t\tmimeType: item.mimeType,\n\t\t\t\t\tsize: item.size ?? undefined,\n\t\t\t\t\twidth: item.width ?? undefined,\n\t\t\t\t\theight: item.height ?? undefined,\n\t\t\t\t\tblurhash: item.blurhash ?? undefined,\n\t\t\t\t\tdominantColor: item.dominantColor ?? undefined,\n\t\t\t\t\talt: item.alt ?? undefined,\n\t\t\t\t\tpreviewUrl: `/_emdash/api/media/file/${item.storageKey}`,\n\t\t\t\t\tmeta: {\n\t\t\t\t\t\tstorageKey: item.storageKey,\n\t\t\t\t\t\tcaption: item.caption,\n\t\t\t\t\t\tblurhash: item.blurhash,\n\t\t\t\t\t\tdominantColor: item.dominantColor,\n\t\t\t\t\t},\n\t\t\t\t})),\n\t\t\t\tnextCursor: result.nextCursor,\n\t\t\t};\n\t\t},\n\n\t\tasync get(id: string) {\n\t\t\tconst item = await repo().findById(id);\n\t\t\tif (!item) return null;\n\n\t\t\treturn {\n\t\t\t\tid: item.id,\n\t\t\t\tfilename: item.filename,\n\t\t\t\tmimeType: item.mimeType,\n\t\t\t\tsize: item.size ?? undefined,\n\t\t\t\twidth: item.width ?? undefined,\n\t\t\t\theight: item.height ?? undefined,\n\t\t\t\tblurhash: item.blurhash ?? undefined,\n\t\t\t\tdominantColor: item.dominantColor ?? undefined,\n\t\t\t\talt: item.alt ?? undefined,\n\t\t\t\tpreviewUrl: `/_emdash/api/media/file/${item.storageKey}`,\n\t\t\t\tmeta: {\n\t\t\t\t\tstorageKey: item.storageKey,\n\t\t\t\t\tcaption: item.caption,\n\t\t\t\t\tblurhash: item.blurhash,\n\t\t\t\t\tdominantColor: item.dominantColor,\n\t\t\t\t},\n\t\t\t};\n\t\t},\n\n\t\tasync upload(_input) {\n\t\t\tif (!storage) {\n\t\t\t\tthrow new Error(\"Storage not configured for local media provider\");\n\t\t\t}\n\n\t\t\t// This is handled by the existing media upload endpoint\n\t\t\t// The provider interface is used by external providers\n\t\t\t// For local, we delegate to the existing system\n\t\t\tthrow new Error(\"Local upload should use /_emdash/api/media endpoint\");\n\t\t},\n\n\t\tasync delete(id: string) {\n\t\t\tconst repoInstance = repo();\n\t\t\tconst item = await repoInstance.findById(id);\n\t\t\tif (!item) return;\n\n\t\t\t// Delete from storage if available\n\t\t\tif (storage) {\n\t\t\t\ttry {\n\t\t\t\t\tawait storage.delete(item.storageKey);\n\t\t\t\t} catch {\n\t\t\t\t\t// Ignore storage deletion errors\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tawait repoInstance.delete(id);\n\n\t\t\t// If this row was referenced by `logo`, `favicon`, or\n\t\t\t// `seo.defaultOgImage`, the worker-scoped settings cache now\n\t\t\t// holds a stale URL. The provider routes (and any future caller)\n\t\t\t// bypass `handleMediaDelete`, so we invalidate here too.\n\t\t\tinvalidateSiteSettingsCache();\n\t\t},\n\n\t\tgetEmbed(value: MediaValue, _options?: EmbedOptions): EmbedResult {\n\t\t\tconst storageKey =\n\t\t\t\ttypeof value.meta?.storageKey === \"string\" ? value.meta.storageKey : value.id;\n\t\t\tconst src = `/_emdash/api/media/file/${storageKey}`;\n\t\t\tconst mimeType = value.mimeType || \"\";\n\n\t\t\t// Prefer the first-class fields; fall back to `meta` for legacy snapshots\n\t\t\t// stored before LQIP was promoted off the provider-specific `meta` bag.\n\t\t\tconst blurhash =\n\t\t\t\tvalue.blurhash ??\n\t\t\t\t(typeof value.meta?.blurhash === \"string\" ? value.meta.blurhash : undefined);\n\t\t\tconst dominantColor =\n\t\t\t\tvalue.dominantColor ??\n\t\t\t\t(typeof value.meta?.dominantColor === \"string\" ? value.meta.dominantColor : undefined);\n\n\t\t\t// Determine embed type based on MIME type\n\t\t\tif (mimeType.startsWith(\"image/\")) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"image\",\n\t\t\t\t\tsrc,\n\t\t\t\t\twidth: value.width,\n\t\t\t\t\theight: value.height,\n\t\t\t\t\tblurhash,\n\t\t\t\t\tdominantColor,\n\t\t\t\t\talt: value.alt,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (mimeType.startsWith(\"video/\")) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"video\",\n\t\t\t\t\tsrc,\n\t\t\t\t\twidth: value.width,\n\t\t\t\t\theight: value.height,\n\t\t\t\t\tcontrols: true,\n\t\t\t\t\tpreload: \"metadata\",\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tif (mimeType.startsWith(\"audio/\")) {\n\t\t\t\treturn {\n\t\t\t\t\ttype: \"audio\",\n\t\t\t\t\tsrc,\n\t\t\t\t\tcontrols: true,\n\t\t\t\t\tpreload: \"metadata\",\n\t\t\t\t};\n\t\t\t}\n\n\t\t\t// Fallback: treat as image (for unknown types)\n\t\t\treturn {\n\t\t\t\ttype: \"image\",\n\t\t\t\tsrc,\n\t\t\t\twidth: value.width,\n\t\t\t\theight: value.height,\n\t\t\t\tblurhash,\n\t\t\t\tdominantColor,\n\t\t\t\talt: value.alt,\n\t\t\t};\n\t\t},\n\n\t\tgetThumbnailUrl(id: string, _mimeType?: string) {\n\t\t\t// For local media, return the file URL\n\t\t\treturn `/_emdash/api/media/file/${id}`;\n\t\t},\n\t};\n\n\treturn provider;\n};\n\n/**\n * Helper to convert a MediaRepository item to MediaProviderItem\n */\nexport function repoItemToProviderItem(item: {\n\tid: string;\n\tfilename: string;\n\tmimeType: string;\n\tsize: number | null;\n\twidth: number | null;\n\theight: number | null;\n\talt: string | null;\n\tcaption: string | null;\n\tstorageKey: string;\n\tblurhash: string | null;\n\tdominantColor: string | null;\n}): MediaProviderItem {\n\treturn {\n\t\tid: item.id,\n\t\tfilename: item.filename,\n\t\tmimeType: item.mimeType,\n\t\tsize: item.size ?? undefined,\n\t\twidth: item.width ?? undefined,\n\t\theight: item.height ?? undefined,\n\t\tblurhash: item.blurhash ?? undefined,\n\t\tdominantColor: item.dominantColor ?? undefined,\n\t\talt: item.alt ?? undefined,\n\t\tpreviewUrl: `/_emdash/api/media/file/${item.storageKey}`,\n\t\tmeta: {\n\t\t\tstorageKey: item.storageKey,\n\t\t\tcaption: item.caption,\n\t\t\tblurhash: item.blurhash,\n\t\t\tdominantColor: item.dominantColor,\n\t\t},\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;AA6CA,MAAa,uBAAuE,WAAW;CAC9F,MAAM,EAAE,IAAI,YAAY;AAExB,KAAI,CAAC,GACJ,OAAM,IAAI,MAAM,oDAAoD;CAMrE,MAAM,YAAY,OAAO,gBAAgB;CACzC,MAAM,aAAa,IAAI,gBAAgB,WAAW,CAAC;AA8JnD,QA5JgC;EAC/B,MAAM,KAAK,SAA2B;GACrC,MAAM,SAAS,MAAM,MAAM,CAAC,SAAS;IACpC,QAAQ,QAAQ;IAChB,OAAO,QAAQ;IACf,UAAU,QAAQ;IAElB,CAAC;AAEF,UAAO;IACN,OAAO,OAAO,MAAM,KAAK,UAAU;KAClC,IAAI,KAAK;KACT,UAAU,KAAK;KACf,UAAU,KAAK;KACf,MAAM,KAAK,QAAQ;KACnB,OAAO,KAAK,SAAS;KACrB,QAAQ,KAAK,UAAU;KACvB,UAAU,KAAK,YAAY;KAC3B,eAAe,KAAK,iBAAiB;KACrC,KAAK,KAAK,OAAO;KACjB,YAAY,2BAA2B,KAAK;KAC5C,MAAM;MACL,YAAY,KAAK;MACjB,SAAS,KAAK;MACd,UAAU,KAAK;MACf,eAAe,KAAK;MACpB;KACD,EAAE;IACH,YAAY,OAAO;IACnB;;EAGF,MAAM,IAAI,IAAY;GACrB,MAAM,OAAO,MAAM,MAAM,CAAC,SAAS,GAAG;AACtC,OAAI,CAAC,KAAM,QAAO;AAElB,UAAO;IACN,IAAI,KAAK;IACT,UAAU,KAAK;IACf,UAAU,KAAK;IACf,MAAM,KAAK,QAAQ;IACnB,OAAO,KAAK,SAAS;IACrB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