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

67 lines • 3.24 kB
import { MediaManager } from './media_manager.js'; import { getBootedApp } from './services/booted_app.js'; import { placeholderOf } from './transformers/placeholder.js'; /** * Core of {@link storeSingleFile} driven by an explicit {@link MediaManager}, so tests can exercise * it with an in-memory manager without booting an app. Attaches the file to the owner's collection * (single-file replacement comes from the collection config) and resolves the original URL plus, * best-effort, a `thumb` conversion URL. */ export async function storeSingleFileWith(manager, input) { const record = await manager.library.for(input.ownerType, input.ownerId).attach({ collection: input.collection, fileName: input.fileName, mimeType: input.mimeType, contents: input.contents, }); const url = await manager.library.url(record.id); // The `thumb` conversion is optional: if the collection doesn't define it (or it can't be // generated), there's simply no thumbnail — return null rather than failing the whole store. let thumbUrl = null; try { thumbUrl = await manager.library.url(record.id, 'thumb'); } catch { thumbUrl = null; } return { url, thumbUrl, id: record.id, placeholder: placeholderOf(record) }; } /** * Core of {@link removeSingleFile} driven by an explicit {@link MediaManager}. Empties the owner's * collection by deleting every record it holds (a single-file collection holds at most one). */ export async function removeSingleFileWith(manager, input) { const records = await manager.library.for(input.ownerType, input.ownerId).list(input.collection); for (const record of records) await manager.library.delete(record.id); } /** * Store a single file for an owner via the app-bound {@link MediaManager}. Resolves the manager from * the container, then delegates to {@link storeSingleFileWith}. The `collection` must be configured * `single: true` in `config/media.ts` for prior files to be replaced. * * The container is read from the provider-captured booted app ({@link getBootedApp}) rather than * `@adonisjs/core/services/app` — see `./services/booted_app.js` for why that import is unreliable * under a duplicated `@adonisjs/core` tree (pnpm dual-package hazard). */ export async function storeSingleFile(input) { const manager = await getBootedApp().container.make(MediaManager); return storeSingleFileWith(manager, input); } /** * Remove the owner's file in a collection via the app-bound {@link MediaManager}. Resolves the * manager from the container, then delegates to {@link removeSingleFileWith}. */ export async function removeSingleFile(input) { const manager = await getBootedApp().container.make(MediaManager); return removeSingleFileWith(manager, input); } /** * Whether the single-file store is usable — i.e. the {@link MediaManager} is bound in the app * container. Callers use this to feature-detect media before routing a store through it. */ export async function isSingleFileStoreAvailable() { return getBootedApp().container.hasBinding(MediaManager); } export { placeholderOf } from './transformers/placeholder.js'; //# sourceMappingURL=single_file_store.js.map