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

82 lines • 3.63 kB
import { allTenants, tenantPathPrefix } from '../agora/tenancy.js'; import { tenantScopedStore } from '../tenant_scoped_store.js'; import { DashboardError } from './service.js'; /** Resolve the console scope from the `authorize` grant, the viewer's tenant and their pick. */ export function resolveConsoleTenantScope(input) { const authorized = input.granted !== undefined ? input.granted : input.own ? [input.own.id] : 'none'; const selected = input.selected?.trim() || undefined; const pick = selected === '*' ? undefined : selected; const base = { authorized, ...(input.granted === undefined && input.own ? { own: input.own } : {}), }; if (authorized === 'none') { return { ...base, selected: undefined, ids: [], forged: pick !== undefined }; } if (pick === undefined) { return { ...base, selected: undefined, ids: authorized === '*' ? undefined : [...authorized], forged: false, }; } if (authorized === '*' || authorized.includes(pick)) { return { ...base, selected: pick, ids: [pick], forged: false }; } return { ...base, selected: pick, ids: [], forged: true }; } /** The ids of a console scope as a {@link TenantScope} (`undefined`: unconfined). */ export function tenantScopeOf(ids) { if (ids === undefined) return undefined; return { tenants: ids.filter((id) => id !== null), includeNull: ids.includes(null), }; } /** * The storage prefixes a confined viewer may browse on the disks: `t/<tenantId>/` per tenant in * scope (where tenancy stores new files). `undefined` when the viewer is not confined. */ export function consoleKeyPrefixes(ids) { if (ids === undefined) return undefined; return ids.filter((id) => id !== null).map(tenantPathPrefix); } /** * The console's view of the media manager with tenancy on, confined to `ids` (`undefined`: every * tenant): the store only sees those tenants' records (another tenant's reads as missing), and the * upload list only their sessions. Upload-session reads run across tenants first (the console's * own scope replaces the request's tenant), then keep the scope's. */ export function scopedManager(manager, ids) { const scope = tenantScopeOf(ids); const visible = (session) => ids === undefined || ids.includes(session.tenantId ?? null); const resumable = manager.resumable; const sessions = async () => allTenants(() => resumable.list(ids !== undefined ? { tenantIds: ids } : {})); return { storage: manager.storage, hasResumable: manager.hasResumable, store: scope === undefined ? manager.store : tenantScopedStore(manager.store, { scope: () => scope }), get resumable() { return { list: async (filter) => (await allTenants(() => resumable.list({ ...filter, ...(ids !== undefined ? { tenantIds: ids } : {}) }))).filter(visible), abort: async (id) => { if (!(await sessions()).some((s) => s.id === id && visible(s))) { throw new DashboardError(`unknown upload: ${id}`, 404); } return allTenants(() => resumable.abort(id)); }, listParts: async (id) => { if (!(await sessions()).some((s) => s.id === id && visible(s))) return []; return allTenants(() => resumable.listParts(id)); }, }; }, }; } //# sourceMappingURL=tenant_scope.js.map