@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
90 lines • 4.7 kB
TypeScript
import type { MediaCollectionRegistry } from './media_collection.js';
import type { ResumableUploadManager } from './resumable_upload.js';
/** The TUS protocol version this server implements. */
export declare const TUS_VERSION = "1.0.0";
/** A web-framework-neutral TUS request (the provider maps an AdonisJS `HttpContext` to this). */
export interface TusRequest {
method: 'OPTIONS' | 'POST' | 'HEAD' | 'PATCH' | 'DELETE';
/** Upload id parsed from the URL (for HEAD/PATCH/DELETE). */
uploadId?: string;
headers: Record<string, string | undefined>;
body?: Uint8Array;
}
/** A web-framework-neutral TUS response (the provider writes it back onto the `HttpContext`). */
export interface TusResponse {
status: number;
headers: Record<string, string>;
body?: string;
}
export interface TusUploadHandlerOptions {
manager: ResumableUploadManager;
/** Disk uploads land on. */
disk: string;
/**
* Collection this endpoint uploads into. When set (together with {@link collections}), its
* `acceptsMimeTypes` is enforced at `POST` and on the first `PATCH` — see the class docs. The
* collection config stays the single source of truth: nothing here restates the MIME list.
*/
collection?: string;
/**
* The collection registry to read {@link collection} from — `media.collections` in an app. Passed
* rather than the resolved list precisely so the app cannot drift from `config/media.ts`.
*/
collections?: MediaCollectionRegistry;
/** Base path the upload resources are exposed at, for the `Location` header. Default `/uploads`. */
basePath?: string;
/** Reject creations whose `Upload-Length` exceeds this. */
maxSize?: number;
/** Compute the final object key. Default: `uploads/<token>/<filename>`. */
keyFor?: (filename: string, token: string, metadata: Record<string, string>) => string;
idGenerator?: () => string;
}
/** Parse a TUS `Upload-Metadata` header (`key b64val,key2 b64val2`). */
export declare function parseTusMetadata(header?: string): Record<string, string>;
/**
* TUS 1.0.0 server core (`creation` + `termination` + `expiration` extensions), delegating storage
* to a {@link ResumableUploadManager}. It is framework-agnostic: a thin AdonisJS route adapter maps
* the `HttpContext` request/response to/from {@link TusRequest}/{@link TusResponse}.
*
* Protocol surface:
* - `OPTIONS` — advertise `Tus-Version`/`Tus-Extension`/`Tus-Max-Size`.
* - `POST` — create with `Upload-Length` + `Upload-Metadata`; returns `201` + `Location`.
* - `HEAD` — report `Upload-Offset` (+ `Upload-Length`/`Upload-Expires`).
* - `PATCH` — append at `Upload-Offset` with `Content-Type: application/offset+octet-stream`;
* auto-completes at the declared length.
* - `DELETE` — terminate (abort) an in-flight upload.
*
* ## Collection-aware MIME validation (optional)
*
* Given a `collection` + `collections` registry, the handler enforces that collection's
* `acceptsMimeTypes` at the two earliest moments the protocol allows, so a client learns it sent the
* wrong file *before* spending the bandwidth a resumable upload exists to protect:
*
* 1. **`POST`** — the declared `filetype` in `Upload-Metadata`, if present, must be whitelisted.
* Rejected with `415 Unsupported Media Type`, before a single byte is uploaded.
* 2. **first `PATCH`** — the real magic-byte signature of the leading bytes is checked with
* {@link verifyContentAgainstWhitelist}. This catches a client that lied in `filetype`. Also
* `415`, and the session plus any partial object are aborted so nothing is left behind.
*
* This is **bandwidth economy and fast feedback, not the security boundary**. The storage invariant
* is still guaranteed by `MediaLibrary.attach` / `attachExisting`, which re-validate the assembled
* object at attach time and remain the final barrier — the TUS checks only see the first chunk, and
* an endpoint configured without a `collection` performs no MIME validation at all.
*/
export declare class TusUploadHandler {
#private;
private readonly manager;
private readonly disk;
private readonly collection;
/** Resolved from the collection registry at construction; `undefined` ⇒ no MIME gate here. */
private readonly accepts;
private readonly basePath;
private readonly maxSize;
private readonly keyFor;
private readonly newId;
constructor(options: TusUploadHandlerOptions);
handle(req: TusRequest): Promise<TusResponse>;
/** Map storage errors onto TUS status codes (409 conflict, 410 expired, 404 unknown). */
private mapError;
}
//# sourceMappingURL=tus.d.ts.map