UNPKG

@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

628 lines • 29.6 kB
import { uint8View } from '../disk_read.js'; import { isExtendedDisk } from '../extended_disk.js'; import { sanitizeInsight } from './object_insights.js'; import { objectProxyUrl } from './object_urls.js'; /** TTL of the signed URLs handed to the browser for object preview/download. */ export const URL_TTL_SECONDS = 300; /** Cap on the get→put streaming path used for cross-disk copy/move (drivers can't copy across disks). */ export const MAX_TRANSFER_BYTES = 100 * 1024 * 1024; /** Page size for the flat sweep that recursive folder delete/copy/move paginates over. */ export const FOLDER_SWEEP_LIMIT = 1000; /** Page size for the client-side scan behind {@link DashboardService.collectionsSummary}. */ export const COLLECTIONS_SUMMARY_PAGE_SIZE = 200; /** Hard cap on records scanned by {@link DashboardService.collectionsSummary} — beyond this the * rollup is partial rather than unbounded. */ export const COLLECTIONS_SUMMARY_SCAN_LIMIT = 5000; /** Ceiling for a console (direct) upload — buffered in memory, so bounded to protect the process * heap. Larger files belong on the resumable (TUS) upload path, not this convenience upload. */ export const MAX_UPLOAD_BYTES = 100 * 1024 * 1024; /** Strip leading and trailing slashes — a folder prefix normalized to its bare path (no delimiter). */ function normalizeFolder(prefix) { return prefix.replace(/^\/+/, '').replace(/\/+$/, ''); } /** An error carrying an HTTP status for the provider to surface verbatim. */ export class DashboardError extends Error { status; constructor(message, status) { super(message); this.status = status; this.name = 'DashboardError'; } } /** Shown by the console when the Lucid upload-session store's tables do not exist yet. */ export const UPLOAD_SESSIONS_TABLE_MISSING = 'upload sessions table missing — run the media migration (node ace migration:run)'; /** * Whether `error` is the database saying a table does not exist: Postgres `42P01`, MySQL/MariaDB * `ER_NO_SUCH_TABLE` (1146), SQLite `no such table`, SQL Server 208 (`Invalid object name`). */ export function isMissingTableError(error) { if (typeof error !== 'object' || error === null) return false; const { code, errno, number, message } = error; if (code === '42P01' || code === 'ER_NO_SUCH_TABLE' || errno === 1146 || number === 208) { return true; } return typeof message === 'string' && /no such table|invalid object name/i.test(message); } /** Run an upload-session store call, turning a missing table into a 503 the console can show. */ async function withUploadSessionTables(run) { try { return await run(); } catch (error) { if (isMissingTableError(error)) throw new DashboardError(UPLOAD_SESSIONS_TABLE_MISSING, 503); throw error; } } /** * All dashboard read/action logic, expressed over a structural {@link MediaManagerLike}. It reads the * *real* `@adonis-agora/media` surface — disk `list`/`stat`/`getSignedUrl`/`copy`/`move`/`deleteMany` * and the resumable `UploadSessionStore.list()` — and never invents its own persistence. The provider * is a thin HTTP shell around this; the logic is pure and unit-tested without booting AdonisJS. */ export class DashboardService { manager; options; insightProviders; constructor(manager, options, /** Host-registered {@link ObjectInsightProvider}s, from `config/media_dashboard.ts`'s `objectInsights`. */ insightProviders = []) { this.manager = manager; this.options = options; this.insightProviders = insightProviders; } /** Coarse capability topology for the SPA to enable/disable affordances. */ topology() { return { disks: this.options.diskNames.length, hasUploads: this.manager.hasResumable, actions: this.options.actions, }; } /** Enumerate the exposed disks with their capabilities (non-extended disks report `list:false`). */ disks() { const defaultDisk = this.manager.storage.defaultDisk; const disks = this.options.diskNames.map((name) => { const disk = this.manager.storage.disk(name); const capabilities = isExtendedDisk(disk) ? disk.capabilities : { presign: false, multipart: false, publicUrls: false, list: false }; return { name, default: name === defaultDisk, capabilities }; }); return { disks }; } /** * List one page of a bucket under `prefix` (folders rolled up on the `/` delimiter), paginated * through the ecosystem's cursor interface ({@link CursorParams} → the `CursorPageInfo` envelope): * `after` is the previous page's `nextCursor`, `first` its size. * * `after`/`first` map onto the driver's own `cursor`/`limit` — S3's `ListObjectsV2` continuation * token — which is why the response's `prevCursor`/`hasPrev` are pinned to `null`/`false`: there is * no backwards token to hand out. */ async objects(diskName, params = {}) { const synthetic = this.syntheticFolders(params.prefix ?? ''); if (synthetic) return synthetic; this.assertKey(params.prefix ?? ''); const disk = this.extended(diskName); const result = await disk.list(params.prefix ?? '', { delimiter: '/', ...(params.after !== undefined ? { cursor: params.after } : {}), ...(params.first !== undefined ? { limit: params.first } : {}), }); // Drop phantom folders whose name is empty — a CommonPrefix of only slashes (`/`, `//`), // produced by a stray key with a leading slash. The S3 driver normalizes such a prefix back // to the root, so listing INTO one returns the root again (the phantom included) — a self- // reference that infinite-loops the browser (endless re-list of root). Filtering it out removes // the trap; those leading-slash keys are unreachable from the console anyway (the driver strips // the leading slash). const folders = result.folders .map((prefix) => ({ name: folderName(prefix), prefix })) .filter((folder) => folder.name !== ''); const files = result.files.map((entry) => ({ key: entry.key, name: entry.name, sizeBytes: entry.sizeBytes, lastModified: entry.lastModified ? entry.lastModified.toISOString() : null, })); return { folders, files, nextCursor: result.cursor ?? null, // Forward-only backend: the continuation token walks one way, so there is never a previous // cursor to report. The fields stay, pinned, so the shape matches the ecosystem's page type. prevCursor: null, hasNext: result.cursor !== undefined, hasPrev: false, }; } /** * The URL to report for an object's bytes, under the host's {@link ObjectUrlStrategy}. `auto` * signs a short-lived URL straight to the store; `proxy` routes through this server, for a host * whose browser cannot reach the store at all. */ objectUrl(disk, diskName, key) { if (this.options.objectUrls?.strategy === 'proxy') { return Promise.resolve(objectProxyUrl(this.options.objectUrls.apiBasePath, diskName, key)); } return disk.getSignedUrl(key, { expiresIn: URL_TTL_SECONDS }); } /** Object metadata + a URL for preview/download (signed, or this console's proxy — see `objectUrls`). */ async object(diskName, key) { const meta = await this.objectMeta(diskName, key); const url = await this.objectUrl(this.manager.storage.disk(diskName), diskName, key); return { ...meta, url }; } /** {@link object} without the URL: size, content type and last modified only. */ async objectMeta(diskName, key) { if (!key) throw new DashboardError('key is required', 400); this.assertKey(key); const disk = this.manager.storage.disk(diskName); const stat = isExtendedDisk(disk) ? await disk.stat(key) : await metaAsStat(disk, key); return { key, size: stat.size, ...(stat.contentType !== undefined ? { contentType: stat.contentType } : {}), ...(stat.lastModified !== undefined ? { lastModified: stat.lastModified.toISOString() } : {}), }; } /** * The object's raw byte stream plus the metadata the provider needs to serve it inline. Backs the * console's same-origin preview proxy so text/PDF render in the browser instead of downloading * (and so a CORS-locked bucket is still previewable). */ async objectStream(diskName, key) { if (!key) throw new DashboardError('key is required', 400); this.assertKey(key); const disk = this.manager.storage.disk(diskName); const stat = isExtendedDisk(disk) ? await disk.stat(key) : await metaAsStat(disk, key); const stream = await disk.getStream(key); return { stream, contentType: stat.contentType ?? 'application/octet-stream', size: stat.size }; } /** * What the HOST knows about this object, from every registered `objectInsights` provider (see * `config/media_dashboard.ts`). `{ insights: [] }` when none are registered. A provider that * throws is skipped and logged — annotation must never be able to stop an admin opening a file, and * providers run concurrently so one host lookup being slow doesn't serialize the others. */ async objectInsights(diskName, key) { this.assertKey(key); if (this.insightProviders.length === 0) return { insights: [] }; // Validates the disk the same way every other object read does, so an unknown disk 404s here too. this.manager.storage.disk(diskName); const settled = await Promise.all(this.insightProviders.map(async (provider) => { try { return await provider.resolve({ disk: diskName, key }); } catch (error) { console.warn(`[media-dashboard] object-insight provider "${provider.id}" failed for ${diskName}:${key} — ${error instanceof Error ? error.message : String(error)}`); return null; } })); return { insights: settled .filter((insight) => insight !== null) .map(sanitizeInsight), }; } /** In-progress resumable uploads, projected from the session store (empty when unconfigured). */ async uploads(filter = {}) { if (!this.manager.hasResumable) return { uploads: [] }; const sessions = await withUploadSessionTables(() => this.manager.resumable.list({ ...(filter.disk !== undefined ? { disk: filter.disk } : {}), ...(filter.prefix !== undefined ? { keyPrefix: filter.prefix } : {}), })); return { uploads: sessions.map(toUploadInfo) }; } /** Full detail (+ recorded parts) of one resumable upload session. */ async uploadDetail(id) { if (!this.manager.hasResumable) throw new DashboardError('no upload store configured', 404); const sessions = await withUploadSessionTables(() => this.manager.resumable.list()); const session = sessions.find((candidate) => candidate.id === id); if (!session) throw new DashboardError(`unknown upload: ${id}`, 404); const parts = await withUploadSessionTables(() => this.manager.resumable.listParts(id)); return { upload: toUploadInfo(session), parts }; } /** * Cancels a resumable session: discards its native multipart upload (or buffered parts) and drops * its record, so it stops appearing as in-progress. Actions-gated. */ async abortUpload(id) { this.assertActions(); if (!this.manager.hasResumable) throw new DashboardError('no upload store configured', 404); await withUploadSessionTables(() => this.manager.resumable.abort(id)); } /** * One cursor-paginated page of stored media-library records across owners/collections, projected to * the SPA's {@link MediaEntry}. Delegates to the real {@link MediaStore.list} — no bespoke query. * * Paginated through the ecosystem's cursor interface ({@link CursorParams} → `CursorPage`): * `after`/`first` map onto the store's `cursor`/`limit` keyset page, which — like the disk listing — * only walks forward, hence the pinned `prevCursor: null` / `hasPrev: false`. */ async collections(params = {}) { const page = await this.manager.store.list({ ...(params.collection !== undefined ? { collection: params.collection } : {}), ...(params.ownerType !== undefined ? { ownerType: params.ownerType } : {}), ...(params.ownerId !== undefined ? { ownerId: params.ownerId } : {}), ...(params.prefix !== undefined ? { prefix: params.prefix } : {}), ...(params.after !== undefined ? { cursor: params.after } : {}), ...(params.first !== undefined ? { limit: params.first } : {}), }); return { items: page.items.map(toMediaEntry), nextCursor: page.nextCursor, prevCursor: null, hasNext: page.nextCursor !== null, hasPrev: false, }; } /** * Per-collection rollup (record count + total bytes) for the console's collection chips. One * aggregate query when the store implements {@link MediaStore.collectionTotals} (both bundled * stores do). A store without it is walked through `list()` in batches and summed client-side — * bounded at {@link COLLECTIONS_SUMMARY_SCAN_LIMIT} records so a very large library degrades to a * partial (still useful) summary instead of an unbounded scan. */ async collectionsSummary() { const store = this.manager.store; if (typeof store.collectionTotals === 'function') { const rows = await store.collectionTotals(); return { collections: rows.map(({ collection, count, sumSize }) => ({ key: collection, count, sumSize, })), }; } const totals = new Map(); let cursor; let scanned = 0; do { const page = await this.manager.store.list({ limit: COLLECTIONS_SUMMARY_PAGE_SIZE, ...(cursor !== undefined ? { cursor } : {}), }); for (const record of page.items) { const bucket = totals.get(record.collection) ?? { count: 0, sumSize: 0 }; bucket.count += 1; bucket.sumSize += record.size; totals.set(record.collection, bucket); } scanned += page.items.length; cursor = page.nextCursor ?? undefined; } while (cursor !== undefined && scanned < COLLECTIONS_SUMMARY_SCAN_LIMIT); return { collections: Array.from(totals.entries()).map(([key, agg]) => ({ key, ...agg })), }; } /** * Full detail of one stored `MediaRecord`, plus a URL per generated conversion (signed, or this * console's proxy — same `objectUrls` strategy as {@link object}). Only conversions carrying a * concrete `{disk, path}` artifact get a link — a metadata-only conversion (probe results with no * output file) has nothing to preview. */ async mediaRecord(id) { const record = await this.manager.store.find(id); if (!record) throw new DashboardError(`unknown media record: ${id}`, 404); const linkable = Object.entries(record.conversions).filter((entry) => typeof entry[1].disk === 'string' && typeof entry[1].path === 'string'); const variants = await Promise.all(linkable.map(async ([name, conversion]) => { const disk = this.manager.storage.disk(conversion.disk); const url = await this.objectUrl(disk, conversion.disk, conversion.path); return { name, url }; })); return { record: toMediaEntry(record), variants }; } /** One stored `MediaRecord` as the console lists it, without signing any URL. */ async mediaEntry(id) { const record = await this.manager.store.find(id); if (!record) throw new DashboardError(`unknown media record: ${id}`, 404); return toMediaEntry(record); } /** Deletes a stored `MediaRecord`. Actions-gated (does not touch its underlying disk object). */ async deleteMediaRecord(id) { this.assertActions(); const record = await this.manager.store.find(id); if (!record) throw new DashboardError(`unknown media record: ${id}`, 404); await this.manager.store.delete(id); } /** Server-side copy (same disk) or streamed cross-disk copy. Original is kept. */ async copy(body) { await this.transfer(body, false); } /** Server-side move (same disk) or streamed cross-disk move. Source is deleted after transfer. */ async move(body) { await this.transfer(body, true); } /** Batched delete of many keys on one disk. */ async remove(body) { this.assertActions(); if (body.keys.length === 0) return; for (const key of body.keys) this.assertKey(key); const disk = this.extended(body.disk); await disk.deleteMany(body.keys); } /** * Writes an object from a byte stream (a browser upload) — the console's convenience upload, * distinct from the resumable TUS path. The caller supplies the full key (prefix + filename). * Bounded at {@link MAX_UPLOAD_BYTES} so a runaway upload can't exhaust the process heap. Actions-gated. */ async putObject(diskName, key, body, contentType) { this.assertActions(); if (!key) throw new DashboardError('key is required', 400); this.assertKey(key); const disk = this.manager.storage.disk(diskName); const chunks = []; let total = 0; for await (const chunk of body) { const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk); total += buffer.length; if (total > MAX_UPLOAD_BYTES) { throw new DashboardError(`upload exceeds the ${MAX_UPLOAD_BYTES / (1024 * 1024)} MB console limit`, 413); } chunks.push(buffer); } // A plain Uint8Array VIEW of the concatenated bytes — not a second copy of the upload. await disk.put(key, uint8View(Buffer.concat(chunks)), contentType ? { contentType } : {}); } /** * Create a "folder" — a zero-byte marker object at `<prefix>/`, which S3-style delimiter listing * surfaces as a navigable folder. Normalizes to exactly one trailing slash (so the key never * double-slashes); rejects an empty prefix. Actions-gated. */ async createFolder(body) { this.assertActions(); const disk = this.extended(body.disk); const normalized = normalizeFolder(body.prefix); if (normalized === '') throw new DashboardError('folder name is required', 400); this.assertKey(`${normalized}/`); await disk.put(`${normalized}/`, new Uint8Array(0)); } /** * Recursively delete a "folder": every object under `<prefix>/` (nested included) plus the * zero-byte marker itself. Sweeps with an EMPTY delimiter so the driver lists keys FLAT — the * default `/` delimiter would roll nested keys up into folder prefixes and the sweep would miss * them, deleting only direct children. Paginates until the disk reports no more, then deletes the * marker explicitly (its key equals the sweep prefix, which `list` filters out). Actions-gated. */ async deleteFolder(body) { this.assertActions(); const disk = this.extended(body.disk); const normalized = normalizeFolder(body.prefix); if (normalized === '') throw new DashboardError('folder is required', 400); this.assertKey(`${normalized}/`); const sweepPrefix = `${normalized}/`; let cursor; do { const result = await disk.list(sweepPrefix, { delimiter: '', limit: FOLDER_SWEEP_LIMIT, ...(cursor !== undefined ? { cursor } : {}), }); if (result.files.length > 0) await disk.deleteMany(result.files.map((entry) => entry.key)); cursor = result.cursor; } while (cursor !== undefined); await disk.delete(sweepPrefix); } /** Copy a whole "folder" (every object under `<from>/`, nested included) to `<to>/`, same or cross disk. */ async copyFolder(body) { await this.transferFolder('copy', body); } /** Move a whole "folder" (every object under `<from>/`, nested included) to `<to>/`, same or cross disk. */ async moveFolder(body) { await this.transferFolder('move', body); } async transfer(body, remove) { this.assertActions(); if (!body.from || !body.to) throw new DashboardError('from and to are required', 400); this.assertKey(body.from); this.assertKey(body.to); const toDisk = body.toDisk ?? body.disk; if (toDisk === body.disk && body.from === body.to) throw new DashboardError('source and destination are identical', 400); await this.transferKey(remove ? 'move' : 'copy', body.disk, body.from, toDisk, body.to); } /** * Relocate a single object, same disk or across disks. Same-disk uses the driver's native * `copy`/`move` (server-side, no bytes through the app). Cross-disk has no driver primitive, so the * object is streamed through the app (get→put, capped at {@link MAX_TRANSFER_BYTES}); a `move` then * deletes the source. Callers gate actions. */ async transferKey(op, fromDisk, from, toDisk, to) { const source = this.extended(fromDisk); if (toDisk === fromDisk) { if (op === 'move') await source.move(from, to); else await source.copy(from, to); return; } const dest = this.manager.storage.disk(toDisk); const stat = await source.stat(from); if (stat.size > MAX_TRANSFER_BYTES) { throw new DashboardError('object too large for cross-disk transfer', 413); } const bytes = await source.getBytes(from); await dest.put(to, bytes, { ...(stat.contentType !== undefined ? { contentType: stat.contentType } : {}), }); if (op === 'move') await source.deleteMany([from]); } /** * Shared engine for {@link copyFolder}/{@link moveFolder}. Same flat-listing sweep as * {@link deleteFolder}; each key is relocated via {@link transferKey} (so cross-disk works * transparently), preserving its path relative to the source. On the SAME disk, rejects a * destination inside the source (which would recurse forever); across disks that can't happen. The * destination folder marker is written, and for a move the source marker removed, so both listings * stay consistent. Actions-gated. */ async transferFolder(op, body) { this.assertActions(); const source = this.extended(body.disk); const toDisk = body.toDisk ?? body.disk; const dest = this.extended(toDisk); const fromNormalized = normalizeFolder(body.from); const toNormalized = normalizeFolder(body.to); if (fromNormalized === '') throw new DashboardError('source folder is required', 400); if (toNormalized === '') throw new DashboardError('destination folder is required', 400); this.assertKey(`${fromNormalized}/`); this.assertKey(`${toNormalized}/`); const fromPrefix = `${fromNormalized}/`; const toPrefix = `${toNormalized}/`; if (toDisk === body.disk && toPrefix.startsWith(fromPrefix)) { throw new DashboardError(`cannot ${op} a folder into itself`, 400); } let cursor; do { const result = await source.list(fromPrefix, { delimiter: '', limit: FOLDER_SWEEP_LIMIT, ...(cursor !== undefined ? { cursor } : {}), }); for (const entry of result.files) { const destKey = `${toPrefix}${entry.key.slice(fromPrefix.length)}`; await this.transferKey(op, body.disk, entry.key, toDisk, destKey); } cursor = result.cursor; } while (cursor !== undefined); // The marker's key equals the sweep prefix, so the sweep skipped it: write the destination // marker, and for a move drop the source one. Idempotent if there was no marker. await dest.put(toPrefix, new Uint8Array(0)); if (op === 'move') await source.delete(fromPrefix); } extended(diskName) { const disk = this.manager.storage.disk(diskName); if (!isExtendedDisk(disk)) { throw new DashboardError(`disk "${diskName}" does not support listing/object operations`, 400); } return disk; } /** A key outside the viewer's tenant prefixes reads as missing (tenancy, confined viewer). */ assertKey(key) { const prefixes = this.options.keyPrefixes; if (prefixes === undefined) return; const normalized = key.replace(/^\/+/, ''); if (!prefixes.some((prefix) => normalized.startsWith(prefix))) { throw new DashboardError('not found', 404); } } /** * For a confined viewer, a listing ABOVE their tenant prefixes (the root, `t/`) shows just the * folders that lead to them; `undefined` when the prefix needs a real listing. */ syntheticFolders(prefix) { const prefixes = this.options.keyPrefixes; if (prefixes === undefined) return undefined; const normalized = prefix.replace(/^\/+/, ''); if (prefixes.some((p) => normalized.startsWith(p))) return undefined; const empty = { folders: [], files: [], nextCursor: null, prevCursor: null, hasNext: false, hasPrev: false, }; // Nothing in scope (no organization, or a pick outside the grant): an empty root, like the // record listings. if (prefixes.length === 0) { if (normalized === '') return empty; throw new DashboardError('not found', 404); } const below = prefixes.filter((p) => p.startsWith(normalized)); if (below.length === 0) throw new DashboardError('not found', 404); const folders = Array.from(new Set(below.map((p) => normalized + p.slice(normalized.length).split('/')[0] + '/'))).map((folder) => ({ name: folderName(folder), prefix: folder })); return { folders, files: [], nextCursor: null, prevCursor: null, hasNext: false, hasPrev: false, }; } assertActions() { if (!this.options.actions) throw new DashboardError('actions are disabled on this console', 403); } } /** `photos/2024/` → `2024`; `` → ``. */ function folderName(prefix) { const trimmed = prefix.replace(/\/+$/, ''); const idx = trimmed.lastIndexOf('/'); return idx === -1 ? trimmed : trimmed.slice(idx + 1); } /** Project a stored `MediaRecord` to the console's `MediaEntry` (dates → ISO, conversions → names). */ function toMediaEntry(record) { return { id: record.id, ownerType: record.ownerType, ownerId: record.ownerId, collection: record.collection, name: record.name, fileName: record.fileName, mimeType: record.mimeType, sizeBytes: record.size, disk: record.disk, path: record.path, conversions: Object.keys(record.conversions), createdAt: record.createdAt.toISOString(), updatedAt: record.updatedAt.toISOString(), ...(record.tenantId !== undefined ? { tenantId: record.tenantId } : {}), }; } function toUploadInfo(session) { const size = session.size ?? null; const percent = size && size > 0 ? Math.min(100, Math.round((session.offset / size) * 100)) : null; return { id: session.id, disk: session.disk, key: session.key, offset: session.offset, size, percent, parts: session.parts, multipart: session.multipartUploadId !== undefined, ...(session.createdAt ? { createdAt: session.createdAt.toISOString() } : {}), ...(session.tenantId !== undefined ? { tenantId: session.tenantId } : {}), }; } /** Fallback `stat` for non-extended disks via `getMetaData`. */ async function metaAsStat(disk, key) { const meta = await disk.getMetaData(key); return { size: meta.contentLength, contentType: meta.contentType, lastModified: meta.lastModified, }; } //# sourceMappingURL=service.js.map