@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
114 lines • 4.96 kB
JavaScript
import { TransformerRuntimeMissingError } from '../errors.js';
/**
* The built-in **image placeholder** {@link Transformer} — a metadata-only transform: it writes no
* artifact, and persists on `record.conversions['placeholder'].meta` a tiny inline preview
* (`dataUrl`), the original's dimensions and its dominant colour. Everything a page needs to
* render a blurred LQIP with the right aspect ratio before the real image arrives, without a
* second request.
*
* ```ts
* // config/media.ts
* { name: 'avatar', acceptsMimeTypes: ['image/png', 'image/jpeg'], transformers: [transformers.placeholder()] }
*
* const record = await media.library.find(id)
* record.conversions.placeholder?.meta // { dataUrl: 'data:image/webp;base64,…', width, height, color }
* ```
*
* Eager by default. A record whose MIME type is not `image/*`, or is an SVG, is skipped (`meta: {}`) rather than
* failing the attach, so declaring it on a mixed collection (images + PDFs) is safe.
*/
export class PlaceholderTransformer {
name;
eager;
options;
engine;
constructor(options = {}) {
this.name = options.name ?? 'placeholder';
this.eager = options.eager ?? true;
this.options = { size: options.size ?? 16, quality: options.quality ?? 50 };
this.engine = options.engine;
}
async transform(context) {
// Vectors are skipped too: they render crisp at any size (nothing to blur up to), and an SVG
// without intrinsic dimensions makes sharp throw — which, eager, would roll back the attach.
const { mimeType } = context.record;
if (!mimeType.startsWith('image/') || mimeType.startsWith('image/svg'))
return { meta: {} };
const engine = this.engine ?? (await loadSharpPlaceholderEngine(this.name));
const placeholder = await engine.generate(await context.getBytes(), this.options);
return { meta: { ...placeholder } };
}
}
/**
* Read the blur-up placeholder off a record: the first conversion whose `meta` has the
* {@link ImagePlaceholder} shape, so a renamed transformer (`name: 'lqip'`) is found too. `null`
* when the collection has no placeholder transformer, it hasn't run, or it skipped a non-image.
*
* ```ts
* const record = await media.library.find(user.avatarMediaId)
* return { avatarUrl, avatarPlaceholder: record && placeholderOf(record) }
* ```
*/
export function placeholderOf(record) {
for (const conversion of Object.values(record.conversions)) {
const meta = conversion.meta;
if (meta &&
typeof meta.dataUrl === 'string' &&
typeof meta.color === 'string' &&
Number.isFinite(meta.width) &&
Number.isFinite(meta.height)) {
return {
dataUrl: meta.dataUrl,
width: meta.width,
height: meta.height,
color: meta.color,
};
}
}
return null;
}
/**
* sharp-backed {@link PlaceholderEngine}, importing the optional peer on first use.
*
* @internal `importSharp` is the seam tests use to simulate a host without the peer.
*/
export async function loadSharpPlaceholderEngine(transformerName, importSharp = () => import('sharp')) {
let sharp;
try {
sharp = (await importSharp()).default;
}
catch (error) {
// Only a missing `sharp` itself — a broken install missing a transitive dep must surface as is.
if (error.code === 'ERR_MODULE_NOT_FOUND' &&
error instanceof Error &&
error.message.includes("'sharp'")) {
throw new TransformerRuntimeMissingError(transformerName, "the optional peer dependency 'sharp'", 'Install it (`pnpm add sharp`) or inject a custom `engine`.');
}
throw error;
}
return {
async generate(input, { size, quality }) {
const image = sharp(input, { failOn: 'none' }).rotate();
const metadata = await image.metadata();
// `metadata()` reports the stored (pre-rotation) size; orientations 5–8 swap the axes.
const swap = (metadata.orientation ?? 1) >= 5;
const width = (swap ? metadata.height : metadata.width) ?? 0;
const height = (swap ? metadata.width : metadata.height) ?? 0;
const [{ dominant }, tiny] = await Promise.all([
image.clone().stats(),
image
.clone()
.resize(size, size, { fit: 'inside' })
.webp({ quality, alphaQuality: quality })
.toBuffer(),
]);
return {
dataUrl: `data:image/webp;base64,${tiny.toString('base64')}`,
width,
height,
color: `#${[dominant.r, dominant.g, dominant.b].map((c) => c.toString(16).padStart(2, '0')).join('')}`,
};
},
};
}
//# sourceMappingURL=placeholder.js.map