@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
69 lines • 3.28 kB
JavaScript
import { describeStackUse, endpointHost, s3EndpointTech } from '../stack.js';
/**
* The disk factory namespace used in `config/media.ts`, parallel to `stores` and `processors`:
*
* ```ts
* import { defineConfig, disks } from '@adonis-agora/media'
*
* export default defineConfig({
* disk: 's3',
* disks: {
* s3: disks.s3({ bucket: 'my-bucket', region: 'us-east-1', publicBaseUrl: 'https://cdn.example.com' }),
* },
* })
* ```
*
* Each factory returns a {@link DiskFactory} — a lazy thunk. Calling it in the config file costs
* nothing; `@aws-sdk/client-s3` is only imported when the provider builds the selected disk at
* boot, keeping the AWS SDK an optional peer. (Presigning needs no SDK at all — it is hand-rolled
* SigV4, see `sigv4.ts`.)
*/
export const disks = {
/** S3 (or S3-compatible) disk — needs the optional `@aws-sdk/client-s3` peer. */
s3(config) {
const factory = async () => {
const { S3Client } = await import('@aws-sdk/client-s3');
const { S3Disk } = await import('./s3.js');
const client = new S3Client({
...(config.region !== undefined ? { region: config.region } : {}),
...(config.credentials !== undefined ? { credentials: config.credentials } : {}),
...(config.endpoint !== undefined ? { endpoint: config.endpoint } : {}),
...(config.forcePathStyle !== undefined ? { forcePathStyle: config.forcePathStyle } : {}),
});
return new S3Disk({
client,
bucket: config.bucket,
...(config.keyPrefix !== undefined ? { keyPrefix: config.keyPrefix } : {}),
...(config.publicBaseUrl !== undefined ? { publicBaseUrl: config.publicBaseUrl } : {}),
...(config.region !== undefined ? { region: config.region } : {}),
...(config.endpoint !== undefined ? { endpoint: config.endpoint } : {}),
...(config.publicEndpoint !== undefined ? { publicEndpoint: config.publicEndpoint } : {}),
...(config.forcePathStyle !== undefined ? { forcePathStyle: config.forcePathStyle } : {}),
...(config.visibility !== undefined ? { visibility: config.visibility } : {}),
});
};
// Telescope's Architecture page: the bucket, never the endpoint (it may carry credentials).
const host = endpointHost(config.endpoint) ?? config.region;
return describeStackUse(factory, {
tech: s3EndpointTech(config.endpoint),
label: config.bucket,
purpose: 'media files',
...(host !== undefined ? { host } : {}),
});
},
};
/**
* Build every disk named in `config.disks` into a concrete {@link Disk} map. The media provider
* calls this once at boot (each factory lazily loads its peer), then serves the resulting disks
* from a synchronous resolver, falling back to Drive for any name not defined here.
*/
export async function resolveConfiguredDisks(factories) {
const built = {};
if (factories) {
for (const [name, factory] of Object.entries(factories)) {
built[name] = await factory();
}
}
return built;
}
//# sourceMappingURL=factory.js.map