@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
130 lines • 6.29 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
import { args, flags } from '@adonisjs/core/ace';
import { formatBytes, listRequest, MediaConsoleCommand } from '../src/commands/console_command.js';
import { readCollectionsQuery } from '../src/dashboard/list_api.js';
/** `node ace media:collections:list` — record count and total size per collection. */
export class MediaCollectionsList extends MediaConsoleCommand {
static commandName = 'media:collections:list';
static description = 'List collections with their record count and total size';
async handle(service) {
const summary = await service.collectionsSummary();
if (this.json)
return this.printJson(summary);
this.printTable(['Collection', 'Records', 'Size'], summary.data.map((row) => [row.key, String(row.count), formatBytes(row.sumSize)]), 'No media records.');
}
}
/** `node ace media:records:list` — stored media records, newest first (one page). */
export class MediaRecordsList extends MediaConsoleCommand {
static commandName = 'media:records:list';
static description = 'List stored media records, newest first (one page)';
async handle(service) {
const params = await readCollectionsQuery(listRequest({
filter: {
collection: this.collection,
ownerType: this.ownerType,
ownerId: this.ownerId,
prefix: this.prefix,
},
sort: this.sort,
after: this.after,
first: this.first,
}));
const page = await service.collections(params);
if (this.json)
return this.printJson(page);
this.printTable(['Id', 'Collection', 'Owner', 'File', 'Type', 'Size', 'Created'], page.data.map((record) => [
record.id,
record.collection,
`${record.ownerType}#${record.ownerId}`,
record.fileName,
record.mimeType,
formatBytes(record.sizeBytes),
record.createdAt,
]), 'No media records.');
if (page.meta.nextCursor !== null) {
this.logger.info(`More: --after ${JSON.stringify(page.meta.nextCursor)}`);
}
}
}
__decorate([
flags.string({ description: 'Only this collection' })
], MediaRecordsList.prototype, "collection", void 0);
__decorate([
flags.string({ description: 'Only this owner type, e.g. User' })
], MediaRecordsList.prototype, "ownerType", void 0);
__decorate([
flags.string({ description: 'Only this owner id (usually with --owner-type)' })
], MediaRecordsList.prototype, "ownerId", void 0);
__decorate([
flags.string({ description: 'Only records whose stored path starts with this' })
], MediaRecordsList.prototype, "prefix", void 0);
__decorate([
flags.string({ description: "Cursor of the next page (the previous page's nextCursor)" })
], MediaRecordsList.prototype, "after", void 0);
__decorate([
flags.number({ description: 'Page size (default 50)' })
], MediaRecordsList.prototype, "first", void 0);
__decorate([
flags.string({ description: 'Sort: "-createdAt" (the only order records list in)' })
], MediaRecordsList.prototype, "sort", void 0);
/** `node ace media:records:show <id>` — one media record, with a URL per generated conversion. */
export class MediaRecordsShow extends MediaConsoleCommand {
static commandName = 'media:records:show';
static description = 'Show one media record and its conversions';
async handle(service) {
const detail = await service.mediaRecord(this.id);
if (this.json)
return this.printJson(detail);
const { record } = detail;
this.printFields([
['Id', record.id],
['Collection', record.collection],
['Owner', `${record.ownerType}#${record.ownerId}`],
['Name', record.name],
['File name', record.fileName],
['Type', record.mimeType],
['Size', formatBytes(record.sizeBytes)],
['Disk', record.disk],
['Path', record.path],
['Organization', record.tenantId],
['Created', record.createdAt],
['Updated', record.updatedAt],
['Conversions', record.conversions.length > 0 ? record.conversions.join(', ') : '-'],
]);
for (const variant of detail.variants)
this.logger.log(` ${variant.name}: ${variant.url}`);
}
}
__decorate([
args.string({ description: 'Media record id' })
], MediaRecordsShow.prototype, "id", void 0);
/** `node ace media:records:delete <id>` — delete a media record (its files are left on the disk). */
export class MediaRecordsDelete extends MediaConsoleCommand {
static commandName = 'media:records:delete';
static description = 'Delete a media record (the stored files stay on the disk)';
async handle(service) {
this.assertActions(service);
// Read first: an unknown (or out-of-scope) id fails before the question is asked.
const record = await service.mediaEntry(this.id);
const what = `media record ${record.id} (${record.collection}, ${record.fileName})`;
if (!(await this.confirmed(this.force, `Delete ${what}?`)))
return;
await service.deleteMediaRecord(this.id);
await this.audit(`deleted ${what}`);
if (this.json)
return this.printJson({ ok: true, id: record.id });
this.logger.success(`Deleted ${what}`);
}
}
__decorate([
args.string({ description: 'Media record id' })
], MediaRecordsDelete.prototype, "id", void 0);
__decorate([
flags.boolean({ description: 'Do not ask for confirmation' })
], MediaRecordsDelete.prototype, "force", void 0);
//# sourceMappingURL=console_records.js.map