@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
112 lines • 5.17 kB
JavaScript
import { clampMediaListLimit, decodeMediaCursor, encodeMediaCursor, tenantMatches, } from '../media_store.js';
/** In-memory {@link MediaStore} for tests and scratch apps. */
export class InMemoryMediaStore {
records = new Map();
tenantFiltering = true;
async save(record) {
const copy = { ...record };
this.records.set(copy.id, copy);
return { ...copy };
}
async find(id) {
const found = this.records.get(id);
return found ? { ...found } : null;
}
async listByOwner(ownerType, ownerId, collection) {
return [...this.records.values()]
.filter((r) => r.ownerType === ownerType &&
r.ownerId === ownerId &&
(collection === undefined || r.collection === collection))
.sort((a, b) => a.order - b.order)
.map((r) => ({ ...r }));
}
async list(options = {}) {
const limit = clampMediaListLimit(options.limit);
const ordered = [...this.records.values()]
.filter((r) => (options.ownerType === undefined || r.ownerType === options.ownerType) &&
(options.ownerId === undefined || r.ownerId === options.ownerId) &&
(options.collection === undefined || r.collection === options.collection) &&
(options.prefix === undefined ||
options.prefix === '' ||
r.path.startsWith(options.prefix)) &&
tenantMatches(options.tenantIds, r.tenantId))
// Newest first, tie-broken by id desc — the same stable keyset the Lucid store paginates on.
.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime() || (a.id < b.id ? 1 : a.id > b.id ? -1 : 0));
// Drop everything up to and including the cursor row, per the (createdAt desc, id desc) order.
const cursor = decodeMediaCursor(options.cursor);
const after = cursor
? ordered.filter((r) => r.createdAt.getTime() < cursor.createdAt ||
(r.createdAt.getTime() === cursor.createdAt && r.id < cursor.id))
: ordered;
const page = after.slice(0, limit);
const hasMore = after.length > limit;
const last = page[page.length - 1];
return {
items: page.map((r) => ({ ...r })),
nextCursor: hasMore && last
? encodeMediaCursor({ createdAt: last.createdAt.getTime(), id: last.id })
: null,
};
}
async listByOwnerPaged(ownerType, ownerId, options = {}) {
const collections = options.collections;
const all = [...this.records.values()].filter((r) => r.ownerType === ownerType &&
r.ownerId === ownerId &&
(collections === undefined || collections.includes(r.collection)) &&
tenantMatches(options.tenantIds, r.tenantId));
return keysetPage(all, options.cursor, options.limit);
}
async delete(id) {
this.records.delete(id);
}
async collectionTotals(options = {}) {
const totals = new Map();
for (const record of this.records.values()) {
if (!tenantMatches(options.tenantIds, record.tenantId))
continue;
const entry = totals.get(record.collection) ?? {
collection: record.collection,
count: 0,
sumSize: 0,
newest: Number.NEGATIVE_INFINITY,
};
entry.count += 1;
entry.sumSize += record.size;
entry.newest = Math.max(entry.newest, record.createdAt.getTime());
totals.set(record.collection, entry);
}
return [...totals.values()]
.sort((a, b) => b.newest - a.newest)
.map(({ collection, count, sumSize }) => ({ collection, count, sumSize }));
}
async listTenants() {
const ids = new Set();
for (const record of this.records.values())
if (record.tenantId)
ids.add(record.tenantId);
return [...ids].sort();
}
async nextOrder(ownerType, ownerId, collection) {
const inCollection = await this.listByOwner(ownerType, ownerId, collection);
return inCollection.reduce((max, r) => Math.max(max, r.order + 1), 0);
}
}
/** Newest first (createdAt desc, id desc), resumed strictly after `cursor`. */
function keysetPage(records, cursorValue, limitValue) {
const limit = clampMediaListLimit(limitValue);
const ordered = records.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime() || (a.id < b.id ? 1 : a.id > b.id ? -1 : 0));
const cursor = decodeMediaCursor(cursorValue);
const after = cursor
? ordered.filter((r) => r.createdAt.getTime() < cursor.createdAt ||
(r.createdAt.getTime() === cursor.createdAt && r.id < cursor.id))
: ordered;
const page = after.slice(0, limit);
const last = page[page.length - 1];
return {
items: page.map((r) => ({ ...r })),
nextCursor: after.length > limit && last
? encodeMediaCursor({ createdAt: last.createdAt.getTime(), id: last.id })
: null,
};
}
//# sourceMappingURL=in_memory_media_store.js.map