@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
91 lines • 4.67 kB
JavaScript
import { MediaNotFoundError } from './errors.js';
import { deliverRecord, findUnscoped } from './internal_symbols.js';
/** Signed-URL lifetime used when neither the call nor `delivery.signedTtlSeconds` sets one. */
export const DEFAULT_DELIVERY_SIGNED_TTL_SECONDS = 300;
/**
* Collapse a requested {@link DeliveryMode} to a concrete one for a specific object.
*
* Only `auto` needs the disk. Visibility is asked of the disk itself (`getVisibility`, which every
* `@adonisjs/drive` disk exposes and which the bundled `disks.s3()` answers from its configured
* visibility) — never guessed from `getUrl` being callable, since a URL can always be *synthesised*
* for a private object and would resolve `auto` to a link that 403s.
*
* A disk that cannot answer leaves the visibility genuinely unknown, and the safe read of unknown is
* "assume private": `auto` then resolves to `signed`, which works on a public object too.
*/
export async function resolveDeliveryMode(mode, disk, key) {
const requested = mode ?? 'auto';
if (requested !== 'auto')
return requested;
if (typeof disk.getVisibility !== 'function')
return 'signed';
return (await disk.getVisibility(key)) === 'public' ? 'public' : 'signed';
}
/**
* `next/image`'s default `deviceSizes` + `imageSizes`: the widths a loader may ask for. The same
* list on both sides is what keeps a loader's `srcset` and the server's whitelist in step.
*/
export const DEFAULT_IMAGE_WIDTHS = [
16, 32, 48, 64, 96, 128, 256, 384, 640, 750, 828, 1080, 1200, 1920, 2048, 3840,
];
/** `next/image`'s default `qualities`: one, so a client cannot multiply the cache by quality. */
export const DEFAULT_IMAGE_QUALITIES = [75];
/**
* Framework-agnostic read counterpart to {@link TusUploadHandler}: the app mounts ONE route, with
* its own middleware, and delegates the "how do these bytes reach the client" question here.
*
* ```ts
* const delivery = new MediaDeliveryHandler({ library: media })
*
* router.get('/media/:id', async ({ params, response, auth }) => {
* await authorize(auth.user, params.id) // ← the app's job, see below
* const result = await delivery.handle({ mediaId: params.id })
* if (result.kind === 'redirect') return response.redirect(result.url)
* response.header('content-type', result.mimeType)
* return response.stream(result.stream)
* }).use(middleware.auth())
* ```
*
* **This handler performs NO authorization.** It resolves a media id to bytes or a URL and nothing
* else — exactly the split {@link TusUploadHandler} draws. Whoever may read a given record is a
* question only the app can answer, so guard the route (middleware, an ownership check, a signed
* media token) before calling `handle`. Mounting it unguarded publishes every record to anyone who
* can guess an id.
*/
export class MediaDeliveryHandler {
library;
mode;
signedTtlSeconds;
requireSignature;
constructor(options) {
const source = options.library;
this.library = 'library' in source ? source.library : source;
this.mode = options.mode;
this.signedTtlSeconds = options.signedTtlSeconds;
this.requireSignature = options.requireSignature;
}
async handle(request) {
// With `delivery.signing`, a signed request is authorized by its signature (any size the app
// signed); an unsigned one is refused when signing is required. Throws DeliverySignatureError.
const trusted = this.library.checkDeliverySignature(request, this.requireSignature !== undefined ? { required: this.requireSignature } : {});
const options = {
...(trusted ? { trusted: true } : {}),
...(request.conversion !== undefined ? { conversion: request.conversion } : {}),
...(request.width !== undefined && request.width !== ''
? { width: request.width, quality: request.quality, accept: request.accept }
: {}),
...(this.mode !== undefined ? { mode: this.mode } : {}),
...(this.signedTtlSeconds !== undefined ? { signedTtlSeconds: this.signedTtlSeconds } : {}),
};
// A signed request was authorized by the app that signed it: with tenancy on it is served
// whichever tenant the file belongs to.
if (trusted) {
const record = await this.library[findUnscoped](request.mediaId);
if (!record)
throw new MediaNotFoundError(request.mediaId);
return this.library[deliverRecord](record, options);
}
return this.library.deliver(request.mediaId, options);
}
}
//# sourceMappingURL=delivery.js.map