Files
glenloch-media/src/db/videos.ts
T
bhursey f7a0c8618f
CI / build (push) Has been cancelled
feat(media): primary media storage, growth goals, retention and delivery tools
Event photos and videos are now stored only on the NFS/local share named in
Settings -> Media storage. Uploads, photo editing, video processing and the
per-event filing step refuse to run until that share is configured and
writable, so nothing is silently written inside the container. Video
originals, renditions, posters and sidecars share one folder on the share.

Also adds volunteer growth goals with a mentor queue, an admin retention and
storage sweep with a cron entry point, cached gallery thumbnails with
per-folder ZIP downloads, a server-rendered booth-sheet PDF, and in-app
confirm/release for every upcoming assignment.

Service reminders now ignore the one-off event reminder rule, which had been
queueing an extra bogus reminder for each published service.

Generated with Codebuff 🤖
Co-Authored-By: Codebuff <noreply@codebuff.com>
2026-11-20 23:21:34 +00:00

280 lines
7.1 KiB
TypeScript

/**
* Event video queries and access checks.
*
* Video bytes are never stored in Postgres: `originalStoredName`,
* `webStoredName` and `posterStoredName` all point into the video's folder on
* the configured media storage share. Every access check reuses the event access
* model, so a video is private unless its visibility says otherwise.
*/
import { and, asc, desc, eq, inArray } from "drizzle-orm";
import { db } from "@/db";
import {
eventVideos,
events,
type EventVideoStatus,
type EventVideoVisibility,
} from "@/db/schema";
import { getEventAccess, type EventSummaryRow } from "@/db/events";
import type { SystemRole } from "@/auth/roles";
import { uuidv7 } from "@/lib/uuid";
export type EventVideoRow = typeof eventVideos.$inferSelect;
export interface EventVideoWithEvent extends EventVideoRow {
eventTitle: string;
eventStartsAt: Date;
}
export interface EventVideoAccess {
video: EventVideoRow;
event: EventSummaryRow;
canManage: boolean;
canContribute: boolean;
}
export interface EventVideoPatch {
title?: string;
description?: string | null;
visibility?: EventVideoVisibility;
durationMs?: number | null;
width?: number | null;
height?: number | null;
trimStartMs?: number;
trimEndMs?: number | null;
webStoredName?: string | null;
posterStoredName?: string | null;
status?: EventVideoStatus;
errorMessage?: string | null;
processedAt?: Date | null;
updatedAt?: Date;
}
function withEvent(row: {
video: EventVideoRow;
eventTitle: string;
eventStartsAt: Date;
}): EventVideoWithEvent {
return {
...row.video,
eventTitle: row.eventTitle,
eventStartsAt: row.eventStartsAt,
};
}
export async function listEventVideos(
eventId: string,
): Promise<EventVideoRow[]> {
return db
.select()
.from(eventVideos)
.where(eq(eventVideos.eventId, eventId))
.orderBy(desc(eventVideos.createdAt));
}
export async function listEventVideosForEvents(
eventIds: readonly string[],
): Promise<EventVideoWithEvent[]> {
if (eventIds.length === 0) return [];
const rows = await db
.select({
video: eventVideos,
eventTitle: events.title,
eventStartsAt: events.startsAt,
})
.from(eventVideos)
.innerJoin(events, eq(events.id, eventVideos.eventId))
.where(inArray(eventVideos.eventId, [...eventIds]))
.orderBy(desc(eventVideos.createdAt));
return rows.map(withEvent);
}
export async function getEventVideoRow(
videoId: string,
): Promise<EventVideoRow | null> {
const [row] = await db
.select()
.from(eventVideos)
.where(eq(eventVideos.id, videoId))
.limit(1);
return row ?? null;
}
export async function getEventVideoAccess(
videoId: string,
userId: string,
role: SystemRole | null | undefined,
): Promise<EventVideoAccess | null> {
const [row] = await db
.select({ video: eventVideos, eventId: events.id })
.from(eventVideos)
.innerJoin(events, eq(events.id, eventVideos.eventId))
.where(eq(eventVideos.id, videoId))
.limit(1);
if (!row) return null;
const access = await getEventAccess(row.video.eventId, userId, role);
if (!access) return null;
return {
video: row.video,
event: access.event,
canManage: access.canManage,
canContribute: access.canContribute,
};
}
/** Ready, public/unlisted video with its event; used by watch and embed routes. */
export async function getPublicEventVideo(
videoId: string,
): Promise<EventVideoWithEvent | null> {
const [row] = await db
.select({
video: eventVideos,
eventTitle: events.title,
eventStartsAt: events.startsAt,
})
.from(eventVideos)
.innerJoin(events, eq(events.id, eventVideos.eventId))
.where(
and(
eq(eventVideos.id, videoId),
eq(eventVideos.status, "ready"),
inArray(eventVideos.visibility, ["public", "unlisted"]),
),
)
.limit(1);
return row ? withEvent(row) : null;
}
export async function findEventVideoByChecksum(
eventId: string,
checksumSha256: string,
): Promise<EventVideoRow | null> {
const [row] = await db
.select()
.from(eventVideos)
.where(
and(
eq(eventVideos.eventId, eventId),
eq(eventVideos.checksumSha256, checksumSha256),
),
)
.limit(1);
return row ?? null;
}
export async function addEventVideo(input: {
id?: string;
eventId: string;
uploaderId: string;
title: string;
description: string | null;
filename: string;
mimeType: string;
sizeBytes: number;
checksumSha256: string;
originalStoredName: string;
archiveFolder: string;
visibility: EventVideoVisibility;
}): Promise<EventVideoRow> {
const [row] = await db
.insert(eventVideos)
.values({
// The upload route reserves this id for the NFS path, so reuse it here.
id: input.id ?? uuidv7(),
eventId: input.eventId,
uploaderId: input.uploaderId,
title: input.title,
description: input.description,
filename: input.filename,
mimeType: input.mimeType,
sizeBytes: input.sizeBytes,
checksumSha256: input.checksumSha256,
originalStoredName: input.originalStoredName,
archiveFolder: input.archiveFolder,
visibility: input.visibility,
status: "processing",
trimStartMs: 0,
trimEndMs: null,
createdAt: new Date(),
updatedAt: new Date(),
})
.returning();
return row;
}
export async function updateEventVideo(
videoId: string,
patch: EventVideoPatch,
): Promise<EventVideoRow | null> {
const [row] = await db
.update(eventVideos)
.set({ ...patch, updatedAt: patch.updatedAt ?? new Date() })
.where(eq(eventVideos.id, videoId))
.returning();
return row ?? null;
}
export async function markVideoProcessing(
videoId: string,
): Promise<EventVideoRow | null> {
return updateEventVideo(videoId, {
status: "processing",
errorMessage: null,
});
}
export async function markVideoReady(input: {
videoId: string;
durationMs: number;
width: number;
height: number;
trimStartMs: number;
trimEndMs: number;
webStoredName: string;
posterStoredName: string;
}): Promise<EventVideoRow | null> {
return updateEventVideo(input.videoId, {
status: "ready",
durationMs: input.durationMs,
width: input.width,
height: input.height,
trimStartMs: input.trimStartMs,
trimEndMs: input.trimEndMs,
webStoredName: input.webStoredName,
posterStoredName: input.posterStoredName,
errorMessage: null,
processedAt: new Date(),
});
}
export async function markVideoFailed(
videoId: string,
message: string,
): Promise<EventVideoRow | null> {
return updateEventVideo(videoId, {
status: "failed",
errorMessage: message.slice(0, 1000),
});
}
export async function deleteEventVideo(
videoId: string,
): Promise<EventVideoRow | null> {
const [row] = await db
.delete(eventVideos)
.where(eq(eventVideos.id, videoId))
.returning();
return row ?? null;
}
export async function listVideosForEventOrdered(
eventId: string,
): Promise<EventVideoRow[]> {
return db
.select()
.from(eventVideos)
.where(eq(eventVideos.eventId, eventId))
.orderBy(asc(eventVideos.createdAt));
}