@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 • 4.99 kB
JavaScript
/**
* Lucid-backed {@link UploadSessionStore}. Persists resumable upload sessions in
* `media_upload_sessions` and, for multipart-capable disks, the per-part ETags in
* `media_upload_parts`, via `@adonisjs/lucid`'s query builder (Knex) — so it works across
* SQLite / Postgres / MySQL. JSON `metadata` is stored as TEXT and timestamps as epoch-ms integers,
* keeping the schema portable, exactly like {@link LucidMediaStore}. Publish the migration with
* `node ace configure @adonis-agora/media`.
*/
export class LucidUploadSessionStore {
db;
table;
partsTable;
connectionName;
tenantColumn;
constructor(db, options = {}) {
this.db = db;
this.table = options.table ?? 'media_upload_sessions';
this.partsTable = options.partsTable ?? 'media_upload_parts';
this.connectionName = options.connectionName;
this.tenantColumn = options.tenantColumn === true;
}
client() {
return this.connectionName ? this.db.connection(this.connectionName) : this.db.connection();
}
async create(session) {
const stored = { ...session, createdAt: session.createdAt ?? new Date() };
await this.client()
.table(this.table)
.insert(toRow(stored, this.tenantColumn))
.onConflict('id')
.merge();
return { ...stored };
}
async get(id) {
const row = (await this.client().from(this.table).where('id', id).first());
return row ? fromRow(row) : null;
}
async update(session) {
await this.client()
.table(this.table)
.insert(toRow(session, this.tenantColumn))
.onConflict('id')
.merge();
return { ...session };
}
async delete(id) {
await this.client().from(this.table).where('id', id).delete();
await this.client().from(this.partsTable).where('session_id', id).delete();
}
async addPart(id, part) {
// Upsert by (session_id, part_number) so a retried part overwrites rather than duplicates.
await this.client()
.table(this.partsTable)
.insert({ session_id: id, part_number: part.partNumber, etag: part.etag })
.onConflict(['session_id', 'part_number'])
.merge();
}
async listParts(id) {
const rows = (await this.client()
.from(this.partsTable)
.where('session_id', id)
.orderBy('part_number', 'asc'));
return rows.map((r) => ({ partNumber: Number(r.part_number), etag: r.etag }));
}
async list(filter = {}) {
let q = this.client().from(this.table);
if (filter.disk !== undefined)
q = q.where('disk', filter.disk);
if (filter.keyPrefix !== undefined)
q = q.where('key', 'like', `${filter.keyPrefix}%`);
if (this.tenantColumn && filter.tenantIds !== undefined) {
const ids = filter.tenantIds.filter((id) => id !== null);
const withNull = filter.tenantIds.includes(null);
q = q.where((builder) => {
if (ids.length === 0 && !withNull)
builder.whereRaw('1 = 0');
if (ids.length > 0)
builder.whereIn('tenant_id', ids);
if (withNull)
builder.orWhereNull('tenant_id');
});
}
const rows = (await q.orderBy('created_at', 'asc'));
return rows.map(fromRow);
}
}
function toRow(session, tenantColumn) {
return {
...(tenantColumn ? { tenant_id: session.tenantId ?? null } : {}),
id: session.id,
disk: session.disk,
key: session.key,
content_type: session.contentType ?? null,
size: session.size ?? null,
offset: session.offset,
parts: session.parts,
multipart_upload_id: session.multipartUploadId ?? null,
metadata: session.metadata ? JSON.stringify(session.metadata) : null,
created_at: (session.createdAt ?? new Date()).getTime(),
expires_at: session.expiresAt ? session.expiresAt.getTime() : null,
};
}
function fromRow(row) {
const session = {
id: row.id,
disk: row.disk,
key: row.key,
contentType: row.content_type ?? undefined,
size: row.size === null ? undefined : Number(row.size),
offset: Number(row.offset),
parts: Number(row.parts),
createdAt: new Date(Number(row.created_at)),
};
if (row.tenant_id !== undefined)
session.tenantId = row.tenant_id;
if (row.multipart_upload_id)
session.multipartUploadId = row.multipart_upload_id;
if (row.expires_at !== null)
session.expiresAt = new Date(Number(row.expires_at));
if (row.metadata) {
try {
session.metadata = JSON.parse(row.metadata);
}
catch {
// ignore malformed metadata
}
}
return session;
}
//# sourceMappingURL=lucid.js.map