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.81 kB
import { Transform } from 'node:stream'; import { withTenant } from '../agora/tenancy.js'; import { ACCOUNT_DEFAULTS, accountCollections, } from './extension.js'; import { verifyUploadTicket } from './upload_ticket.js'; /** * `POST <accountConsole.path>/uploads`: stores one file for the account console's upload * component. The request carries the ticket the page issued (owner, collection, tenant, expiry, * HMAC-signed) in `x-media-upload-ticket`, the file name in `x-file-name` and the bytes as the body. * Everything a collection enforces on attach (MIME whitelist, content check) still applies. */ export async function handleAccountUpload(request, deps) { const ticket = verifyUploadTicket(deps.secret, request.header('x-media-upload-ticket'), (deps.now ?? Date.now)()); if (!ticket) return { status: 401, body: { error: 'invalid or expired upload ticket' } }; const collection = accountCollections(deps.options).find((c) => c.name === ticket.collection); if (!collection || collection.upload === false) { return { status: 403, body: { error: 'uploads are not allowed in this collection' } }; } const max = deps.options.maxUploadBytes ?? ACCOUNT_DEFAULTS.maxUploadBytes; const length = Number(request.header('content-length')); if (!Number.isFinite(length) || length <= 0) { return { status: 411, body: { error: 'content-length is required' } }; } if (length > max) return { status: 413, body: { error: 'file too large' } }; let fileName = 'file'; try { fileName = decodeURIComponent(request.header('x-file-name') ?? '') || 'file'; } catch { return { status: 400, body: { error: 'invalid x-file-name' } }; } const mimeType = (request.header('content-type') ?? 'application/octet-stream').split(';')[0]; const media = await deps.media(); // The declared length is only trusted as far as the bytes go: more (or fewer) bytes than // declared, or more than the limit, fail the upload, so the stored size is the bytes stored. let received = 0; const sizeError = (message) => Object.assign(new Error(message), { code: 'E_MEDIA_UPLOAD_SIZE' }); const counted = request.body.pipe(new Transform({ transform(chunk, _encoding, done) { received += chunk.length; if (received > length || received > max) done(sizeError('file larger than declared')); else done(null, chunk); }, flush(done) { if (received !== length) done(sizeError('file shorter than declared')); else done(); }, })); request.body.on('error', (error) => counted.destroy(error)); const attach = () => media.library.attach({ ownerType: ticket.ownerType, ownerId: ticket.ownerId, collection: ticket.collection, fileName: fileName.slice(0, 255), mimeType: mimeType.trim() || 'application/octet-stream', contents: counted, size: length, ...(ticket.tenantId !== undefined ? { tenantId: ticket.tenantId } : {}), }); try { const record = media.tenancy.enabled ? await withTenant(ticket.tenantId ? { id: ticket.tenantId, kind: ticket.tenantKind ?? 'organization' } : null, attach) : await attach(); return { status: 201, body: { id: record.id } }; } catch (error) { const code = error.code; const status = typeof code === 'string' && code.startsWith('E_MEDIA_') ? 422 : 500; return { status, body: { error: status === 422 && error instanceof Error ? error.message : 'upload failed' }, }; } } //# sourceMappingURL=upload_endpoint.js.map