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

988 lines • 54.8 kB
import { createHmac, randomBytes, randomUUID, timingSafeEqual } from 'node:crypto';
import { ACCOUNT_EXTENSIONS_BINDING, } from '../src/account_console/contract.js';
import { accountCollections, accountPath, mediaAccountExtension, } from '../src/account_console/extension.js';
import { handleAccountUpload } from '../src/account_console/upload_endpoint.js';
import { UPLOAD_SCRIPT } from '../src/account_console/upload_script.js';
import { resolveMediaTenancy } from '../src/agora/tenancy.js';
import { buildConsoleService, readConsoleServiceConfig } from '../src/dashboard/console_service.js';
import { assertMediaTopology, } from '../src/define_config.js';
import { DirectUploadHandler } from '../src/direct_upload_handler.js';
import { createDriveBackedResolver } from '../src/disks/drive.js';
import { resolveConfiguredDisks } from '../src/disks/factory.js';
import { registerDoctorChecks } from '../src/doctor/agora_doctor.js';
import { dbSchemaDoctorCheck, hasConfiguredSchema, mediaDoctorChecks, } from '../src/doctor/checks.js';
import { mediaConsoleCommandHandler } from '../src/federation/console.js';
import { CONSOLE_KEY_PURPOSE, commandKeyJwk, consoleKeyReport, consoleSiblingsOf, resolveAppConsoleAuth, } from '../src/federation/console_auth.js';
import { appKeyOf, ConsoleCommandRefused, RedisConsoleChannel, resolveRedisConnection, } from '../src/federation/redis_channel.js';
import { sanitizeFileName } from '../src/file_name.js';
import { isMimeType } from '../src/http_safety.js';
import { MediaManager } from '../src/media_manager.js';
import { setBootedApp } from '../src/services/booted_app.js';
import { describeMediaConfig } from '../src/stack.js';
import { resolveStore } from '../src/stores/factory.js';
import { resolveTenantVerifier } from '../src/topology/auth.js';
import { resolveMediaTenant } from '../src/topology/authkit_tenant.js';
import { MediaControlPlane } from '../src/topology/control_plane.js';
import { partitionStore } from '../src/topology/partition_store.js';
import { buildTenantManager, standaloneAccountsOf } from '../src/topology/tenant_manager.js';
import { TusUploadHandler } from '../src/tus.js';
import { resolveUploadSessionStore } from '../src/upload_sessions/factory.js';
/**
 * Wires `@adonis-agora/media` into the AdonisJS application: binds a singleton {@link MediaManager}
 * built from `config/media.ts`. Storage is delegated to `@adonisjs/drive` — the manager resolves disks
 * from the booted Drive manager, so this package never reimplements disk drivers. The selected store
 * (`memory` / `lucid`) and image processor (`processors.sharp()`) are built lazily from the config so
 * their peer dependencies (`@adonisjs/lucid`, `sharp`) load only when chosen.
 *
 * ```ts
 * const media = await app.container.make(MediaManager)
 * await media.library.attach({ ownerType: 'Post', ownerId: '1', collection: 'gallery', ... })
 * ```
 */
