@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
117 lines • 5.97 kB
TypeScript
import type { DirectUploadManager } from './direct_upload.js';
import type { AttachExistingInput } from './media_library.js';
import type { MediaRecord } from './media_record.js';
import type { DirectUploadPolicy, MultipartPart } from './types.js';
/**
* A web-framework-neutral direct-upload request, one variant per lifecycle step (the provider maps
* an AdonisJS `HttpContext` to this; any other framework maps its own request the same way).
*/
export type DirectUploadRequest = {
action: 'initiate';
/** Name of the file being uploaded — feeds `keyFor`, never used as a key verbatim. */
fileName: string;
/** Total size in bytes. */
size: number;
contentType?: string | undefined;
/** Extra metadata persisted on the session. */
metadata?: Record<string, string> | undefined;
} | {
action: 'status';
uploadId: string;
} | {
action: 'confirm-part';
uploadId: string;
partNumber: number;
etag: string;
} | {
action: 'complete';
uploadId: string;
parts?: MultipartPart[] | undefined;
} | {
action: 'abort';
uploadId: string;
};
/** A web-framework-neutral response: a status code and a JSON-serializable body. */
export interface DirectUploadResponse {
status: number;
body?: unknown;
}
export interface DirectUploadHandlerOptions {
manager: DirectUploadManager;
/** Disk uploads land on. Defaults to the manager's default disk. */
disk?: string | undefined;
/**
* Collection this endpoint uploads into. Forwarded to the manager's `initiate`, whose MIME gate
* (against the registry the manager holds) rejects a wrong declared type with `415` before the
* multipart upload opens. The collection config stays the single source of truth.
*/
collection?: string | undefined;
/** Reject initiations whose `size` exceeds this many bytes (`413`). */
maxSize?: number | undefined;
/** Compute the final object key. Default: `uploads/<token>/<fileName>`. */
keyFor?: ((fileName: string, token: string, metadata: Record<string, string>) => string) | undefined;
idGenerator?: (() => string) | undefined;
/**
* App-injected strategy that owns the variant decisions (key resolution, what the upload becomes
* on completion, error→HTTP mapping). When present, `initiate`/`complete`/`abort` route through
* it; when absent, the handler keeps its built-in key/complete behavior. See {@link DirectUploadPolicy}.
*/
policy?: DirectUploadPolicy<unknown, unknown> | undefined;
/**
* Adopt an assembled object into the media library — wired to `MediaManager.completeDirectUploadToLibrary`
* by the provider. The policy's `complete` path calls it with the resolution's `sessionId`/`target`
* (plus the caller-supplied parts) and returns the resulting record. Unused without a {@link policy}.
*/
adopt?: ((sessionId: string, target: Omit<AttachExistingInput, 'key' | 'disk' | 'size'>, parts?: MultipartPart[] | undefined) => Promise<MediaRecord>) | undefined;
}
/**
* HTTP half of the session-backed direct upload flow, mirroring {@link TusUploadHandler}: the core
* ({@link DirectUploadManager}) never sees HTTP, this class never sees a framework, and a thin
* AdonisJS route adapter maps `HttpContext` to {@link DirectUploadRequest}/{@link DirectUploadResponse}.
*
* The endpoint surface it expects to be mounted on (see the provider's `uploads.direct.routes`):
*
* - `POST /` → `initiate` — `201` + session id, part size, presigned URLs
* - `GET /:id` → `status` — confirmed ETags + fresh URLs for pending parts
* - `POST /:id/parts/:partNumber` → `confirm-part` — record one uploaded part's ETag
* - `POST /:id/complete` → `complete` — assemble; `409` names any missing parts
* - `DELETE /:id` → `abort`
*
* **This handler performs NO authorization** — the same split as `TusUploadHandler` and
* `MediaDeliveryHandler`. Who may upload, and into whose namespace the key resolves, are questions
* only the app can answer: guard the route, and resolve the key server-side via {@link keyFor}
* (the client sends a `fileName`, never a key).
*/
export declare class DirectUploadHandler {
private readonly manager;
private readonly disk;
private readonly collection;
private readonly maxSize;
private readonly keyFor;
private readonly newId;
private readonly policy;
private readonly adopt;
constructor(options: DirectUploadHandlerOptions);
handle(req: DirectUploadRequest, ctx?: unknown): Promise<DirectUploadResponse>;
/** The `201` body for a created session — shared by the built-in and policy initiate paths. */
private initiateBody;
/**
* Policy-driven `initiate`: the policy decides the key/collection/metadata (and may attach a
* `response` merged onto the `201`), the manager opens the upload. If anything throws after the
* decision, the decision's `rollback` runs and the policy's `mapError` gets first refusal on the
* HTTP mapping (with the decision in the info); an unmapped error falls through to {@link mapError}.
*/
private initiatePolicy;
/**
* Policy-driven `complete`: `resolveComplete` maps the session onto a library `target`, `adopt`
* assembles + adopts the object, and the `200` body is whatever `onComplete` returns. Parts-shape
* validation is deliberately skipped — the policy owns what a completion means. `mapError` (with
* the resolution in the info) gets first refusal on failures; else {@link mapError}.
*/
private completePolicy;
/** Policy-driven `abort`: the policy cleans up app-side state, then the session is dropped. */
private abortPolicy;
/** Map manager errors onto HTTP statuses; anything unexpected keeps propagating. */
private mapError;
}
//# sourceMappingURL=direct_upload_handler.d.ts.map