UNPKG

@adonis-agora/media

Version:

Media-library for AdonisJS — owner collections (MIME whitelist, single-file replace, ordering), image conversions (eager/lazy), pluggable content transformers (HLS video via mediabunny, metadata probe, image blur-up placeholder), and column attachments, a

72 lines • 3.21 kB
import { UploadSessionStoreNotConfiguredError } from '../errors.js'; import { markLucidStore } from '../stack.js'; /** * The resumable session-store factory namespace used in `config/media.ts` (mirrors {@link stores}): * * ```ts * import { defineConfig, uploadSessions } from '@adonis-agora/media' * * export default defineConfig({ * uploads: { * resumable: { * store: 'lucid', * stores: { * memory: uploadSessions.memory(), * lucid: uploadSessions.lucid({ connection: 'pg' }), * }, * routes: { enabled: true }, * }, * }, * }) * ``` */ export const uploadSessions = { /** In-memory session store — single-process, non-durable. Handy for tests and scratch apps. */ memory() { return async () => { const { InMemoryUploadSessionStore } = await import('../testing/in_memory_upload_session_store.js'); return new InMemoryUploadSessionStore(); }; }, /** Persist resumable sessions in SQL via `@adonisjs/lucid`. */ lucid(config = {}) { const factory = async (ctx) => { // Resolve by the STRING alias rather than importing `@adonisjs/lucid/services/db`, which // resolves the `Database` CLASS and makes it the container token. See the equivalent comment // in `../stores/factory.ts` for why a class token breaks when two copies of `@adonisjs/lucid` // coexist in the host app's tree. const db = await ctx.app.container.make('lucid.db'); const { LucidUploadSessionStore } = await import('./lucid.js'); return new LucidUploadSessionStore(db, { ...(config.connection !== undefined ? { connectionName: config.connection } : {}), ...(config.table !== undefined ? { table: config.table } : {}), ...(config.partsTable !== undefined ? { partsTable: config.partsTable } : {}), ...(config.schema !== undefined ? { schema: config.schema } : {}), ...(ctx.tenancy === true ? { tenantColumn: true } : {}), }); }; // Readable without building the store, so `agora:doctor` knows a schema is configured. markLucidStore(factory, config.connection); return config.schema === undefined ? factory : Object.assign(factory, { schema: config.schema }); }, }; /** * Build the resumable session store selected by `config.store` from the `config.stores` map. A named * store with no matching factory THROWS {@link UploadSessionStoreNotConfiguredError} (a non-durable * fallback would silently lose resumable state); only the zero-config path (no `store` named) * resolves to the in-memory store. */ export async function resolveUploadSessionStore(config, ctx) { const name = config.store; if (name) { const factory = config.stores?.[name]; if (!factory) throw new UploadSessionStoreNotConfiguredError(name); return factory(ctx); } const { InMemoryUploadSessionStore } = await import('../testing/in_memory_upload_session_store.js'); return new InMemoryUploadSessionStore(); } //# sourceMappingURL=factory.js.map