export default class MediaProvider {
    app;
    /** The channel this app answers other apps' federated consoles on (`consoleCommands`). */
    #consoleChannel;
    /** See {@link MediaProvider.#resolvedConfig}. */
    #resolvedConfigPromise;
    constructor(app) {
        this.app = app;
    }
    register() {
        // Hand the booted app to the internal singleton so it never has to `import` an
        // `@adonisjs/core` copy that may not be the one `bin/server` booted (see
        // `../src/services/booted_app.js`) — mirrors the fix already applied to the `@adonisjs/lucid`
        // `Database` token (`'lucid.db'` string alias) for the same class of dual-package hazard.
        setBootedApp(this.app);
        this.app.container.singleton(MediaManager, async () => {
            // A tenant's url/token/partition: what config/media.ts sets, else authkit-client's.
            const { config } = await this.#resolvedConfig();
            assertMediaTopology(config);
            // A tenant: records, files and conversions live on the control plane.
            if (config.topology === 'tenant' && config.tenant)
                return buildTenantManager(config);
            // Validate + build the store from config first, so a misconfigured `store` fails fast (before
            // we touch Drive) rather than silently booting on a non-durable in-memory fallback.
            // Organizations as tenants: off unless `config/context.ts` (or `tenancy` here) turns it on.
            const tenancy = resolveMediaTenancy(config.tenancy);
            const ctx = { app: this.app, ...(tenancy.enabled ? { tenancy: true } : {}) };
            const store = await resolveStore(config, ctx);
            const imageProcessor = await this.#resolveImageProcessor(config);
            // Build the resumable (TUS) session store only when `uploads.resumable` is configured, so its
            // peer (`@adonisjs/lucid`) loads lazily and the resumable subsystem stays fully opt-in.
            const resumable = config.uploads?.resumable;
            const uploadSessions = resumable
                ? await resolveUploadSessionStore({
                    ...(resumable.store !== undefined ? { store: resumable.store } : {}),
                    ...(resumable.stores !== undefined ? { stores: resumable.stores } : {}),
                }, { app: this.app, ...(tenancy.enabled ? { tenancy: true } : {}) })
                : undefined;
            // Same lazy, opt-in construction for the session-backed direct upload store. It may share
            // the resumable store's Lucid tables, but each subsystem owns its store INSTANCE so enabling
            // one never implies the other.
            const direct = config.uploads?.direct;
            const directUploadSessions = direct
                ? await resolveUploadSessionStore({
                    ...(direct.store !== undefined ? { store: direct.store } : {}),
                    ...(direct.stores !== undefined ? { stores: direct.stores } : {}),
                }, { app: this.app, ...(tenancy.enabled ? { tenancy: true } : {}) })
                : undefined;
            // Build any disks declared in `config.disks` (e.g. the bundled `disks.s3()` driver). Each
            // factory lazily loads its peer (the AWS SDK), so nothing is imported unless an S3 disk is
            // actually configured.
            const configuredDisks = await resolveConfiguredDisks(config.disks);
            // Resolve disks from Drive without a hard dependency: import its service lazily. We keep the
            // module NAMESPACE, never a value read out of it — Drive assigns its manager inside
            // `app.booted(...)`, which resolves immediately while the app is still booting, so the export
            // is `undefined` at this point and only the live binding sees the later assignment. The
            // resolver reads it at the first real disk use and memoizes it (see `createDriveBackedResolver`).
            //
            // An app without the Drive provider (no `drive.manager` binding) never imports it: Drive's
            // service module resolves the binding as it loads, which would fail every media call — even
            // the store reads the console makes. Such an app can still use `config.disks`.
            const driveAvailable = this.app.container.hasBinding('drive.manager');
            const driveService = driveAvailable
                ? (await import('@adonisjs/drive/services/main'))
                : { default: undefined };
            const defaultDisk = config.disk ?? (await this.#resolveDefaultDiskName());
            // A disk named in `config.disks` wins over a Drive disk of the same name; otherwise fall
            // through to Drive so existing Drive disks keep working unchanged.
            const resolve = createDriveBackedResolver({
                driveService,
                configuredDisks,
                defaultDisk,
                driveAvailable,
            });
            // A standalone app that reads people's own files (an avatar) on a control plane.
            const accounts = standaloneAccountsOf(config);
            return new MediaManager({
                defaultDisk,
                resolve,
                store,
                ...(config.topology === 'control-plane' ? { topology: 'control-plane' } : {}),
                ...(accounts !== undefined ? { accounts } : {}),
                ...(imageProcessor !== undefined ? { imageProcessor } : {}),
                ...(config.collections !== undefined ? { collections: config.collections } : {}),
                ...(config.attachmentKeyPrefix !== undefined
                    ? { attachmentKeyPrefix: config.attachmentKeyPrefix }
                    : {}),
                ...(config.emitDiagnostics !== undefined
                    ? { emitDiagnostics: config.emitDiagnostics }
                    : {}),
                ...(config.uploads?.mode !== undefined ? { uploadMode: config.uploads.mode } : {}),
                ...(config.uploads?.partSize !== undefined
                    ? { uploadPartSize: config.uploads.partSize }
                    : {}),
                ...(config.uploads?.presignTtlSeconds !== undefined
                    ? { uploadPresignTtlSeconds: config.uploads.presignTtlSeconds }
                    : {}),
                ...(uploadSessions !== undefined ? { uploadSessions } : {}),
                ...(resumable?.tmpPrefix !== undefined ? { resumableTmpPrefix: resumable.tmpPrefix } : {}),
                ...(resumable?.sessionTtlSeconds !== undefined
                    ? { resumableSessionTtlSeconds: resumable.sessionTtlSeconds }
                    : {}),
                ...(directUploadSessions !== undefined ? { directUploadSessions } : {}),
                // Direct settings fall back to the shared `uploads.*` knobs, so one part size / TTL can
                // govern both the raw primitives and the session flow.
                ...((direct?.partSize ?? config.uploads?.partSize)
                    ? { directPartSize: (direct?.partSize ?? config.uploads?.partSize) }
                    : {}),
                ...((direct?.presignTtlSeconds ?? config.uploads?.presignTtlSeconds)
                    ? {
                        directPresignTtlSeconds: (direct?.presignTtlSeconds ??
                            config.uploads?.presignTtlSeconds),
                    }
                    : {}),
                ...(direct?.sessionTtlSeconds !== undefined
                    ? { directSessionTtlSeconds: direct.sessionTtlSeconds }
                    : {}),
                // Delivery is config only — no route is mounted for it, deliberately: serving a record is
                // an authorization decision, so the app owns the route (see `MediaDeliveryHandler`).
                ...(config.delivery?.mode !== undefined ? { deliveryMode: config.delivery.mode } : {}),
                ...(config.delivery?.signedTtlSeconds !== undefined
                    ? { deliverySignedTtlSeconds: config.delivery.signedTtlSeconds }
                    : {}),
                ...(config.delivery?.images !== undefined
                    ? { deliveryImages: config.delivery.images }
                    : {}),
                ...(config.delivery?.signing !== undefined
                    ? { deliverySigning: config.delivery.signing }
                    : {}),
                ...(config.delivery?.path !== undefined ? { deliveryPath: config.delivery.path } : {}),
                ...(config.delivery?.streamPath !== undefined
                    ? { deliveryStreamPath: config.delivery.streamPath }
                    : {}),
                ...(config.access?.identity !== undefined
                    ? { accessIdentity: config.access.identity }
                    : {}),
                ...(tenancy.enabled ? { tenancy } : {}),
            });
        });
    }
    /**
     * Mount the media routes once the app has booted (see the comment inside for why). No-ops when
     * neither the TUS resumable routes nor the direct-upload routes are enabled in config.
     */
    /**
     * `config/media.ts`, with a tenant's missing `url`/`token`/`partition` filled in from
     * `@adonis-agora/authkit-client` (`tenantOf`, detected by shape). Resolved once; throws, naming
     * both fixes, when a field resolves from neither.
     */
    #resolvedConfig() {
        this.#resolvedConfigPromise ??= resolveMediaTenant(this.app.config.get('media', {}), this.app.container);
        return this.#resolvedConfigPromise;
    }
    async boot() {
        this.#resolvedAppKey = await appKeyOf(this.app);
        // A tenant whose url/token/partition resolve from neither config/media.ts nor authkit-client
        // fails the boot of a web process here, naming both fixes. An ace command never fails for it.
        if (this.app.config.get('media', {}).topology === 'tenant' &&
            this.app.getEnvironment?.() === 'web') {
            await this.#resolvedConfig();
        }
        // A federated console this app cannot authenticate fails the boot here, in web processes (where
        // the channel starts), saying what to add. Never in `ready()`: the HTTP server is listening then.
        const consoleCommands = this.app.config.get('media', {}).consoleCommands;
        if (consoleCommands && this.app.getEnvironment?.() === 'web')
            await this.#consoleAuth();
        // Route registration can't happen synchronously in `boot()`: at this point in the AdonisJS
        // lifecycle the HTTP server/router binding may not be resolvable yet (bindings from other
        // providers' `boot()` methods can still be pending), and — critically — the *documented*
        // `@adonisjs/core/services/router` singleton is only assigned once the app's "booted" hooks run
        // (`await app.booted(async () => { router = ... })` inside that service module itself), which
        // fire strictly AFTER every provider's own `boot()`. A provider that imports that singleton and
        // calls `router.get(...)` directly inside `boot()` crashes every entrypoint (serve/ace/tests)
        // that registers it, because `router` is still `undefined` at that point — the same hazard this
        // provider now avoids for `MediaManager` itself (see `../src/services/booted_app.js`).
        //
        // Deferring to `app.booted(...)` runs our route registration as another "booted" hook — the
        // same mechanism the router service uses to become available in the first place — which is
        // guaranteed to fire BEFORE the HTTP server's own `boot()` commits the router (the last point at
        // which routes can still be added; see `Server#boot()` in `@adonisjs/http-server`, which runs
        // inside `app.start()`, strictly after all "booted" hooks). Resolving `router` fresh from the
        // container here (rather than depending on that service singleton) is also the same pattern
        // `@adonisjs/core`'s own `AppServiceProvider` and `@adonis-agora/durable`'s `DashboardProvider`
        // use internally, and is immune to the dual-package hazard for the same reason `booted_app.ts`
        // is: it comes from `this.app` (captured at registration), never from a module-level import.
        // `node ace agora:doctor` reads these after boot (registered from `boot()`, which Adonis awaits).
        await registerDoctorChecks(this.app.container, mediaDoctorChecks).catch(() => { });
        // `media.db-schema` only when a Lucid store sets `schema`: without one, no new doctor line.
        if (hasConfiguredSchema(this.app.config.get('media', undefined))) {
            await registerDoctorChecks(this.app.container, [dbSchemaDoctorCheck]).catch(() => { });
        }
        // What media runs on, for telescope's Architecture page (read from the config after boot).
        await this.#describeStack();
        // Media's "My files" page in the authkit account console, when authkit's registry is in the
        // app and `accountConsole.collections` lists something. No import of authkit: a binding name and
        // an object literal.
        await this.#registerAccountPages();
        await this.app.booted(async () => {
            const router = await this.app.container.make('router');
            this.#registerRoutes(router);
        });
    }
    /**
     * Answer another app's federated media console (`consoleCommands` in config/media.ts). Web
     * processes only: a short-lived `ace` command that won a command's claim could exit before it
     * answers. A connection that cannot claim a command (no `SET NX`) answers nothing, and says so.
     */
    async ready() {
        const config = this.app.config.get('media', {});
        const options = config.consoleCommands;
        if (!options || this.app.getEnvironment() !== 'web')
            return;
        const log = async (level, line) => {
            try {
                const logger = await this.app.container.make('logger');
                logger[level](line);
            }
            catch {
                /* logging must never break boot */
            }
        };
        // Resolved (and checked) in `boot()`; resolved here when only `ready()` runs.
        const { auth, siblings } = await this.#consoleAuth();
        // The token mode's key shows as a short id in the mode; `node ace media:console-key` prints it.
        await log('info', `[@adonis-agora/media] console commands: ${auth.modes.join(' + ')}; ${options.actions === true
            ? 'actions on'
            : 'actions off (set consoleCommands.actions to let the console act)'}`);
        // Automatic crypto mode: the key must exist in this app's partition. Created where crypto may;
        // otherwise ONE line says what to run (never a boot failure: commands fail until then).
        // Token mode as this app's only way in: publish the command public key in authkit's key
        // directory, so consoles fetch it (no `publicKey` to copy) and know to use a token (§13a).
        // With crypto active, nothing is published and nothing said: crypto is enough (§13e), unless
        // crypto commands must carry a token too (explicit crypto + verifyTenant, migrating).
        const authkit = siblings.authkit;
        if (auth.keys && (!auth.crypto || auth.channel.cryptoRequiresToken === true)) {
            if (typeof authkit?.publishAppKey === 'function') {
                void authkit
                    .publishAppKey({ purpose: CONSOLE_KEY_PURPOSE, jwk: commandKeyJwk(auth.keys) })
                    .catch(() => undefined);
            }
            else if (options.secret === undefined) {
                // Token mode is this app's only way to be reached, and no console can find its key.
                await log('warn', "[@adonis-agora/media] console commands: token mode is this app's only way to be reached, but authkit-client's `service` is not configured, so its key is not in authkit's key directory: configure it, or give each console controlPlane.publicKey (`node ace media:console-key` prints it)");
            }
        }
        if (auth.checkKey) {
            // A crypto tenant: one line per outcome change; the next command's decrypt is the retry.
            await auth.checkKey();
        }
        else if (auth.crypto?.auto && siblings.crypto) {
            const report = await consoleKeyReport(siblings.crypto);
            if (report)
                await log(report.level, report.line);
        }
        try {
            const channel = new RedisConsoleChannel({
                client: await resolveRedisConnection(this.app.container, options.connection),
                prefix: options.prefix,
                ...auth.channel,
            });
            const settings = readConsoleServiceConfig(this.app.config);
            const handler = mediaConsoleCommandHandler({
                partition: auth.partition,
                actions: options.actions === true,
                service: async () => buildConsoleService(await this.app.container.make(MediaManager), {
                    ...settings,
                    console: { ...settings.console, actions: options.actions === true },
                }),
                log: (line) => void log('info', line),
            });
            // A refused or garbage message is logged once per reason and dropped: never a crash.
            const said = new Set();
            await channel.answer(handler, {
                onError: (error) => {
                    const line = error instanceof ConsoleCommandRefused
                        ? `[@adonis-agora/media] console commands: refused a command (${error.reason})`
                        : `[@adonis-agora/media] console commands: ${error instanceof Error ? error.message : String(error)}`;
                    if (said.has(line))
                        return;
                    if (said.size < 100)
                        said.add(line);
                    void log('warn', line);
                },
            });
            this.#consoleChannel = channel;
        }
        catch (error) {
            await log('error', `[@adonis-agora/media] consoleCommands is on, but ${error instanceof Error ? error.message : String(error)}`);
        }
    }
    /** How this app answers the federated console, resolved once (it throws when nothing resolves). */
    #consoleAuth() {
        this.#consoleAuthOnce ??= (async () => {
            const config = this.app.config.get('media', {});
            const siblings = await consoleSiblingsOf(this.app.container);
            const auth = resolveAppConsoleAuth({
                options: config.consoleCommands,
                controlPlaneVerifyTenant: config.topology === 'control-plane' ? config.verifyTenant : undefined,
                appKey: await appKeyOf(this.app),
                siblings,
                logKey: (note) => void this.#log(note.level === 'info' ? 'info' : 'warn', note.line),
            });
            return { auth, siblings };
        })();
        return this.#consoleAuthOnce;
    }
    #consoleAuthOnce;
    async shutdown() {
        await this.#consoleChannel?.close();
        this.#consoleChannel = undefined;
    }
    /** The `accountConsole` options when the page is configured (at least one collection). */
    #accountConsole() {
        const options = this.app.config.get('media', {}).accountConsole;
        if (!options || accountCollections(options).length === 0)
            return undefined;
        return options;
    }
    /** The app's `APP_KEY` (`app.appKey`, a string or a Secret), or `undefined` without one. */
    #appKey() {
        return this.#resolvedAppKey;
    }
    /** The app's APP_KEY, read in `boot()` (§13c: `appKeyOf` resolves config providers, async). */
    #resolvedAppKey;
    async #registerAccountPages() {
        const options = this.#accountConsole();
        if (!options)
            return;
        // Outside the `try`: a production app with no secret must fail its boot, not just warn.
        const secret = this.#uploadSecret("accountConsole's upload tickets");
        const container = this.app.container;
        try {
            if (typeof container.hasBinding !== 'function')
                return;
            if (!container.hasBinding(ACCOUNT_EXTENSIONS_BINDING))
                return;
            const registry = (await container.make(ACCOUNT_EXTENSIONS_BINDING));
            if (typeof registry?.register !== 'function' || (registry.contract ?? 0) < 1)
                return;
            registry.register(mediaAccountExtension({
                media: () => this.app.container.make(MediaManager),
                options,
                secret,
            }));
        }
        catch (error) {
            try {
                const logger = await this.app.container.make('logger');
                logger.warn(`[@adonis-agora/media] could not add the account console page: ${error instanceof Error ? error.message : String(error)}`);
            }
            catch {
                /* logging must never break boot */
            }
        }
    }
    /**
     * The account console's upload endpoint and its script, under `accountConsole.path` (default
     * `/media/account`): `GET /upload.js` and `POST /uploads` (authorized by the page's signed
     * ticket). With `publicUrl` (pages shown by an IdP in another app) the endpoint answers CORS.
     */
    #mountAccountRoutes(router) {
        const options = this.#accountConsole();
        if (!options || !accountCollections(options).some((c) => c.upload !== false))
            return;
        const base = accountPath(options);
        const secret = this.#uploadSecret("accountConsole's upload tickets");
        const remote = options.publicUrl !== undefined;
        const cors = (ctx) => {
            if (!remote)
                return;
            ctx.response.header('access-control-allow-origin', '*');
            ctx.response.header('access-control-allow-methods', 'POST, OPTIONS');
            ctx.response.header('access-control-allow-headers', 'content-type, x-file-name, x-media-upload-ticket');
        };
        router
            .get(`${base}/upload.js`, async (ctx) => {
            if (remote)
                ctx.response.header('access-control-allow-origin', '*');
            return ctx.response
                .header('content-type', 'text/javascript; charset=utf-8')
                .header('cache-control', 'public, max-age=300')
                .send(UPLOAD_SCRIPT);
        })
            .as('media.account.upload_script');
        if (remote) {
            router
                .route(`${base}/uploads`, ['OPTIONS'], async (ctx) => {
                cors(ctx);
                return ctx.response.status(204).send('');
            })
                .as('media.account.uploads.preflight');
        }
        router
            .post(`${base}/uploads`, async (ctx) => {
            cors(ctx);
            const result = await handleAccountUpload({ header: (name) => ctx.request.header(name), body: ctx.request.request }, {
                media: () => this.app.container.make(MediaManager),
                options,
                secret,
            });
            return ctx.response.status(result.status).json(result.body);
        })
            .as('media.account.uploads');
    }
    /**
     * The tenant API of a media control plane (`topology: 'control-plane'`), under
     * `controlPlane.prefix` (default `/_media`): other apps keep their media here through it, each
     * request authorized by the app's token (`verifyTenant`).
     */
    #mountControlPlane(router, config) {
        if (config.topology !== 'control-plane' || !config.verifyTenant)
            return;
        const manager = () => this.app.container.make(MediaManager);
        const logger = {
            warn: (line) => void this.#log('warn', line),
            error: (line) => void this.#log('error', line),
        };
        const plane = new MediaControlPlane({
            library: async (partition) => (await manager()).controlPlaneLibrary(partition),
            store: async (partition) => partitionStore((await manager()).store, partition),
            verifyTenant: resolveTenantVerifier(config.verifyTenant),
            ...(config.controlPlane?.prefix !== undefined ? { prefix: config.controlPlane.prefix } : {}),
            ...(config.controlPlane?.collections !== undefined
                ? { collections: config.controlPlane.collections }
                : {}),
            ...(config.controlPlane?.maxBytes !== undefined
                ? { maxBytes: config.controlPlane.maxBytes }
                : {}),
            ...(config.controlPlane?.accounts !== undefined
                ? { accounts: config.controlPlane.accounts }
                : {}),
            ...(config.delivery?.mode !== undefined ? { deliveryMode: config.delivery.mode } : {}),
            logger,
        });
        plane.register({
            route: (pattern, methods, handler) => router.route(pattern, methods, (ctx) => handler(ctx)),
        });
    }
    async #log(level, line) {
        try {
            const logger = await this.app.container.make('logger');
            logger[level](line);
        }
        catch {
            /* logging must never break a request */
        }
    }
    /**
     * Mount the optional direct-S3 upload routes when `uploads.routes.enabled` is set. These are plain
     * AdonisJS routes (NOT controllers): each resolves the {@link MediaManager} singleton lazily and
     * delegates to its upload methods.
     *
     * The client never picks where bytes land: it sends a `fileName`, the key is derived here
     * (`routes.keyFor`, default `uploads/<uuid>/<fileName>`), and the disk is `routes.disk` (or one of
     * `routes.disks` it names). The later multipart calls carry the `uploadToken` the initiate
     * answered, an HMAC (APP_KEY) over the disk, key and upload id, so nobody can complete or abort
     * someone else's upload. The routes are guarded by `routes.middleware`; without it they do not
     * mount in production (a boot error says what to add) and warn elsewhere.
     *
     * Routes (relative to `uploads.routes.prefix`, default `/media/uploads`):
     * - `POST   /direct/initiate`                    → presign multipart parts
     * - `POST   /direct/:uploadId/parts/:partNumber` → presign one (retried) part
     * - `POST   /direct/:uploadId/complete`          → assemble the parts
     * - `DELETE /direct/:uploadId`                   → abort the upload
     * - `PUT    /proxy`                              → stream bytes through the app (proxy mode)
     */
    #registerRoutes(router) {
        const config = this.app.config.get('media', {});
        this.#mountControlPlane(router, config);
        this.#mountAccountRoutes(router);
        this.#mountTusRoutes(router, config);
        this.#mountDirectSessionRoutes(router, config);
        const routes = config.uploads?.routes;
        if (!routes?.enabled)
            return;
        const middleware = this.#guardUploadRoutes('uploads.routes', routes);
        const prefix = (routes.prefix ?? '/media/uploads').replace(/\/+$/, '');
        const manager = () => this.app.container.make(MediaManager);
        const uploadSecret = this.#uploadSecret("uploads.routes' upload tokens");
        const secret = () => uploadSecret;
        const keyFor = routes.keyFor ?? ((fileName, token) => `uploads/${token}/${fileName}`);
        /** The disk this request writes to: the route's, or one of `routes.disks` the client names. */
        const diskOf = (media, ctx) => {
            const fallback = routes.disk ?? media.storage.defaultDisk;
            const asked = ctx.request.input('disk');
            if (asked === undefined || asked === null || asked === '' || asked === fallback) {
                return fallback;
            }
            if (typeof asked === 'string' && (routes.disks ?? []).includes(asked))
                return asked;
            throw refusal(400, `disk "${String(asked)}" is not one these routes write to (uploads.routes.disk${routes.disks ? ' or uploads.routes.disks' : ''})`);
        };
        /** The server-side key for the file the client names; a client-sent `key` is refused. */
        const keyOf = (ctx) => {
            if (ctx.request.input('key') !== undefined) {
                throw refusal(400, 'the upload key is chosen by the server: send `fileName` instead of `key`');
            }
            const fileName = ctx.request.input('fileName');
            if (typeof fileName !== 'string' || fileName === '')
                throw refusal(400, 'fileName is required');
            return keyFor(sanitizeFileName(fileName), randomUUID(), ctx);
        };
        const contentTypeOf = (value) => {
            if (value === undefined || value === null || value === '')
                return undefined;
            const base = typeof value === 'string' ? value.split(';')[0].trim() : '';
            if (!isMimeType(base))
                throw refusal(400, 'contentType must be a type/subtype MIME type');
            return base;
        };
        /** The `uploadToken` of a later multipart call, checked against its disk, key and upload id. */
        const ticketed = (ctx) => {
            const key = ctx.request.input('key');
            const disk = ctx.request.input('disk');
            const token = ctx.request.input('uploadToken');
            const uploadId = String(ctx.params.uploadId);
            if (typeof key !== 'string' || typeof disk !== 'string' || typeof token !== 'string') {
                throw refusal(400, 'key, disk and uploadToken (as the initiate answered them) are required');
            }
            if (!sameToken(uploadTokenFor(secret(), disk, key, uploadId), token)) {
                throw refusal(403, 'this upload token is not for this upload');
            }
            return { key, disk, uploadId };
        };
        const json = (handler, okStatus = 200) => {
            return async (ctx) => {
                try {
                    const result = await handler(await manager(), ctx);
                    return ctx.response.status(okStatus).json(result ?? { ok: true });
                }
                catch (error) {
                    const message = error instanceof Error ? error.message : 'upload error';
                    const { code, status } = error;
                    const answer = typeof status === 'number'
                        ? status
                        : code === 'E_MEDIA_UPLOAD_NOT_SUPPORTED' || code === 'E_MEDIA_UNSAFE_FILE_NAME'
                            ? 400
                            : 500;
                    return ctx.response
                        .status(answer)
                        .json({ error: message, ...(code !== undefined ? { code } : {}) });
                }
            };
        };
        const group = router.group(() => {
            router
                .post(`${prefix}/direct/initiate`, json(async (media, ctx) => {
                const body = ctx.request.body();
                const disk = diskOf(media, ctx);
                const key = keyOf(ctx);
                const contentType = contentTypeOf(ctx.request.input('contentType'));
                const created = await media.initiateDirectUpload({
                    key,
                    disk,
                    ...(contentType !== undefined ? { contentType } : {}),
                    ...(typeof body.size === 'number' ? { size: body.size } : {}),
                    ...(typeof body.partSize === 'number' ? { partSize: body.partSize } : {}),
                });
                return {
                    ...created,
                    uploadToken: uploadTokenFor(secret(), created.disk, created.key, created.uploadId),
                };
            }))
                .as('media.uploads.direct.initiate');
            router
                .post(`${prefix}/direct/:uploadId/parts/:partNumber`, json(async (media, ctx) => {
                const { key, disk, uploadId } = ticketed(ctx);
                return media.uploads.presignPart({
                    key,
                    disk,
                    uploadId,
                    partNumber: Number(ctx.params.partNumber),
                });
            }))
                .as('media.uploads.direct.presignPart');
            router
                .post(`${prefix}/direct/:uploadId/complete`, json(async (media, ctx) => {
                const { key, disk, uploadId } = ticketed(ctx);
                const body = ctx.request.body();
                return media.completeDirectUpload({
                    key,
                    disk,
                    uploadId,
                    parts: Array.isArray(body.parts) ? body.parts : [],
                });
            }))
                .as('media.uploads.direct.complete');
            router
                .delete(`${prefix}/direct/:uploadId`, json(async (media, ctx) => {
                const { key, disk, uploadId } = ticketed(ctx);
                await media.abortDirectUpload({ key, disk, uploadId });
                return { ok: true };
            }))
                .as('media.uploads.direct.abort');
            router
                .put(`${prefix}/proxy`, json(async (media, ctx) => {
                const disk = diskOf(media, ctx);
                const key = keyOf(ctx);
                const contentType = contentTypeOf(ctx.request.header('content-type'));
                return media.proxyUpload({
                    key,
                    disk,
                    // The raw Node request is a Readable — stream it straight to the disk without buffering.
                    contents: ctx.request.request,
                    ...(contentType !== undefined ? { contentType } : {}),
                });
            }, 201))
                .as('media.uploads.proxy');
        });
        if (middleware.length > 0) {
            group.middleware(middleware);
        }
    }
    /**
     * Mount the optional TUS resumable-upload routes when `uploads.resumable.routes.enabled`. Like the
     * direct routes these are plain AdonisJS routes (NOT controllers): each resolves the singleton
     * {@link MediaManager} lazily and drives a framework-agnostic {@link TusUploadHandler} over
     * `media.resumable`. The handler is built once, lazily, from the resolved manager.
     *
     * Routes (relative to `uploads.resumable.routes.prefix`, default `/media/uploads/tus`):
     * - `OPTIONS /`      → protocol discovery (version, extensions, max size)
     * - `POST    /`      → create a session (`Upload-Length` + `Upload-Metadata`) → `201` + `Location`
     * - `HEAD    /:id`   → report `Upload-Offset` (resume point)
     * - `PATCH   /:id`   → append bytes at `Upload-Offset`; auto-completes at the declared length
     * - `DELETE  /:id`   → terminate the upload
     *
     * Set `uploads.resumable.routes.collection` to have the handler enforce that collection's
     * `acceptsMimeTypes` at create time and on the first chunk (`415`), instead of only at attach.
     */
    #mountTusRoutes(router, config) {
        const resumable = config.uploads?.resumable;
        if (!resumable?.routes?.enabled)
            return;
        const middleware = this.#guardUploadRoutes('uploads.resumable.routes', resumable.routes);
        const prefix = (resumable.routes.prefix ?? '/media/uploads/tus').replace(/\/+$/, '');
        const routeDisk = resumable.routes.disk;
        const maxSize = resumable.routes.maxSize;
        const collection = resumable.routes.collection;
        // Build the TUS handler once, on first request, from the resolved MediaManager singleton.
        let handler;
        const getHandler = async () => {
            if (!handler) {
                const media = await this.app.container.make(MediaManager);
                handler = new TusUploadHandler({
                    manager: media.resumable,
                    disk: routeDisk ?? media.storage.defaultDisk,
                    basePath: prefix,
                    // The collection's `acceptsMimeTypes` is read from the manager's registry, never restated
                    // here, so the TUS gate and the attach-time barrier can never disagree.
                    collections: media.collections,
                    ...(collection !== undefined ? { collection } : {}),
                    ...(maxSize !== undefined ? { maxSize } : {}),
                });
            }
            return handler;
        };
        const run = (method, withBody = false) => {
            return async (ctx) => {
                const tus = await getHandler();
                const headers = {};
                for (const [name, value] of Object.entries(ctx.request.headers())) {
                    headers[name.toLowerCase()] = Array.isArray(value) ? value[0] : value;
                }
                const req = { method, headers };
                if (ctx.params.id !== undefined)
                    req.uploadId = String(ctx.params.id);
                if (withBody)
                    req.body = await readRawBody(ctx);
                const res = await tus.handle(req);
                for (const [name, value] of Object.entries(res.headers)) {
                    ctx.response.header(name, value);
                }
                ctx.response.status(res.status);
                return res.body ?? '';
            };
        };
        const group = router.group(() => {
            router.route(prefix, ['OPTIONS'], run('OPTIONS')).as('media.uploads.tus.options');
            router.post(prefix, run('POST')).as('media.uploads.tus.create');
            router.route(`${prefix}/:id`, ['HEAD'], run('HEAD')).as('media.uploads.tus.head');
            router.patch(`${prefix}/:id`, run('PATCH', true)).as('media.uploads.tus.patch');
            router.delete(`${prefix}/:id`, run('DELETE')).as('media.uploads.tus.delete');
        });
        if (middleware.length > 0) {
            group.middleware(middleware);
        }
    }
    /**
     * The middleware of one upload route group. Without any (and without `allowUnguarded`), the
     * group does not mount in production (throws at boot, naming the option) and warns elsewhere.
     */
    #guardUploadRoutes(option, routes) {
        const middleware = routes.middleware ?? [];
        if (middleware.length > 0 || routes.allowUnguarded === true)
            return middleware;
        const what = `${option} has no \`middleware\`: anyone could write files to your storage through these routes`;
        const fix = `Set \`${option}.middleware: [middleware.auth()]\` in config/media.ts (or \`${option}.allowUnguarded: true\` when a global middleware guards them)`;
        if (this.app.inProduction === true) {
            throw new Error(`[@adonis-agora/media] ${what}, so they are not mounted in production. ${fix}.`);
        }
        void this.#log('warn', `[@adonis-agora/media] ${what} (they would not mount in production). ${fix}.`);
        return middleware;
    }
    /**
     * The one secret every upload credential this package issues is signed with (the multipart
     * `uploadToken`s of `uploads.routes`, the account console's upload tickets): `uploads.secret`,
     * else `APP_KEY`, so every instance accepts what another signed. Without either, production fails
     * at boot naming both; elsewhere a per-process key is used, with a warning (a credential then
     * fails on another process). `what` names the credential in those messages.
     */
    #uploadSecret(what) {
        const explicit = this.app.config.get('media', {}).uploads?.secret;
        const raw = typeof explicit === 'string' ? explicit : explicit?.release?.();
        if (typeof raw === 'string' && raw !== '') {
            if (raw.length < 16) {
                throw new Error('[@adonis-agora/media] uploads.secret must be at least 16 characters.');
            }
            return raw;
        }
        const appKey = this.#appKey();
        if (appKey)
            return appKey;
        const message = `${what} are signed with APP_KEY or uploads.secret, and this app has neither`;
        const fix = 'Set APP_KEY (`appKey` in config/app.ts), or `uploads.secret` in config/media.ts';
        if (this.app.inProduction === true) {
            throw new Error(`[@adonis-agora/media] ${message}. ${fix}.`);
        }
        void this.#log('warn', `[@adonis-agora/media] ${message}: using a per-process key (one issued by another process is refused). ${fix}.`);
        this.#devUploadSecret ??= randomBytes(32).toString('hex');
        return this.#devUploadSecret;
    }
    #devUploadSecret;
    /**
     * Mount the optional session-backed direct upload routes when `uploads.direct.routes.enabled`.
     * Plain AdonisJS routes (NOT controllers), each driving a framework-agnostic
     * {@link DirectUploadHandler} over `media.direct` — mirroring the TUS mount. The client sends a
     * `fileName`, never a key: the key is resolved server-side by the handler's `keyFor`.
     *
     * Routes (relative to `uploads.direct.routes.prefix`, default `/media/uploads/direct/sessions`):
     * - `POST   /`                      → initiate: persist a session, presign every part URL
     * - `GET    /:id`                   → status: confirmed ETags + fresh URLs for pending parts
     * - `POST   /:id/parts/:partNumber` → confirm one uploaded part's ETag
     * - `POST   /:id/complete`          → assemble the parts into the final object
     * - `DELETE /:id`                   → abort
     *
     * Guarded by `routes.middleware`: without it they do not mount in production (see
     * {@link #guardUploadRoutes}). Or leave them off and mount your own routes over the handler.
     */
    #mountDirectSessionRoutes(router, config) {
        const direct = config.uploads?.direct;
        if (!direct?.routes?.enabled)
            return;
        const prefix = (direct.routes.prefix ?? '/media/uploads/direct/sessions').replace(/\/+$/, '');
        const routeDisk = direct.routes.disk;
        const maxSize = direct.routes.maxSize;
        const collection = direct.routes.collection;
        const policyThunk = direct.routes.policy;
        const routeMiddleware = this.#guardUploadRoutes('uploads.direct.routes', direct.routes);
        // Build the handler once, on first request, from the resolved MediaManager singleton. The
        // policy (when configured) is lazy-imported here too, so its module loads only if the routes
        // actually serve a request. `adopt` wires the policy's `complete` path onto the library.
        let handler;
        const getHandler = async () => {
            if (!handler) {
                const media = await this.app.container.make(MediaManager);
                const policy = await this.#resolveDirectUploadPolicy(policyThunk);
                handler = new DirectUploadHandler({
                    manager: media.direct,
                    adopt: media.completeDirectUploadToLibrary.bind(media),
                    ...(routeDisk !== undefined ? { disk: routeDisk } : {}),
                    ...(collection !== undefined ? { collection } : {}),
                    ...(maxSize !== undefined ? { maxSize } : {}),
                    ...(policy !== undefined ? { policy } : {}),
                });
            }
            return handler;
        };
        const run = (toRequest) => {
            return async (ctx) => {
                const direct_ = await getHandler();
                const res = await direct_.handle(toRequest(ctx), ctx);
                ctx.response.status(res.status);
                return res.body ?? '';
            };
        };
        // The five session endpoints, grouped so `routes.middleware` (e.g. `middleware.auth()`) guards
        // them all at once. Paths are relative to the group `prefix`. Without middleware they mount only
        // outside production (or with `allowUnguarded`).
        router
            .group(() => {
            router
                .post('/', run((ctx) => {
                const body = ctx.request.body();
                return {
                    action: 'initiate',
                    fileName: body.fileName,
                    size: body.size,
                    ...(body.contentType !== undefined ? { contentType: body.contentType } : {}),
                    ...(body.metadata !== undefined ? { metadata: body.metadata } : {}),
                };
            }))
                .as('media.uploads.direct.sessions.initiate');
            router
                .get('/:id', run((ctx) => ({ action: 'status', uploadId: String(ctx.params.id) })))
                .as('media.uploads.direct.sessions.status');
            router
                .post('/:id/parts/:partNumber', run((ctx) => {
                const body = ctx.request.body();
                return {
                    action: 'confirm-part',
                    uploadId: String(ctx.params.id),
                    partNumber: Number(ctx.params.partNumber),
                    etag: body.etag,
                };
            }))
                .as('media.uploads.direct.sessions.confirmPart');
            router
                .post('/:id/complete', run((ctx) => {
                const body = ctx.request.body();
                return {
                    action: 'complete',
                    uploadId: String(ctx.params.id),
                    ...(body.parts !== undefined ? { parts: body.parts } : {}),
                };
            }))
                .as('media.uploads.direct.sessions.complete');
            router
                .delete('/:id', run((ctx) => ({ action: 'abort', uploadId: String(ctx.params.id) })))
                .as('media.uploads.direct.sessions.abort');
        })
            .prefix(prefix)
            .middleware(routeMiddleware);
    }
    /**
     * Resolve the configured direct-upload {@link DirectUploadPolicy} from its lazy thunk, reading the
     * module's default export. The default export may be a policy CLASS (instantiated here with no
     * arguments) or a ready policy OBJECT (used as-is). Returns `undefined` when no policy is
     * configured, leaving the handler on its built-in key/complete behavior.
     */
    async #resolveDirectUploadPolicy(thunk) {
        if (!thunk)
            return undefined;
        const policy = (await thunk()).default;
        return typeof policy === 'function' ? new policy() : policy;
    }
    /**
     * Tag `config/media.ts` with the disks, stores and Redis connection it selects (see
     * `../src/stack.js`), so telescope's Architecture page draws them without app code. Never throws.
     */
    async #describeStack() {
        try {
            const config = this.app.config.get('media', undefined);
            if (config === null || typeof config !== 'object')
                return;
            const database = this.app.config.get('database', undefined);
            const dbClients = {};
            for (const [name, conn] of Object.entries(database?.connections ?? {})) {
                if (typeof conn?.client === 'string')
                    dbClients[name] = conn.client;
            }
            const defaultRedisConnection = this.app.config.get('redis.connection', undefined);
            const defaultDisk = config.disk ?? (await this.#resolveDefaultDiskName().catch(() => undefined));
            describeMediaConfig(config, {
                ...(defaultDisk ? { defaultDisk } : {}),
                ...(database?.connection ? { defaultDbConnection: database.connection } : {}),
                dbClients,
                ...(defaultRedisConnection ? { defaultRedisConnection } : {}),
            });
        }
        catch {
            // Describing is best-effort: a config it cannot read describes nothing.
        }
    }
    /** A config `imageProcessor` may be a ready instance or a lazy factory thunk. */
    async #resolveImageProcessor(config) {
        const ip = config.imageProcessor;
        if (!ip)
            return undefined;
        if (typeof ip === 'function')
            return ip();
        return ip;
    }
    /**
     * When `config/media.ts` names no `disk`, fall back to Drive's configured default disk name.
     *
     * `app.config.get('drive')` returns the value `config/drive.ts` EXPORTED, and Drive's
     * `defineConfig()` returns an unresolved `ConfigProvider` — `{ type: 'provider', resolver }`. So the
     * default disk name is not a property of that value at all: reading `.default` off it always yields
     * `undefined` (this method used to, and therefore always returned the literal `'default'`), and so
     * does reading `.config.default` — `{ config: { default, fakes, services } }` is the shape the
     * resolver RETURNS, never the shape of the provider itself.
     *
     * The provider therefore has to be resolved, which is exactly what Drive's own provider does before
     * building its manager (`configProvider.resolve(app, app.config.get('drive'))` →
     * `new DriveManager(resolved.config)`). Asking the manager instead is not possible: flydrive's
     * `DriveManager` keeps that config in a `#private` field and exposes no accessor for the default
     * service name (`use()`/`fake()`/`restore()` only default TO it internally).
     *
     * Resolving here is cheap and side-effect free for our purposes: it builds throwaway driver
     * FACTORIES (no driver is constructed, no credentials read) and returns a fresh `locallyServed`
     * array that we discard, so Drive's own resolution — and the file-serving routes it mounts from it —
     * are untouched. `configProvider` is imported lazily, so a host that names its `disk` explicitly
     * never pays for it.
     *
     * A host may also assign a plain object to `drive` (or have no Drive config at all); both still
     * work, and `'default'` remains the last resort so such an app keeps booting instead of crashing.
     */
    async #resolveDefaultDiskName() {
        const driveConfig = this.app.config.get('drive');
        if (driveConfig === undefined || driveConfig === null)
            return 'default';
        const { configProvider } = await import('@adonisjs/core');
        // Returns `null` when the value is not a ConfigProvider, which is also how we detect the
        // plain-object case — one source of truth for "is this a config provider", the framework's own.
        const resolved = await configProvider.resolve(this.app, driveConfig);
        if (resolved)
            return resolved.config?.default ?? resolved.default ?? 'default';
        return driveConfig.default ?? 'default';
    }
}
/** An upload route's refusal: answered with `status` and `message`. */
function refusal(status, message) {
    return Object.assign(new Error(message), { status });
}
/** The `uploadToken` of one direct upload: an HMAC over where it writes and its upload id. */
function uploadTokenFor(secret, disk, key, uploadId) {
    return createHmac('sha256', secret)
        .update(`agora:media-upload:v1\n${disk}\n${key}\n${uploadId}`)
        .digest('base64url');
}
function sameToken(expected, actual) {
    const left = Buffer.from(expected);
    const right = Buffer.from(actual);
    return left.length === right.length && timingSafeEqual(left, right);
}
/**
 * Buffer the raw request body for a TUS `PATCH`. The chunk arrives as
 * `application/offset+octet-stream`, which the AdonisJS bodyparser leaves untouched, so the raw Node
 * request stream still holds the bytes.
 */
async function readRawBody(ctx) {
    const chunks = [];
    for await (const chunk of ctx.request.request)
        chunks.push(Buffer.from(chunk));
    return new Uint8Array(Buffer.concat(chunks));
}
//# sourceMappingURL=media_provider.js.map