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

147 lines • 6.99 kB
import type { StorageManager } from './storage_manager.js'; import type { MultipartPart } from './types.js'; /** * One in-flight resumable upload. Persisted through an {@link UploadSessionStore} so a dropped * connection resumes from {@link offset} rather than re-sending. Bytes accumulate as temporary * parts on the target disk (native S3 multipart when the disk is multipart-capable, otherwise * buffered part objects) until {@link ResumableUploadManager.complete} assembles the final object. */ export interface UploadSession { id: string; disk: string; /** Final object key the assembled upload will be written to. */ key: string; contentType: string | undefined; /** Total expected size in bytes, if declared up front (TUS `Upload-Length`). */ size: number | undefined; /** Bytes received so far — the resume offset. */ offset: number; /** Number of chunk parts written so far. */ parts: number; /** S3 (or other native-multipart) upload id, when the target disk is multipart-capable. */ multipartUploadId?: string; /** When the session was created. */ createdAt?: Date; /** * When the session last changed (a chunk or part landed). The built-in stores set it on `create` * (= `createdAt`) and on every `update`. Absent for sessions stored before the column existed. */ updatedAt?: Date; /** When the session expires (TUS `expiration` extension); absent = never expires. */ expiresAt?: Date; /** Decoded TUS `Upload-Metadata`, retained for finalization/inspection. */ metadata?: Record<string, string>; /** The tenant (organization) the upload belongs to, when tenancy is on (`null`: no tenant). */ tenantId?: string | null; } /** Optional filter for {@link UploadSessionStore.list}. */ export interface UploadSessionListFilter { /** Only sessions on this disk. */ disk?: string; /** Only sessions whose `key` starts with this prefix. */ keyPrefix?: string; /** Only sessions of these tenants (`null`: no tenant). */ tenantIds?: ReadonlyArray<string | null>; } /** * Persistence SPI for resumable upload sessions. Ships with an in-memory driver * (`@adonis-agora/media/testing`) and a Lucid driver (`@adonis-agora/media/upload_sessions/lucid`), * mirroring the {@link MediaStore} pattern. `addPart`/`listParts` are optional and only needed when * the target disk is multipart-capable (they record the S3 part ETags for finalization). */ export interface UploadSessionStore { create(session: UploadSession): Promise<UploadSession>; get(id: string): Promise<UploadSession | null>; update(session: UploadSession): Promise<UploadSession>; delete(id: string): Promise<void>; /** Record one part's ETag, keyed by partNumber (multipart-capable disks only). */ addPart?(id: string, part: MultipartPart): Promise<void>; /** All recorded parts for a session (unordered). Used by `complete()` + resume. */ listParts?(id: string): Promise<MultipartPart[]>; /** * List currently-stored (in-progress) sessions, optionally filtered. Admin-facing (an "uploads in * progress" view), not a hot path. Optional: stores that cannot enumerate omit it and callers * degrade to an empty list. */ list?(filter?: UploadSessionListFilter): Promise<UploadSession[]>; } export interface CreateUploadInput { disk: string; key: string; size?: number; contentType?: string; metadata?: Record<string, string>; } export interface ResumableUploadManagerOptions { storage: StorageManager; sessions: UploadSessionStore; /** Prefix for temporary chunk parts on the target disk (buffered path). Default `.uploads`. */ tmpPrefix?: string; /** Session lifetime in seconds (TUS `expiration`). Omit for sessions that never expire. */ sessionTtlSeconds?: number; idGenerator?: () => string; /** Injectable clock (tests drive expiry deterministically). Default `() => new Date()`. */ clock?: () => Date; /** Emit `upload.*` diagnostics events (default true). */ emitDiagnostics?: boolean; } /** * Framework-agnostic resumable-upload engine — the storage core the TUS HTTP handler drives. Bytes * flow through the backend in chunks; each chunk is written immediately as a part on the target disk * (native S3 multipart when available, else a buffered part object), so a dropped connection resumes * from `offset` without re-sending. {@link complete} assembles the parts into the final object. * * This never imports AdonisJS; the provider exposes it over TUS routes and {@link MediaManager} * surfaces it as `media.resumable`. */ export declare class ResumableUploadManager { private readonly storage; private readonly sessions; private readonly tmpPrefix; private readonly ttlSeconds; private readonly newId; private readonly now; private readonly emitDiagnostics; constructor(options: ResumableUploadManagerOptions); private partKey; /** Begin a resumable upload session and reserve any native multipart upload the disk supports. */ createUpload(input: CreateUploadInput): Promise<UploadSession>; /** Append a chunk at `offset` (must equal the session's current offset). Returns the new offset. */ writeChunk(id: string, offset: number, chunk: Uint8Array): Promise<{ offset: number; }>; /** * Current offset, declared size, declared content type, and expiry for a session (drives the TUS * `HEAD` response). `contentType` is what the client DECLARED at creation (TUS `Upload-Metadata`'s * `filetype`) — nothing here proves it; the TUS handler checks it against the real bytes. */ status(id: string): Promise<{ offset: number; size: number | undefined; contentType: string | undefined; expiresAt: Date | undefined; }>; /** Assemble all parts into the final object, clean up, and return the final key/disk/size. */ complete(id: string): Promise<{ key: string; disk: string; size: number; }>; /** Discard an in-flight session, freeing its native multipart upload or buffered parts. */ abort(id: string): Promise<void>; /** All recorded parts for a session (multipart-capable disks). */ listParts(id: string): Promise<MultipartPart[]>; /** In-progress sessions, optionally filtered. Empty when the store cannot enumerate. */ list(filter?: UploadSessionListFilter): Promise<UploadSession[]>; private emit; /** Yields the buffered parts in order, opening each one only once the previous is drained. */ private streamParts; private cleanup; /** * Load a session, treating an expired one as gone: its native/buffered parts are cleaned up, the * record is deleted, and an {@link UploadSessionExpiredError} is thrown (the TUS handler maps this * to `410 Gone`). */ private require; } //# sourceMappingURL=resumable_upload.d.ts.map