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

122 lines • 5.56 kB
import { Readable } from 'node:stream'; import { MediaControlPlaneError } from '../errors.js'; import { isHeaderSafe, safeContentType } from '../http_safety.js'; import { fromWireRecord } from './protocol.js'; import { nodeStreamOf } from './tenant_client.js'; const enc = encodeURIComponent; /** * A person's own files on the media control plane (`controlPlane.accounts` there), shared by every * app: an avatar uploaded in one app shows in all of them. * * - reading uses this app's service token (`media:read`); * - writing and removing use the person's own access token: the control plane writes to the token's * `sub`, so no app can change another person's file. * * ```ts * // a controller: the person's avatar, served by this app * return media.accounts.serve(ctx, params.id, 'avatar', { conversion: 'md' }) * * // the person changes it (their session's access token) * await media.accounts.replace(session.accessToken, 'avatar', { contents, mimeType }) * ``` */ export class TenantAccounts { client; constructor(client) { this.client = client; } /** The person's records in `collection` (one for a `single` collection). */ async records(accountId, collection) { const body = await this.client.json(`/accounts/${enc(accountId)}/${enc(collection)}`); return body.data.map(fromWireRecord); } /** The bytes of the person's file (a conversion of it), with `If-None-Match` passed on. */ async read(accountId, collection, options = {}) { let response; try { response = await this.client.request(`/accounts/${enc(accountId)}/${enc(collection)}/file`, { query: { conversion: options.conversion, w: options.width }, ...(options.ifNoneMatch ? { headers: { 'if-none-match': options.ifNoneMatch } } : {}), }); } catch (error) { if (error instanceof MediaControlPlaneError && error.status === 404) { return { kind: 'not-found' }; } throw error; } const etag = response.headers.get('etag') ?? undefined; if (response.status === 304) { await response.body?.cancel(); return { kind: 'not-modified', etag }; } const length = Number(response.headers.get('content-length')); return { kind: 'file', stream: nodeStreamOf(response), contentType: response.headers.get('content-type') ?? 'application/octet-stream', ...(Number.isFinite(length) && length > 0 ? { size: length } : {}), etag, }; } /** * Answer a request with the person's file: `200` with the bytes, `304` when the browser's copy is * current, `404` when there is none. Cached privately for a minute by default. */ async serve(ctx, accountId, collection, options = {}) { const ifNoneMatch = ctx.request.header('if-none-match') ?? undefined; const read = await this.read(accountId, collection, { ...(options.conversion !== undefined ? { conversion: options.conversion } : {}), ...(options.width !== undefined ? { width: options.width } : {}), ...(ifNoneMatch ? { ifNoneMatch } : {}), }); ctx.response.header('cache-control', options.cacheControl ?? 'private, max-age=60'); if (read.kind === 'not-found') { ctx.response.status(404); return ctx.response.send(''); } if (read.etag && isHeaderSafe(read.etag)) ctx.response.header('etag', read.etag); if (read.kind === 'not-modified') { ctx.response.status(304); return ctx.response.send(''); } ctx.response.header('content-type', safeContentType(read.contentType)); if (read.size !== undefined) ctx.response.header('content-length', String(read.size)); ctx.response.header('x-content-type-options', 'nosniff'); return ctx.response.stream(read.stream); } /** * Replace the person's file in `collection` with `contents`, as the owner of `accessToken` * (their own access token, never this app's). Refused content answers `422` with the media error * code (`E_MEDIA_MIME_NOT_ALLOWED`, `E_MEDIA_CONTENT_TYPE_MISMATCH`, …). */ async replace(accessToken, collection, input) { const size = Buffer.isBuffer(input.contents) ? input.contents.byteLength : input.size; const body = await this.client.json(`/accounts/me/${enc(collection)}`, { method: 'PUT', upload: true, bearer: accessToken, headers: { 'content-type': 'application/octet-stream', 'x-media-mime-type': input.mimeType, ...(input.fileName ? { 'x-media-file-name': input.fileName } : {}), ...(size !== undefined ? { 'x-media-size': String(size) } : {}), }, body: input.contents instanceof Readable ? input.contents : new Uint8Array(input.contents), }); return fromWireRecord(body.data); } /** Remove the person's files in `collection`, as the owner of `accessToken`. Idempotent. */ async remove(accessToken, collection) { const response = await this.client.request(`/accounts/me/${enc(collection)}`, { method: 'DELETE', bearer: accessToken, }); await response.body?.cancel(); } } //# sourceMappingURL=tenant_accounts.js.map