Compare commits

...

7 Commits

Author SHA1 Message Date
twotalesanimation ec125261e4 Slim the Docker image and clean up after every deploy
Deploy Vault CMS / deploy (push) Successful in 1m15s
- Build Next.js as a standalone server and copy only that into the
  runtime image, instead of the whole node_modules tree
- Mark three dynamic file reads with turbopackIgnore so the standalone
  trace does not pull in the entire project
- Deploy workflow prunes unused images and build cache older than a day
  at the start and end of every run, including failed ones, and logs
  disk usage before building

Failed builds were leaving their layers behind until the server's disk
filled, which broke the deploy, the database and Gitea sign-in.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 20:43:34 +02:00
twotalesanimation 3083a48b5f Re-trigger deploy after freeing disk space on the server
Deploy Vault CMS / deploy (push) Successful in 2m31s
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 18:04:17 +02:00
twotalesanimation 9a73058eb4 Upgrade Next.js to 16.4.6 and show locks on latest videos
Deploy Vault CMS / deploy (push) Failing after 4m1s
- Upgrade next and eslint-config-next from 16.3.5 to 16.4.6. The older
  version has critical advisories, which made the Docker build's
  `pnpm audit --audit-level critical` step fail and blocked deploys.
- /api/videos/latest now reports whether the viewer can open each video;
  the dashboard shows a lock and the reason instead of opening the player.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 17:56:19 +02:00
twotalesanimation c98b5c5692 Load watch progress in one request on dashboard and player
Deploy Vault CMS / deploy (push) Failing after 5s
Add GET /api/progress/all, which returns the signed-in user's progress
and merged watch segments for every video (or one playlist) using two
queries.

- Dashboard: replaces two sequential requests per video with a single
  request on load
- Player sidebar: replaces one request per playlist video with a single
  request per playlist

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 17:48:35 +02:00
twotalesanimation a834caa511 Fix watch tracking, video unlocking and access-control gaps
Deploy Vault CMS / deploy (push) Failing after 38s
Watch progress and unlocking:
- Player samples the playhead and reports the ranges actually played,
  so skipped sections earn no credit and early plays are not lost
- Server merges ranges into compact segment rows; progress never regresses
- Single 80% unlock rule enforced server-side (lib/watch-progress.ts);
  the Unlock button and automatic unlock now agree
- Lock state and progress bar update live from each progress save
- Playlist is derived from the video, so unlocks work for shared
  playlists and for links from watch history and liked videos
- First/previous video decided by playlist position, not index numbers

Access control:
- Video API and HLS/MP4 routes require a signed-in viewer who is
  enrolled and has the video unlocked; thumbnails require sign-in
- Enrolment sync reads course codes from the allow-list, not the request
- /api/admin/meta is admin-only
- Account status, role and levels are re-checked from the database
  during a session, so deactivated students lose access within a minute

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
2026-11-11 16:58:07 +02:00
twotalesanimation 08b6e58b8e Patch React2Shell RCE and harden auth, uploads and containers
Deploy Vault CMS / deploy (push) Successful in 2m23s
- Upgrade next 16.0.3 -> 16.3.5 and react/react-dom 19.2.0 -> 19.3.0
  (CVE-2025-55182 / CVE-2025-66478 unauthenticated RCE), next-auth -> 4.24.15
- Require admin role on /api/users, /api/courses and /api/enrollments,
  which were unauthenticated
- Replace ALLOWED_ADMINS checks that allowed any signed-in user when the
  variable was empty with the role-based requireAdmin
- Sanitize upload filenames, restrict thumbnail extensions, store edited
  thumbnails in UPLOADS_DIR, tighten traversal checks in file routes
- Dockerfile: Node 22, pinned pnpm, non-root user, critical audit gate
- Compose: read-only rootfs, dropped caps, no-new-privileges, Postgres
  password from env
- Exclude .env from Docker build context; add security headers
- Deploy with git reset --hard instead of git pull
- Remove stale package-lock.json

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-15 11:52:33 +02:00
twotalesanimation a863a5cbf3 Serve uploaded MP4s at /uploads/videos with Range support
Video records store url = /uploads/videos/<id>.mp4, but nothing served
that path: Next.js only serves static files from public/, so the
player's MP4 fallback 404'd and videos were unplayable until HLS
transcoding finished. (Thumbnails hit the same problem historically —
see scripts/update-thumbnail-urls.ts.)

Add a route handler at the exact stored URL so no DB migration is
needed. Unlike the HLS route it streams from disk via createReadStream
instead of buffering the file, and implements HTTP Range requests
(206/416, open-ended and suffix ranges) — browsers require ranges for
<video> seeking, and files can be multi-GB.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-08-01 17:41:37 +02:00
38 changed files with 2310 additions and 11275 deletions
+5 -2
View File
@@ -2,8 +2,11 @@
.gitignore
node_modules
.next
.env.local
.env.*.local
# Never bake secrets into image layers; they are passed in at runtime
.env
.env.*
.gitea
*.tsbuildinfo
.DS_Store
*.log
npm-debug.log*
+18 -2
View File
@@ -21,11 +21,27 @@ jobs:
cd /opt/owicms-src
# Force the checkout to match the remote exactly, discarding any
# local modifications (a plain pull can keep or merge tampered files)
git fetch origin
git reset --hard origin/master
cd /opt/owicms
docker compose up -d --build
# Clean up afterwards whether or not the deploy succeeds; otherwise
# every failed build leaves its layers behind until the disk fills.
# Build cache used in the last day is kept, so routine deploys stay fast.
cleanup() {
docker image prune -f || true
docker builder prune -af --filter until=24h || true
}
trap cleanup EXIT
docker image prune -f
# Start from a clean slate too, in case an earlier run was killed.
cleanup
echo "Disk before build:"
df -h /opt || true
docker system df || true
docker compose up -d --build
+26 -12
View File
@@ -1,18 +1,23 @@
# Multi-stage Dockerfile for Next.js CMS
# Stage 1: Build
FROM node:20-alpine AS builder
FROM node:22-alpine AS builder
WORKDIR /app
# Copy package files
COPY package.json pnpm-lock.yaml ./
# Install dependencies
RUN npm install -g pnpm && pnpm install --frozen-lockfile
# Install dependencies (pnpm pinned so builds are reproducible)
RUN npm install -g pnpm@10.22.0 && pnpm install --frozen-lockfile
# Copy source code
COPY . .
# Refuse to build when a production dependency has a known critical
# vulnerability. Runs after COPY so it isn't skipped by the layer cache on
# deploys that only change source code.
RUN pnpm audit --prod --audit-level critical
ARG DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy"
ENV DATABASE_URL=$DATABASE_URL
@@ -21,24 +26,33 @@ ENV DATABASE_URL=$DATABASE_URL
RUN pnpm exec prisma generate
# Build Next.js application
RUN npm run build
RUN pnpm run build
# Stage 2: Production Runtime
FROM node:20-alpine
FROM node:22-alpine
WORKDIR /app
# Install dumb-init for proper signal handling
RUN apk add --no-cache dumb-init
# Copy from builder: node_modules and .next build output
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/.next ./.next
# Copy the standalone server (only the files it actually uses, including the
# traced parts of node_modules), plus static assets, which it does not include.
# Files stay root-owned so the app user cannot rewrite its own code; only the
# Next.js cache directory is writable.
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
COPY --from=builder /app/package.json ./package.json
RUN mkdir -p .next/cache && chown node:node .next/cache
# Set production environment
# Set production environment. HOSTNAME makes the server listen on all
# interfaces rather than only the container's hostname.
ENV NODE_ENV=production
ENV HOSTNAME=0.0.0.0
ENV PORT=3000
# Run as the unprivileged "node" user (uid 1000) instead of root
USER node
# Expose port 3000
EXPOSE 3000
@@ -46,5 +60,5 @@ EXPOSE 3000
# Use dumb-init to handle signals properly
ENTRYPOINT ["dumb-init", "--"]
# Start Next.js production server
CMD ["npm", "run", "start"]
# Start the standalone Next.js server
CMD ["node", "server.js"]
+5 -2
View File
@@ -1,7 +1,7 @@
# ==============================
# Build stage
# ==============================
FROM node:20-alpine AS builder
FROM node:22-alpine AS builder
WORKDIR /app
@@ -25,7 +25,7 @@ RUN npm run build
# ==============================
# Runtime stage
# ==============================
FROM node:20-alpine
FROM node:22-alpine
# Install FFmpeg
RUN apk add --no-cache ffmpeg
@@ -48,5 +48,8 @@ COPY prisma ./prisma
ENV NODE_ENV=production
ENV UPLOADS_DIR=/uploads
# Run as the unprivileged "node" user (uid 1000) instead of root
USER node
# The container runs once and exits
CMD ["node", "dist/index.js"]
+2 -10
View File
@@ -1,19 +1,11 @@
// app/api/admin/create-course/route.ts
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
async function checkAdmin() {
const session = await getServerSession(authOptions);
if (!session?.user?.email) throw { status: 401, message: 'Unauthorized' };
const allowed = (process.env.ALLOWED_ADMINS || '').split(',').map(s=>s.trim()).filter(Boolean);
if (allowed.length && !allowed.includes(session.user.email)) throw { status: 403, message: 'Forbidden' };
}
import { requireAdmin } from '@/lib/admin-check';
export async function POST(req: Request) {
try {
await checkAdmin();
await requireAdmin();
const body = await req.json();
const { title, code } = body;
if (!title) return NextResponse.json({ error: 'missing title' }, { status: 400 });
+2 -10
View File
@@ -1,19 +1,11 @@
// app/api/admin/create-playlist/route.ts
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
async function checkAdmin() {
const session = await getServerSession(authOptions);
if (!session?.user?.email) throw { status: 401, message: 'Unauthorized' };
const allowed = (process.env.ALLOWED_ADMINS || '').split(',').map(s=>s.trim()).filter(Boolean);
if (allowed.length && !allowed.includes(session.user.email)) throw { status: 403, message: 'Forbidden' };
}
import { requireAdmin } from '@/lib/admin-check';
export async function POST(req: Request) {
try {
await checkAdmin();
await requireAdmin();
const { title, courseId, additionalCourseIds } = await req.json();
if (!title || !courseId) return NextResponse.json({ error: 'missing fields' }, { status: 400 });
+4 -12
View File
@@ -2,20 +2,12 @@
// Assign or remove a playlist from additional courses
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
async function checkAdmin() {
const session = await getServerSession(authOptions);
if (!session?.user?.email) throw { status: 401, message: 'Unauthorized' };
const allowed = (process.env.ALLOWED_ADMINS || '').split(',').map(s=>s.trim()).filter(Boolean);
if (allowed.length && !allowed.includes(session.user.email)) throw { status: 403, message: 'Forbidden' };
}
import { requireAdmin } from '@/lib/admin-check';
// GET: Get all courses a playlist is assigned to
export async function GET(req: Request) {
try {
await checkAdmin();
await requireAdmin();
const { searchParams } = new URL(req.url);
const playlistId = searchParams.get('playlistId');
@@ -49,7 +41,7 @@ export async function GET(req: Request) {
// POST: Assign playlist to an additional course
export async function POST(req: Request) {
try {
await checkAdmin();
await requireAdmin();
const { playlistId, courseId } = await req.json();
if (!playlistId || !courseId) return NextResponse.json({ error: 'missing fields' }, { status: 400 });
@@ -76,7 +68,7 @@ export async function POST(req: Request) {
// DELETE: Remove playlist from a course
export async function DELETE(req: Request) {
try {
await checkAdmin();
await requireAdmin();
const { searchParams } = new URL(req.url);
const playlistId = searchParams.get('playlistId');
const courseId = searchParams.get('courseId');
+4 -5
View File
@@ -1,13 +1,12 @@
// app/api/admin/meta/route.ts
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { adminGuard } from '@/lib/admin-check';
export async function GET() {
// Basic protection: ensure signed-in user is admin. Modify as needed.
// If you want stricter, get session from context or check roles.
// Here we skip session check to let dev access; but production should check.
const denied = await adminGuard();
if (denied) return denied;
try {
const courses = await prisma.course.findMany({ orderBy: { title: 'asc' } });
const playlists = await prisma.playlist.findMany({ orderBy: { title: 'asc' } });
+25 -6
View File
@@ -5,6 +5,16 @@ import { authOptions } from '@/lib/auth-options';
import { prisma } from '@/lib/prisma';
import { writeFile, mkdir } from 'fs/promises';
import { join } from 'path';
import { randomUUID } from 'crypto';
const UPLOADS_DIR = process.env.UPLOADS_DIR || '/uploads';
const THUMBNAIL_EXTS: Record<string, string> = {
'image/jpeg': '.jpg',
'image/png': '.png',
'image/webp': '.webp',
'image/gif': '.gif',
};
export async function PATCH(req: Request) {
try {
@@ -93,18 +103,27 @@ export async function POST(req: Request) {
// Handle thumbnail upload if provided
if (thumbnail && thumbnail.size > 0) {
// extension comes from an allowlist, never from client-supplied strings
const ext = THUMBNAIL_EXTS[thumbnail.type];
if (!ext) {
return NextResponse.json(
{ error: 'Thumbnail must be a JPEG, PNG, WebP or GIF image' },
{ status: 400 }
);
}
const bytes = await thumbnail.arrayBuffer();
const buffer = Buffer.from(bytes);
// Save to public/thumbnails
const uploadDir = join(process.cwd(), 'public', 'thumbnails');
// Save with the upload route's thumbnails; the app directory itself is
// read-only in production
const uploadDir = join(UPLOADS_DIR, 'thumbnails');
await mkdir(uploadDir, { recursive: true });
const filename = `${videoId}-${Date.now()}.${thumbnail.type.split('/')[1] || 'jpg'}`;
const filepath = join(uploadDir, filename);
await writeFile(filepath, buffer);
const filename = `${Date.now()}-${randomUUID()}${ext}`;
await writeFile(join(uploadDir, filename), buffer);
updateData.thumbnail = `/thumbnails/${filename}`;
updateData.thumbnail = `/api/thumbnails/${filename}`;
}
if (Object.keys(updateData).length === 0 && !thumbnail && restrictedCourseIds === null) {
+2 -15
View File
@@ -4,8 +4,7 @@ import fs from "fs";
import path from "path";
import { spawn } from "child_process";
import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth-options";
import { requireAdmin } from "@/lib/admin-check";
const UPLOADS_DIR = process.env.UPLOADS_DIR || "/uploads";
@@ -56,23 +55,11 @@ async function getVideoDurationFromFile(filePath: string): Promise<number | null
});
}
async function checkAdmin() {
const session = await getServerSession(authOptions);
if (!session?.user?.email) throw { status: 401, message: "Unauthorized" };
const allowed = (process.env.ALLOWED_ADMINS || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (allowed.length && !allowed.includes(session.user.email))
throw { status: 403, message: "Forbidden" };
return session;
}
export async function POST(req: Request) {
try {
console.log('[Upload/Finalize] Request received');
await checkAdmin();
await requireAdmin();
const body = await req.json();
const { videoId } = body;
+8 -18
View File
@@ -7,8 +7,7 @@ import { IncomingMessage } from "http";
import { spawn } from "child_process";
import formidable, { Fields, Files, File as FormidableFile } from "formidable";
import { prisma } from "@/lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "@/lib/auth-options";
import { requireAdmin } from "@/lib/admin-check";
// For 2GB uploads over Tailscale (~10-50 Mbps), need 30-45 minutes
export const maxDuration = 2700; // 45 minutes for very large uploads over slow connections
@@ -20,6 +19,8 @@ const UPLOADS_DIR = process.env.UPLOADS_DIR || "/uploads";
// post-parse rename is a cheap atomic move instead of a multi-GB copy
const TMP_DIR = path.join(UPLOADS_DIR, "tmp");
const THUMBNAIL_EXTS = new Set([".jpg", ".jpeg", ".png", ".webp", ".gif"]);
// Helper to extract video duration using FFprobe
async function getVideoDurationFromFile(filePath: string): Promise<number | null> {
return new Promise((resolve) => {
@@ -134,18 +135,6 @@ async function moveFile(src: string, dest: string) {
await fs.promises.chmod(dest, 0o644);
}
async function checkAdmin() {
const session = await getServerSession(authOptions);
if (!session?.user?.email) throw { status: 401, message: "Unauthorized" };
const allowed = (process.env.ALLOWED_ADMINS || "")
.split(",")
.map((s) => s.trim())
.filter(Boolean);
if (allowed.length && !allowed.includes(session.user.email))
throw { status: 403, message: "Forbidden" };
return session;
}
export async function POST(req: Request) {
// track saved paths so we can cleanup on error
let savedVideoDest: string | undefined;
@@ -156,7 +145,7 @@ export async function POST(req: Request) {
try {
console.log('[Upload] Request received, content-type:', req.headers.get("content-type"));
const session = await checkAdmin();
const session = await requireAdmin();
console.log('[Upload] Admin check passed for:', session.user?.email);
@@ -184,8 +173,8 @@ export async function POST(req: Request) {
if (thumbFile) {
try {
const originalThumbName = thumbFile.originalFilename || path.basename(thumbFile.filepath);
const tExt = path.extname(originalThumbName) || ".jpg";
const thumbFilename = `${Date.now()}-thumb${tExt}`;
const tExt = path.extname(originalThumbName).toLowerCase();
const thumbFilename = `${Date.now()}-thumb${THUMBNAIL_EXTS.has(tExt) ? tExt : ".jpg"}`;
const thumbDest = path.join(UPLOADS_DIR, "thumbnails", thumbFilename);
await moveFile(thumbFile.filepath, thumbDest);
thumbnailUrl = `/api/thumbnails/${thumbFilename}`;
@@ -198,7 +187,8 @@ export async function POST(req: Request) {
if (!isManualCopy) {
if (!videoFile) return NextResponse.json({ error: "no file" }, { status: 400 });
const originalFilename = videoFile.originalFilename || path.basename(videoFile.filepath);
// basename: the client-supplied name must not be able to contain "../"
const originalFilename = path.basename(videoFile.originalFilename || videoFile.filepath);
const filename = `${Date.now()}-${originalFilename}`;
const dest = path.join(UPLOADS_DIR, "videos", filename);
+5 -1
View File
@@ -1,8 +1,12 @@
// app/api/users/route.ts
// app/api/courses/route.ts
import { NextResponse } from "next/server";
import { prisma } from "../../../lib/prisma";
import { adminGuard } from "@/lib/admin-check";
export async function GET() {
const denied = await adminGuard();
if (denied) return denied;
const courses = await prisma.course.findMany({ select: { id: true, title: true, code: true } });
return NextResponse.json(courses);
}
+11 -3
View File
@@ -1,13 +1,15 @@
// /app/api/enrollments/route.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
import { prisma } from '@/lib/prisma';
import { adminGuard } from '@/lib/admin-check';
// --- GET --------------------------------------------------------------------
export async function GET() {
const denied = await adminGuard();
if (denied) return denied;
try {
const enrollments = await prisma.enrollment.findMany({
include: {
@@ -26,6 +28,9 @@ export async function GET() {
// --- POST --------------------------------------------------------------------
export async function POST(req: NextRequest) {
const denied = await adminGuard();
if (denied) return denied;
try {
const body = await req.json();
@@ -74,6 +79,9 @@ export async function POST(req: NextRequest) {
// --- DELETE --------------------------------------------------------------------
export async function DELETE(req: NextRequest) {
const denied = await adminGuard();
if (denied) return denied;
try {
const body = await req.json();
const id = body?.id;
+54 -98
View File
@@ -1,122 +1,78 @@
// app/api/enrollments/sync/route.ts
import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { prisma } from '@/lib/prisma';
import { normalizeEmail } from '@/lib/normalize-email';
export async function POST(req: NextRequest) {
console.log('🔥 [SYNC-ENROLLMENTS] POST endpoint called');
console.log('🔥 [SYNC-ENROLLMENTS] Request method:', req.method);
console.log('🔥 [SYNC-ENROLLMENTS] Request URL:', req.url);
/**
* Enrols the signed-in user in the courses listed for them on the allow-list.
* The course codes are read from the database; anything the browser sends is
* ignored, so a user cannot enrol themselves in a course they were not given.
*/
export async function POST() {
try {
const session = await getServerSession(authOptions);
console.log('🔥 [SYNC-ENROLLMENTS] Session check:', !!session?.user?.email);
console.log('🔥 [SYNC-ENROLLMENTS] Session user:', session?.user);
if (!session?.user?.email) {
console.log('🔥 [SYNC-ENROLLMENTS] No session or email, returning 401');
const email = normalizeEmail(session?.user?.email);
if (!email) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const userEmail = session.user.email;
console.log('🔥 [SYNC-ENROLLMENTS] About to parse request body...');
const body = await req.json();
console.log('🔥 [SYNC-ENROLLMENTS] Raw body received:', JSON.stringify(body, null, 2));
const levels: string[] = body?.levels || [];
console.log('🔥 [SYNC-ENROLLMENTS] Extracted levels:', levels);
console.log('🔥 [SYNC-ENROLLMENTS] Levels type check - isArray:', Array.isArray(levels));
console.log('🔥 [SYNC-ENROLLMENTS] Levels length:', levels.length);
console.log('🔥 [SYNC-ENROLLMENTS] User:', userEmail);
console.log('🔥 [SYNC-ENROLLMENTS] Received levels:', levels);
if (!Array.isArray(levels) || levels.length === 0) {
console.log('🔥 [SYNC-ENROLLMENTS] VALIDATION FAILED!');
console.log('🔥 [SYNC-ENROLLMENTS] levels isArray:', Array.isArray(levels));
console.log('🔥 [SYNC-ENROLLMENTS] levels length:', levels?.length);
console.log('🔥 [SYNC-ENROLLMENTS] levels value:', levels);
console.log('🔥 [SYNC-ENROLLMENTS] Returning 400 - Levels array required');
return NextResponse.json(
{ error: 'Levels array required' },
{ status: 400 }
);
}
// Get user
const user = await prisma.user.findUnique({
where: { email: userEmail },
const user = await prisma.user.findFirst({
where: { email: { equals: email, mode: 'insensitive' } },
select: { id: true },
});
if (!user) {
console.log('[SYNC-ENROLLMENTS] User not found in database:', userEmail);
return NextResponse.json({ error: 'User not found' }, { status: 404 });
}
console.log('[SYNC-ENROLLMENTS] Found user:', user.id, user.email);
const allowedStudent = await prisma.allowedStudent.findFirst({
where: { email: { equals: email, mode: 'insensitive' } },
select: { levels: true, active: true },
});
const createdEnrollments = [];
const levels = allowedStudent?.active
? allowedStudent.levels
.split(',')
.map((level) => level.trim())
.filter((level) => level.length > 0)
: [];
// For each course level, find the course and create enrollment
for (const level of levels) {
try {
console.log('[SYNC-ENROLLMENTS] Looking for course with code:', level);
const course = await prisma.course.findUnique({
where: { code: level },
});
if (!course) {
console.warn(`[SYNC-ENROLLMENTS] Course code not found: ${level}`);
continue;
}
console.log('[SYNC-ENROLLMENTS] Found course:', course.id, course.code, course.title);
// Check if enrollment already exists
const existing = await prisma.enrollment.findFirst({
where: { userId: user.id, courseId: course.id },
});
if (existing) {
console.log('[SYNC-ENROLLMENTS] Enrollment already exists:', existing.id);
} else {
const enrollment = await prisma.enrollment.create({
data: {
userId: user.id,
courseId: course.id,
},
});
console.log('[SYNC-ENROLLMENTS] Created new enrollment:', enrollment.id);
createdEnrollments.push({
courseCode: level,
courseId: course.id,
enrollmentId: enrollment.id,
});
}
} catch (err) {
console.error(`[SYNC-ENROLLMENTS] Failed to create enrollment for level ${level}:`, err);
}
if (levels.length === 0) {
return NextResponse.json({ message: 'Enrollments synced', created: [] });
}
console.log('[SYNC-ENROLLMENTS] Summary - Created enrollments:', createdEnrollments.length);
console.log('[SYNC-ENROLLMENTS] Details:', createdEnrollments);
const courses = await prisma.course.findMany({
where: { code: { in: levels } },
select: { id: true, code: true },
});
return NextResponse.json(
{
message: 'Enrollments synced',
created: createdEnrollments,
},
{ status: 200 }
);
const missing = levels.filter((level) => !courses.some((c) => c.code === level));
if (missing.length > 0) {
console.warn(`[SYNC-ENROLLMENTS] Course codes not found: ${missing.join(', ')}`);
}
const created = [];
for (const course of courses) {
const existing = await prisma.enrollment.findUnique({
where: { userId_courseId: { userId: user.id, courseId: course.id } },
select: { id: true },
});
if (existing) continue;
const enrollment = await prisma.enrollment.create({
data: { userId: user.id, courseId: course.id },
});
created.push({
courseCode: course.code,
courseId: course.id,
enrollmentId: enrollment.id,
});
}
return NextResponse.json({ message: 'Enrollments synced', created });
} catch (err) {
console.error('🔥 [SYNC-ENROLLMENTS] FATAL ERROR:', err);
console.error('🔥 [SYNC-ENROLLMENTS] Error stack:', err instanceof Error ? err.stack : 'No stack');
return NextResponse.json(
{ error: 'Failed to sync enrollments' },
{ status: 500 }
);
console.error('[SYNC-ENROLLMENTS] error:', err);
return NextResponse.json({ error: 'Failed to sync enrollments' }, { status: 500 });
}
}
+17 -5
View File
@@ -3,6 +3,7 @@ import { NextResponse } from "next/server";
import { prisma } from "../../../../lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "../../../../lib/auth-options";
import { filterAccessibleVideos } from "../../../../lib/watch-progress";
export async function GET(req: Request, context: any) {
try {
@@ -20,6 +21,7 @@ export async function GET(req: Request, context: any) {
const user = await prisma.user.findUnique({
where: { email: session.user.email },
include: { enrollments: { select: { courseId: true } } },
});
if (!user) {
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
@@ -37,21 +39,31 @@ export async function GET(req: Request, context: any) {
image: true,
},
},
videoCourses: { select: { courseId: true, exclusive: true } },
},
orderBy: { index: "asc" },
},
course: true,
courses: { select: { courseId: true } },
},
});
if (!playlist) return NextResponse.json({ error: "Not found" }, { status: 404 });
const enrolled = await prisma.enrollment.findUnique({
where: { userId_courseId: { userId: user.id, courseId: playlist.courseId } },
});
if (!enrolled) return NextResponse.json({ error: "Forbidden" }, { status: 403 });
// Enrolment in the primary course or any course the playlist is shared with.
const userCourseIds = user.enrollments.map((e) => e.courseId);
const playlistCourseIds = [playlist.courseId, ...playlist.courses.map((c) => c.courseId)];
if (!playlistCourseIds.some((courseId) => userCourseIds.includes(courseId))) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
return NextResponse.json({ playlist });
// Hide videos reserved for courses this user is not in, as /api/playlists does.
const visible = {
...playlist,
videos: filterAccessibleVideos(playlist.videos, userCourseIds),
};
return NextResponse.json({ playlist: visible });
} catch (err: any) {
console.error("GET /api/playlists/[id] error:", err);
return NextResponse.json({ error: "Server error" }, { status: 500 });
+65
View File
@@ -0,0 +1,65 @@
// app/api/progress/all/route.ts
// Everything the signed-in user has watched, in one response, so pages do not
// have to ask about each video separately.
import { NextResponse } from 'next/server';
import { prisma } from '@/lib/prisma';
import { getViewer } from '@/lib/video-access';
import { mergeRanges } from '@/lib/watch-progress';
type Segment = { startSec: number; endSec: number; watchedAt?: Date };
/**
* GET /api/progress/all – all of the user's progress
* GET /api/progress/all?playlistId – only videos in that playlist
*
* Returns { progress: { [videoId]: { percent, watchedSec, segments } } }.
* Videos the user has never played are simply absent.
*/
export async function GET(req: Request) {
try {
const viewer = await getViewer();
if (!viewer) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
const playlistId = new URL(req.url).searchParams.get('playlistId');
const where = {
userId: viewer.id,
...(playlistId ? { video: { playlistId } } : {}),
};
const [progressRows, segmentRows] = await Promise.all([
prisma.videoProgress.findMany({
where,
select: { videoId: true, percent: true, watchedSec: true },
}),
prisma.videoWatchSegment.findMany({
where,
select: { videoId: true, startSec: true, endSec: true, watchedAt: true },
}),
]);
const segmentsByVideo = new Map<string, Segment[]>();
for (const { videoId, ...segment } of segmentRows) {
const list = segmentsByVideo.get(videoId);
if (list) list.push(segment);
else segmentsByVideo.set(videoId, [segment]);
}
const progress: Record<
string,
{ percent: number; watchedSec: number; segments: Segment[] }
> = {};
for (const row of progressRows) {
progress[row.videoId] = {
percent: row.percent,
watchedSec: row.watchedSec,
// Older data can hold many overlapping rows per video; send the merged view.
segments: mergeRanges(segmentsByVideo.get(row.videoId) ?? []),
};
}
return NextResponse.json({ progress });
} catch (err: any) {
console.error('progress/all GET error', err);
return NextResponse.json({ error: err?.message ?? 'server error' }, { status: 500 });
}
}
+103 -131
View File
@@ -3,6 +3,14 @@ import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { prisma } from '@/lib/prisma';
import {
UNLOCK_THRESHOLD_PERCENT,
mergeRanges,
sanitizeRanges,
totalSeconds,
unlockNextVideo,
type SecRange,
} from '@/lib/watch-progress';
export async function GET(req: Request) {
try {
@@ -47,36 +55,6 @@ export async function GET(req: Request) {
}
}
// Helper function to calculate unique watched seconds from segments
function calculateWatchedSeconds(segments: Array<{ startSec: number; endSec: number }>): number {
if (segments.length === 0) return 0;
// Sort and merge overlapping ranges
const sorted = segments.sort((a, b) => a.startSec - b.startSec);
const merged: Array<[number, number]> = [];
for (const seg of sorted) {
if (merged.length === 0) {
merged.push([seg.startSec, seg.endSec]);
} else {
const last = merged[merged.length - 1];
if (seg.startSec <= last[1] + 0.5) {
// Overlapping or adjacent, merge
last[1] = Math.max(last[1], seg.endSec);
} else {
// Gap, new range
merged.push([seg.startSec, seg.endSec]);
}
}
}
let total = 0;
for (const [start, end] of merged) {
total += Math.max(0, end - start);
}
return Math.round(total);
}
export async function POST(req: Request) {
try {
const session = await getServerSession(authOptions);
@@ -86,137 +64,131 @@ export async function POST(req: Request) {
const userEmail = session.user.email;
const body = await req.json().catch(() => ({}));
const { videoId, playlistId, watchedSec, lastPos, duration } = body ?? {};
const { videoId, watchedSec, lastPos, duration } = body ?? {};
if (!videoId)
if (!videoId || typeof videoId !== 'string')
return NextResponse.json({ error: 'videoId required' }, { status: 400 });
if (!duration || Number(duration) <= 0)
return NextResponse.json({ error: 'duration required' }, { status: 400 });
const watched = Number(watchedSec ?? 0);
const lastPosition = Number(lastPos ?? 0);
const dur = Number(duration);
// look up user id
const user = await prisma.user.findUnique({ where: { email: userEmail } });
const user = await prisma.user.findUnique({
where: { email: userEmail },
select: { id: true },
});
if (!user)
return NextResponse.json({ error: 'user not found' }, { status: 404 });
// Validate that the video exists
const video = await prisma.video.findUnique({ where: { id: videoId } });
const video = await prisma.video.findUnique({
where: { id: videoId },
select: { id: true, playlistId: true, durationSec: true },
});
if (!video) {
// Log at debug level since this is expected when users have old cached references
console.debug(`[PROGRESS] Video not found: ${videoId} (likely deleted video with cached browser reference)`);
return NextResponse.json({ error: 'video not found' }, { status: 404 });
}
// Ensure VideoProgress exists or create it
let videoProgress = await prisma.videoProgress.findUnique({
where: { userId_videoId: { userId: user.id, videoId } },
});
// The stored duration wins; the player's value is only a fallback for
// videos uploaded without one.
const dur =
video.durationSec && video.durationSec > 0 ? video.durationSec : Number(duration);
if (!Number.isFinite(dur) || dur <= 0)
return NextResponse.json({ error: 'duration required' }, { status: 400 });
if (!videoProgress) {
videoProgress = await prisma.videoProgress.create({
data: {
const lastPosition = Math.max(0, Math.floor(Number(lastPos ?? 0)) || 0);
// Current players send the ranges actually played. Tabs still running the
// previous player only send a total, from which one range is inferred.
let incoming: SecRange[];
if (Array.isArray(body?.ranges)) {
incoming = sanitizeRanges(body.ranges, dur);
} else {
const watched = Number(watchedSec ?? 0);
incoming = sanitizeRanges(
watched > 0 ? [[lastPosition - watched, lastPosition]] : [],
dur
);
}
const where = { userId_videoId: { userId: user.id, videoId } };
const { progress, segments } = await prisma.$transaction(async (tx) => {
const existingProgress = await tx.videoProgress.upsert({
where,
update: {},
create: {
userId: user.id,
videoId,
watchedSec: 0,
lastPos: 0,
percent: 0,
durationSec: dur,
durationSec: Math.round(dur),
},
});
}
// Record the watch segment if watched seconds > 0
let newSegment = null;
if (watched > 0) {
newSegment = await prisma.videoWatchSegment.create({
data: {
userId: user.id,
videoId,
startSec: Math.max(0, lastPosition - watched),
endSec: lastPosition,
},
const existing = await tx.videoWatchSegment.findMany({
where: { userId: user.id, videoId },
select: { startSec: true, endSec: true, watchedAt: true },
orderBy: { startSec: 'asc' },
});
}
// Fetch all segments for this user+video to recalculate totals
const allSegments = await prisma.videoWatchSegment.findMany({
where: { userId: user.id, videoId },
select: { startSec: true, endSec: true },
orderBy: { createdAt: 'asc' },
});
// Calculate total unique watched seconds
const totalWatchedSec = calculateWatchedSeconds(allSegments);
// Calculate completion percentage
const ratio = dur > 0 ? totalWatchedSec / dur : 0;
const percentInt = Math.min(100, Math.round(ratio * 100));
const completed = percentInt >= 80; // Changed from 90 to 80 for unlock threshold
// Update VideoProgress with recalculated values
const upserted = await prisma.videoProgress.update({
where: { userId_videoId: { userId: user.id, videoId } },
data: {
watchedSec: totalWatchedSec,
lastPos: Math.max(videoProgress.lastPos ?? 0, lastPosition),
percent: percentInt,
durationSec: dur,
completed,
updatedAt: new Date(),
},
});
// If 80% watched, unlock next video in playlist for this user
let unlockedNext = null;
if (completed && playlistId) {
// find current video
const current = await prisma.video.findUnique({ where: { id: videoId } });
if (current && current.playlistId === playlistId) {
// Find next video that is NOT instant access and NOT globally locked
// Skip any instant access videos in the sequence
const nextVideos = await prisma.video.findMany({
where: {
playlistId,
index: { gt: current.index },
locked: false,
instantAccess: false,
},
orderBy: { index: 'asc' },
take: 1,
// Keep one row per distinct watched stretch instead of one per update.
const now = new Date();
const merged = mergeRanges([
...existing,
...incoming.map((r) => ({ ...r, watchedAt: now })),
]);
const unchanged =
merged.length === existing.length &&
merged.every(
(m, i) => m.startSec === existing[i].startSec && m.endSec === existing[i].endSec
);
if (!unchanged) {
await tx.videoWatchSegment.deleteMany({ where: { userId: user.id, videoId } });
await tx.videoWatchSegment.createMany({
data: merged.map((m) => ({
userId: user.id,
videoId,
startSec: m.startSec,
endSec: m.endSec,
watchedAt: m.watchedAt ?? now,
})),
});
if (nextVideos.length > 0) {
const next = nextVideos[0];
// Create a VideoUnlock record for this user (per-user unlock tracking)
const unlock = await prisma.videoUnlock.upsert({
where: { userId_videoId: { userId: user.id, videoId: next.id } },
update: {}, // if already exists, do nothing
create: { userId: user.id, videoId: next.id },
});
unlockedNext = { id: next.id, title: next.title };
}
}
}
return NextResponse.json({ ok: true, progress: upserted, unlockedNext, segment: newSegment });
// Progress never goes backwards, even if segment rows were pruned.
const mergedSec = totalSeconds(merged);
const percent = Math.max(
existingProgress.percent,
Math.min(100, Math.round((mergedSec / dur) * 100))
);
const completed =
existingProgress.completed || percent >= UNLOCK_THRESHOLD_PERCENT;
const updated = await tx.videoProgress.update({
where,
data: {
watchedSec: Math.max(existingProgress.watchedSec, mergedSec),
lastPos: Math.max(existingProgress.lastPos ?? 0, lastPosition),
percent,
durationSec: Math.round(dur),
completed,
updatedAt: now,
},
});
return { progress: updated, segments: merged };
});
// Completing a video unlocks the next one in its playlist for this user.
const unlockedNext = progress.completed
? await unlockNextVideo(user.id, video)
: null;
return NextResponse.json({ ok: true, progress, unlockedNext, segments });
} catch (err: any) {
console.error('progress error', err);
// Handle foreign key constraint violations
if (err.code === 'P2003') {
const constraint = err.meta?.constraint_name;
if (constraint?.includes('videoId')) {
console.error(`[PROGRESS] Foreign key violation - invalid videoId: ${err.meta}`);
return NextResponse.json(
{ error: 'Invalid video reference' },
{ status: 400 }
);
}
}
return NextResponse.json(
{ error: err?.message ?? 'server error' },
{ status: 500 }
+13 -6
View File
@@ -4,6 +4,7 @@
import { NextResponse } from 'next/server';
import * as fs from 'fs';
import * as path from 'path';
import { getViewer } from '@/lib/video-access';
// Path to thumbnails directory (should match UPLOADS_DIR/thumbnails)
const THUMBNAILS_DIR = path.join(process.env.UPLOADS_DIR || '/uploads', 'thumbnails');
@@ -23,11 +24,16 @@ export async function GET(
);
}
// Thumbnails show course content, so they need a signed-in user.
if (!(await getViewer())) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
// Construct the file path and validate it
const requestedPath = path.join(THUMBNAILS_DIR, ...pathSegments);
// Security: prevent directory traversal attacks
if (!requestedPath.startsWith(THUMBNAILS_DIR)) {
if (!requestedPath.startsWith(THUMBNAILS_DIR + path.sep)) {
return NextResponse.json(
{ error: 'Invalid request' },
{ status: 403 }
@@ -44,7 +50,7 @@ export async function GET(
}
// Read the file
const fileContent = fs.readFileSync(requestedPath);
const fileContent = await fs.promises.readFile(requestedPath);
// Determine content type based on file extension
let contentType = 'application/octet-stream';
@@ -64,8 +70,7 @@ export async function GET(
status: 200,
headers: {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=31536000, immutable', // Cache for 1 year (thumbnails don't change)
'Access-Control-Allow-Origin': '*', // Allow CORS if needed
'Cache-Control': 'private, max-age=31536000, immutable', // Cache for 1 year (thumbnails don't change)
},
});
} catch (error) {
@@ -90,10 +95,12 @@ export async function HEAD(
return new NextResponse(null, { status: 400 });
}
if (!(await getViewer())) return new NextResponse(null, { status: 401 });
const requestedPath = path.join(THUMBNAILS_DIR, ...pathSegments);
// Security: prevent directory traversal attacks
if (!requestedPath.startsWith(THUMBNAILS_DIR)) {
if (!requestedPath.startsWith(THUMBNAILS_DIR + path.sep)) {
return new NextResponse(null, { status: 403 });
}
@@ -107,7 +114,7 @@ export async function HEAD(
status: 200,
headers: {
'Content-Type': 'image/jpeg', // Default for HEAD requests
'Cache-Control': 'public, max-age=31536000, immutable',
'Cache-Control': 'private, max-age=31536000, immutable',
},
});
} catch (error) {
+1 -1
View File
@@ -57,7 +57,7 @@ export async function POST(
// Security: path traversal guard.
for (const p of [tempZipPath, tempDir, finalDir]) {
if (!path.resolve(p).startsWith(path.resolve(HLS_ROOT))) {
if (!path.resolve(/*turbopackIgnore: true*/ p).startsWith(path.resolve(HLS_ROOT))) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
}
+21 -29
View File
@@ -3,6 +3,7 @@ import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { prisma } from '@/lib/prisma';
import { checkUnlockEligibility } from '@/lib/watch-progress';
export async function GET(req: Request) {
try {
@@ -59,40 +60,31 @@ export async function POST(req: Request) {
if (!user)
return NextResponse.json({ error: 'user not found' }, { status: 404 });
// Verify video exists
const video = await prisma.video.findUnique({
where: { id: videoId },
select: { id: true, locked: true, instantAccess: true },
});
if (!video) {
return NextResponse.json({ error: 'video not found' }, { status: 404 });
// Admins can open anything; everyone else must have earned the unlock.
const role = (session.user as any).role;
const isAdmin = role === 'admin' || role === 'superadmin';
if (!isAdmin) {
const check = await checkUnlockEligibility(user.id, videoId);
if (!check.ok) {
return NextResponse.json({ error: check.error }, { status: check.status });
}
if (check.free) {
return NextResponse.json({ message: 'Video does not need unlocking', unlock: null });
}
} else {
const exists = await prisma.video.findUnique({ where: { id: videoId }, select: { id: true } });
if (!exists) {
return NextResponse.json({ error: 'video not found' }, { status: 404 });
}
}
// Check if already unlocked
const existingUnlock = await prisma.videoUnlock.findUnique({
const unlock = await prisma.videoUnlock.upsert({
where: { userId_videoId: { userId: user.id, videoId } },
update: {},
create: { userId: user.id, videoId },
});
if (existingUnlock) {
return NextResponse.json({
message: 'Video already unlocked',
unlock: existingUnlock
});
}
// Create the unlock record
const unlock = await prisma.videoUnlock.create({
data: {
userId: user.id,
videoId: videoId,
},
});
return NextResponse.json({
message: 'Video unlocked successfully',
unlock
});
return NextResponse.json({ message: 'Video unlocked successfully', unlock });
} catch (err: any) {
console.error('user unlocks POST error', err);
+4
View File
@@ -1,9 +1,13 @@
// app/api/users/route.ts
import { NextResponse } from "next/server";
import { prisma } from "../../../lib/prisma";
import { adminGuard } from "@/lib/admin-check";
export async function GET() {
const denied = await adminGuard();
if (denied) return denied;
const users = await prisma.user.findMany({ select: { id: true, name: true, email: true } });
return NextResponse.json(users);
}
+11
View File
@@ -4,6 +4,7 @@ import { prisma } from "../../../../lib/prisma";
import { getServerSession } from "next-auth";
import { authOptions } from "../../../../lib/auth-options";
import { getVideoUrls } from "../../../../lib/video-urls";
import { checkVideoAccess } from "../../../../lib/video-access";
export async function GET(req: Request, context: any) {
try {
@@ -74,6 +75,16 @@ export async function GET(req: Request, context: any) {
return NextResponse.json({ error: "Forbidden" }, { status: 403 });
}
// Sequential locks apply to students even when they follow a direct link.
const role = (session.user as any).role;
const isAdmin = role === "admin" || role === "superadmin";
if (!isAdmin) {
const access = await checkVideoAccess({ id: user.id, isAdmin: false }, id);
if (!access.ok) {
return NextResponse.json({ error: access.error }, { status: access.status });
}
}
// Find the next video (index = current.index + 1)
const next = await prisma.video.findFirst({
where: {
+21 -5
View File
@@ -4,6 +4,7 @@
import { NextResponse } from 'next/server';
import * as fs from 'fs';
import * as path from 'path';
import { checkVideoAccess, getViewer } from '@/lib/video-access';
// Path to HLS directory (should match UPLOADS_DIR/hls)
const HLS_DIR = path.join(process.env.UPLOADS_DIR || '/uploads', 'hls');
@@ -23,11 +24,22 @@ export async function GET(
);
}
// Signed-in viewers only, and only for videos they may watch.
// The first path segment is the video id.
const viewer = await getViewer();
if (!viewer) {
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 });
}
const access = await checkVideoAccess(viewer, pathSegments[0]);
if (!access.ok) {
return NextResponse.json({ error: access.error }, { status: access.status });
}
// Construct the file path and validate it
const requestedPath = path.join(HLS_DIR, ...pathSegments);
// Security: prevent directory traversal attacks
if (!requestedPath.startsWith(HLS_DIR)) {
if (!requestedPath.startsWith(HLS_DIR + path.sep)) {
return NextResponse.json(
{ error: 'Invalid request' },
{ status: 403 }
@@ -44,7 +56,7 @@ export async function GET(
}
// Read the file
const fileContent = fs.readFileSync(requestedPath);
const fileContent = await fs.promises.readFile(requestedPath);
// Determine content type based on file extension
let contentType = 'application/octet-stream';
@@ -62,8 +74,7 @@ export async function GET(
status: 200,
headers: {
'Content-Type': contentType,
'Cache-Control': 'public, max-age=3600', // Cache for 1 hour
'Access-Control-Allow-Origin': '*', // Allow CORS if needed
'Cache-Control': 'private, max-age=3600', // Viewer's browser only, 1 hour
'Accept-Ranges': 'bytes',
},
});
@@ -89,10 +100,15 @@ export async function HEAD(
return new NextResponse(null, { status: 400 });
}
const viewer = await getViewer();
if (!viewer) return new NextResponse(null, { status: 401 });
const access = await checkVideoAccess(viewer, pathSegments[0]);
if (!access.ok) return new NextResponse(null, { status: access.status });
const requestedPath = path.join(HLS_DIR, ...pathSegments);
// Security: prevent directory traversal attacks
if (!requestedPath.startsWith(HLS_DIR)) {
if (!requestedPath.startsWith(HLS_DIR + path.sep)) {
return new NextResponse(null, { status: 403 });
}
+17 -1
View File
@@ -3,6 +3,7 @@ import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/lib/auth-options';
import { prisma } from '@/lib/prisma';
import { checkVideoAccess } from '@/lib/video-access';
export async function GET() {
try {
@@ -70,7 +71,22 @@ export async function GET() {
take: 10,
});
return NextResponse.json({ videos });
// Tell the dashboard which of these the user can actually open, so locked
// ones show a lock instead of leading to a refusal.
const role = (session.user as any).role;
const viewer = { id: user.id, isAdmin: role === 'admin' || role === 'superadmin' };
const withAccess = await Promise.all(
videos.map(async (video) => {
const access = await checkVideoAccess(viewer, video.id, { peek: true });
return {
...video,
accessible: access.ok,
lockReason: access.ok ? null : access.error,
};
})
);
return NextResponse.json({ videos: withAccess });
} catch (err: any) {
console.error('GET /api/videos/latest error', err);
return NextResponse.json(
+28 -56
View File
@@ -25,6 +25,7 @@ import { SiteHeader } from '@/components/site-header';
import { SidebarInset, SidebarProvider } from '@/components/ui/sidebar';
import { Lock } from 'lucide-react';
import Image from 'next/image';
import { toast } from 'sonner';
type VideoItem = {
id: string;
@@ -173,65 +174,27 @@ export default function DashboardClient() {
syncEnrollments();
}, [session]);
// One request for all of this user's progress; videos never played are
// simply absent and render as 0%.
React.useEffect(() => {
let cancelled = false;
const fetchProgress = async () => {
const progressMap: Record<string, VideoProgressData> = {};
// Fetch progress for playlist videos
for (const playlist of playlistsToShow) {
for (const video of playlist.videos || []) {
try {
const [progressRes, segmentsRes] = await Promise.all([
fetch(`/api/progress?videoId=${video.id}`),
fetch(`/api/progress/segments?videoId=${video.id}`),
]);
if (progressRes.ok) {
const progressData = await progressRes.json();
const segmentsData = segmentsRes.ok ? await segmentsRes.json() : { segments: [] };
progressMap[video.id] = {
percent: progressData.percent ?? 0,
watchedSec: progressData.watchedSec ?? 0,
segments: segmentsData.segments ?? [],
};
}
} catch (err) {
console.error(`Failed to fetch progress for video ${video.id}`, err);
progressMap[video.id] = { percent: 0, watchedSec: 0, segments: [] };
}
}
try {
const res = await fetch('/api/progress/all');
if (!res.ok) return;
const data = await res.json();
if (!cancelled) setVideoProgress(data.progress ?? {});
} catch (err) {
console.error('Failed to fetch progress', err);
}
// Fetch progress for latest videos
for (const video of latestVideos) {
try {
const [progressRes, segmentsRes] = await Promise.all([
fetch(`/api/progress?videoId=${video.id}`),
fetch(`/api/progress/segments?videoId=${video.id}`),
]);
if (progressRes.ok) {
const progressData = await progressRes.json();
const segmentsData = segmentsRes.ok ? await segmentsRes.json() : { segments: [] };
progressMap[video.id] = {
percent: progressData.percent ?? 0,
watchedSec: progressData.watchedSec ?? 0,
segments: segmentsData.segments ?? [],
};
}
} catch (err) {
console.error(`Failed to fetch progress for video ${video.id}`, err);
progressMap[video.id] = { percent: 0, watchedSec: 0, segments: [] };
}
}
setVideoProgress(progressMap);
};
if (playlistsToShow.length > 0 || latestVideos.length > 0) {
fetchProgress();
}
}, [playlistsToShow, latestVideos]);
fetchProgress();
return () => {
cancelled = true;
};
}, []);
const handleOpenVideo = (playlistId: string, videoId: string) => {
// navigate to videoplayer page using query params (keeps your current structure)
@@ -286,6 +249,10 @@ export default function DashboardClient() {
<Card
className="@container/card overflow-hidden cursor-pointer"
onClick={() => {
if (v.accessible === false) {
toast.error(v.lockReason ?? 'This video is locked.');
return;
}
if (v.playlist?.id) {
handleOpenVideo(v.playlist.id, v.id);
}
@@ -299,6 +266,11 @@ export default function DashboardClient() {
className="w-full h-full object-cover"
loading="lazy"
/>
{v.accessible === false ? (
<div className="absolute inset-0 bg-black/40 flex items-center justify-center">
<Lock className="w-5 h-5 text-white" />
</div>
) : null}
</div>
<div className="flex gap-2 items-start pt-2">
{v.uploader?.image && (
@@ -400,7 +372,7 @@ export default function DashboardClient() {
handleOpenVideo(pl.id, v.id);
return;
}
if ((v as any).index === 0) {
if (v.id === pl.videos?.[0]?.id) {
handleOpenVideo(pl.id, v.id);
return;
}
@@ -417,7 +389,7 @@ export default function DashboardClient() {
className="w-full h-full object-cover"
loading="lazy"
/>
{(v as any).locked || (!(v as any).instantAccess && (v as any).index !== 0 && !userUnlocks.has(v.id)) ? (
{(v as any).locked || (!(v as any).instantAccess && v.id !== pl.videos?.[0]?.id && !userUnlocks.has(v.id)) ? (
<div className="absolute inset-0 bg-black/40 flex items-center justify-center">
<Lock className="w-5 h-5 text-white" />
</div>
+202
View File
@@ -0,0 +1,202 @@
// app/uploads/videos/[...path]/route.ts
// Serves uploaded MP4s from UPLOADS_DIR/videos at the /uploads/videos/* URLs
// stored in the database. Streams from disk with HTTP Range support — browsers
// require range requests for <video> seeking, and multi-GB files must never be
// buffered in memory.
import { NextResponse } from "next/server";
import fs from "fs";
import path from "path";
import { Readable } from "stream";
import { prisma } from "@/lib/prisma";
import { checkVideoAccess, getViewer } from "@/lib/video-access";
const VIDEOS_DIR = path.join(process.env.UPLOADS_DIR || "/uploads", "videos");
const CONTENT_TYPES: Record<string, string> = {
".mp4": "video/mp4",
".m4v": "video/mp4",
".webm": "video/webm",
".mov": "video/quicktime",
".mkv": "video/x-matroska",
};
function resolveVideoPath(segments: string[]): string | null {
if (segments.length === 0) return null;
const requested = path.resolve(VIDEOS_DIR, ...segments);
// Security: prevent directory traversal attacks
if (!requested.startsWith(path.resolve(VIDEOS_DIR) + path.sep)) return null;
return requested;
}
async function statFile(filePath: string): Promise<fs.Stats | null> {
try {
const stats = await fs.promises.stat(filePath);
return stats.isFile() ? stats : null;
} catch {
return null;
}
}
// Parses "bytes=start-end" | "bytes=start-" | "bytes=-suffix".
// Returns null for absent/invalid headers (caller serves the full file) and
// "unsatisfiable" when the range is outside the file (caller returns 416).
function parseRange(
header: string | null,
size: number
): { start: number; end: number } | "unsatisfiable" | null {
if (!header) return null;
const m = header.match(/^bytes=(\d*)-(\d*)$/);
if (!m || (m[1] === "" && m[2] === "")) return null;
let start: number;
let end: number;
if (m[1] === "") {
// suffix range: last N bytes
const suffix = parseInt(m[2], 10);
if (suffix === 0) return "unsatisfiable";
start = Math.max(0, size - suffix);
end = size - 1;
} else {
start = parseInt(m[1], 10);
end = m[2] === "" ? size - 1 : Math.min(parseInt(m[2], 10), size - 1);
}
if (start >= size || start > end) return "unsatisfiable";
return { start, end };
}
// Maps a requested file to the Video row it belongs to, then applies the same
// access rule as the video API. Files with no Video row are admin-only.
const VIDEO_ID_TTL_MS = 5 * 60_000;
const videoIdByPath = new Map<string, { expires: number; videoId: string | null }>();
async function findVideoId(segments: string[]): Promise<string | null> {
const relative = segments.join("/");
const hit = videoIdByPath.get(relative);
if (hit && hit.expires > Date.now()) return hit.videoId;
const byUrl = await prisma.video.findFirst({
where: { url: `/uploads/videos/${relative}` },
select: { id: true },
});
// Uploads are normally stored as {videoId}.mp4
const byName =
byUrl ??
(await prisma.video.findUnique({
where: { id: path.basename(relative, path.extname(relative)) },
select: { id: true },
}));
const videoId = byName?.id ?? null;
if (videoIdByPath.size >= 5000) videoIdByPath.clear();
videoIdByPath.set(relative, { expires: Date.now() + VIDEO_ID_TTL_MS, videoId });
return videoId;
}
async function authorize(segments: string[]): Promise<{ status: number; error: string } | null> {
const viewer = await getViewer();
if (!viewer) return { status: 401, error: "Unauthorized" };
if (viewer.isAdmin) return null;
const videoId = await findVideoId(segments);
if (!videoId) return { status: 404, error: "Not found" };
const access = await checkVideoAccess(viewer, videoId);
return access.ok ? null : { status: access.status, error: access.error };
}
function baseHeaders(filePath: string, stats: fs.Stats): Record<string, string> {
const ext = path.extname(filePath).toLowerCase();
return {
"Content-Type": CONTENT_TYPES[ext] ?? "application/octet-stream",
"Accept-Ranges": "bytes",
"Cache-Control": "private, max-age=3600",
"Last-Modified": stats.mtime.toUTCString(),
};
}
export async function GET(
request: Request,
context: { params: Promise<{ path?: string[] }> }
) {
try {
const params = await context.params;
const filePath = resolveVideoPath(params?.path || []);
if (!filePath) {
return NextResponse.json({ error: "Invalid request" }, { status: 400 });
}
const denied = await authorize(params?.path || []);
if (denied) {
return NextResponse.json({ error: denied.error }, { status: denied.status });
}
const stats = await statFile(filePath);
if (!stats) {
console.log(`[Videos] File not found: ${filePath}`);
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
const headers = baseHeaders(filePath, stats);
const range = parseRange(request.headers.get("range"), stats.size);
if (range === "unsatisfiable") {
return new NextResponse(null, {
status: 416,
headers: { ...headers, "Content-Range": `bytes */${stats.size}` },
});
}
if (range) {
const stream = fs.createReadStream(/*turbopackIgnore: true*/ filePath, {
start: range.start,
end: range.end,
});
return new NextResponse(Readable.toWeb(stream) as unknown as BodyInit, {
status: 206,
headers: {
...headers,
"Content-Range": `bytes ${range.start}-${range.end}/${stats.size}`,
"Content-Length": String(range.end - range.start + 1),
},
});
}
const stream = fs.createReadStream(/*turbopackIgnore: true*/ filePath);
return new NextResponse(Readable.toWeb(stream) as unknown as BodyInit, {
status: 200,
headers: { ...headers, "Content-Length": String(stats.size) },
});
} catch (error) {
console.error("[Videos] Error serving video file:", error);
return NextResponse.json({ error: "Internal server error" }, { status: 500 });
}
}
export async function HEAD(
request: Request,
context: { params: Promise<{ path?: string[] }> }
) {
try {
const params = await context.params;
const filePath = resolveVideoPath(params?.path || []);
if (!filePath) return new NextResponse(null, { status: 400 });
const denied = await authorize(params?.path || []);
if (denied) return new NextResponse(null, { status: denied.status });
const stats = await statFile(filePath);
if (!stats) return new NextResponse(null, { status: 404 });
return new NextResponse(null, {
status: 200,
headers: {
...baseHeaders(filePath, stats),
"Content-Length": String(stats.size),
},
});
} catch (error) {
console.error("[Videos] Error in HEAD request:", error);
return new NextResponse(null, { status: 500 });
}
}
+267 -363
View File
@@ -27,6 +27,7 @@ import { Button } from '@/components/ui/button';
import { HlsPlayer } from '@/components/hls';
import { useSession } from 'next-auth/react';
import Image from 'next/image';
import { toast } from 'sonner';
type VideoFromApi = {
id: string;
@@ -50,293 +51,286 @@ type VideoFromApi = {
} | null;
};
type ProgressResult = {
progress?: { percent?: number };
segments?: WatchSegment[];
unlockedNext?: { id: string; title: string } | null;
};
type TrackedVideo = { videoId: string; durationSec: number | null };
/**
* Records which parts of a video were actually played and reports them to
* /api/progress.
*
* It samples the <video> element on a timer rather than relying on media
* events, so it keeps working when the element is swapped (one per video) or
* when playback starts before anything else on the page has settled. A sample
* only counts as watched when the playhead moved forward by no more than the
* wall-clock time that passed, which excludes seeks and scrubbing.
*/
export function useWatchTracker(
videoRef: React.RefObject<HTMLVideoElement | null>,
opts: { sendIntervalMs?: number; commitIntervalSec?: number; debug?: boolean } = {}
opts: {
sendIntervalMs?: number;
sampleIntervalMs?: number;
onResult?: (videoId: string, result: ProgressResult) => void;
} = {}
) {
const sendIntervalMs = opts.sendIntervalMs ?? 5000;
const commitIntervalSec = opts.commitIntervalSec ?? 3;
const debug = !!opts.debug;
const sampleIntervalMs = opts.sampleIntervalMs ?? 500;
const rangesRef = React.useRef<Array<[number, number]>>([]);
const lastTimeRef = React.useRef<number | null>(null);
const currentRangeStartRef = React.useRef<number | null>(null);
const sendTimerRef = React.useRef<number | null>(null);
const commitTimerRef = React.useRef<number | null>(null);
// merging helper
function mergeRanges(ranges: Array<[number, number]>) {
if (!ranges.length) return [];
ranges.sort((a, b) => a[0] - b[0]);
const merged: Array<[number, number]> = [];
for (const [s, e] of ranges) {
if (!merged.length) merged.push([s, e]);
else {
const last = merged[merged.length - 1];
if (s <= last[1] + 0.5) last[1] = Math.max(last[1], e);
else merged.push([s, e]);
}
}
return merged;
}
function addRange(s: number, e: number) {
if (e <= s) return;
if (debug) console.debug("[tracker] addRange", s, e);
rangesRef.current.push([s, e]);
rangesRef.current = mergeRanges(rangesRef.current);
}
function getUniqueWatchedSec() {
let sum = 0;
for (const [a, b] of rangesRef.current) sum += Math.max(0, b - a);
return Math.round(sum);
}
// commit the "current" playing segment to ranges (useful while playing)
function commitCurrentRange() {
const el = videoRef.current;
if (!el) return;
const start = currentRangeStartRef.current;
const now = el.currentTime;
if (start === null) return;
// only commit if we've moved forward at least 0.5s to avoid noise
if (now - start >= 0.5) {
addRange(start, now);
// keep currentRangeStart open at 'now' so we continue accumulating
currentRangeStartRef.current = now;
}
}
// send progress to server (same signature you had)
async function sendProgress(args: { videoId: string; playlistId?: string; duration: number; extra?: Record<string, any> }) {
const { videoId, playlistId, duration, extra } = args;
const watchedSec = getUniqueWatchedSec();
const el = videoRef.current;
const lastPos = Math.floor(el?.currentTime ?? 0);
if (!duration || duration <= 0) return null;
if (watchedSec === 0 && (el?.paused ?? true)) return null;
const payload = { videoId, playlistId, watchedSec, lastPos, duration, ...extra };
try {
if (debug) console.debug("[tracker] sendProgress", payload);
const res = await fetch("/api/progress", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload),
});
// If video not found, stop trying to track progress
if (!res.ok) {
if (res.status === 404) {
console.warn(`[tracker] Video ${videoId} not found, stopping progress tracking`);
stopAutoSend(); // Stop automatic progress tracking for deleted video
return null;
}
return null;
}
return res.ok ? await res.json().catch(() => null) : null;
} catch (err) {
if (debug) console.warn("[tracker] sendProgress error", err);
return null;
}
}
// start/stop auto-send
function startAutoSend(args: { videoId: string; playlistId?: string; duration: number }) {
stopAutoSend(); // clear existing
// immediate send (best-effort)
sendProgress(args).catch(() => {});
// periodic send
sendTimerRef.current = window.setInterval(() => {
const watched = getUniqueWatchedSec();
const el = videoRef.current;
const isPlaying = !!(el && !el.paused && !el.ended);
if (watched > 0 || isPlaying) {
sendProgress(args).catch(() => {});
}
}, sendIntervalMs) as unknown as number;
// commit currently playing ranges periodically (so continuous play is captured)
commitTimerRef.current = window.setInterval(() => {
commitCurrentRange();
}, commitIntervalSec * 1000) as unknown as number;
if (debug) console.debug("[tracker] startAutoSend", { sendIntervalMs, commitIntervalSec });
}
function stopAutoSend(opts?: { sendFinal?: boolean; videoId?: string; playlistId?: string; duration?: number }) {
if (sendTimerRef.current) {
window.clearInterval(sendTimerRef.current);
sendTimerRef.current = null;
}
if (commitTimerRef.current) {
window.clearInterval(commitTimerRef.current);
commitTimerRef.current = null;
}
if (opts?.sendFinal && opts.videoId && opts.duration) {
sendProgress({ videoId: opts.videoId, playlistId: opts.playlistId, duration: opts.duration }).catch(() => {});
}
if (debug) console.debug("[tracker] stopAutoSend");
}
// attach listeners to populate rangesRef
const onResultRef = React.useRef(opts.onResult);
React.useEffect(() => {
onResultRef.current = opts.onResult;
});
const activeRef = React.useRef<TrackedVideo | null>(null);
const rangesRef = React.useRef<Array<[number, number]>>([]);
const lastSampleRef = React.useRef<{ el: HTMLVideoElement; time: number; wall: number } | null>(null);
const lastPosRef = React.useRef(0);
const elementDurationRef = React.useRef(0);
const dirtyRef = React.useRef(false);
const reset = React.useCallback((next: TrackedVideo | null) => {
activeRef.current = next;
rangesRef.current = [];
lastSampleRef.current = null;
lastPosRef.current = 0;
elementDurationRef.current = 0;
dirtyRef.current = false;
}, []);
const sample = React.useCallback(() => {
const el = videoRef.current;
if (!el) {
if (debug) console.debug("[tracker] no video element to attach");
if (!activeRef.current || !el || typeof el.currentTime !== 'number') {
lastSampleRef.current = null;
return;
}
currentRangeStartRef.current = null;
lastTimeRef.current = el.currentTime ?? 0;
const prev = lastSampleRef.current;
const now = el.currentTime;
const wall = performance.now();
lastSampleRef.current = { el, time: now, wall };
const onPlay = () => {
currentRangeStartRef.current = el.currentTime;
lastTimeRef.current = el.currentTime;
if (debug) console.debug("[tracker] play", currentRangeStartRef.current);
};
// First sample of a new element: nothing to compare against yet.
if (!prev || prev.el !== el) return;
const onPause = () => {
if (currentRangeStartRef.current !== null) {
addRange(currentRangeStartRef.current, el.currentTime);
currentRangeStartRef.current = null;
}
if (debug) console.debug("[tracker] pause, ranges:", rangesRef.current);
};
lastPosRef.current = now;
if (Number.isFinite(el.duration) && el.duration > 0) {
elementDurationRef.current = el.duration;
}
if (el.seeking) return;
const onTimeUpdate = () => {
const now = el.currentTime;
const last = lastTimeRef.current ?? now;
// detect seek (big jump)
if (Math.abs(now - last) > 2.5) {
if (currentRangeStartRef.current !== null) {
addRange(currentRangeStartRef.current, last);
const delta = now - prev.time;
if (delta <= 0) return; // paused, or jumped backwards
// Real playback cannot outrun the clock (timers may fire late in
// background tabs, hence wall-clock time rather than the interval).
const elapsedSec = (wall - prev.wall) / 1000;
const maxAdvance = elapsedSec * Math.max(1, el.playbackRate || 1) + 0.75;
if (delta > maxAdvance) return; // jumped forwards
const ranges = rangesRef.current;
const last = ranges[ranges.length - 1];
if (last && Math.abs(last[1] - prev.time) < 0.01) {
last[1] = now; // still in the same stretch of playback
} else {
ranges.push([prev.time, now]);
}
dirtyRef.current = true;
}, [videoRef]);
const flush = React.useCallback(async (keepalive = false) => {
const active = activeRef.current;
if (!active || !dirtyRef.current || rangesRef.current.length === 0) return;
const duration = active.durationSec || Math.floor(elementDurationRef.current);
if (!duration || duration <= 0) return; // metadata not loaded yet; try again next tick
// Everything played this session is resent each time; the server merges,
// so a dropped request loses nothing.
const ranges = rangesRef.current.map(([s, e]) => [s, e]);
dirtyRef.current = false;
try {
const res = await fetch('/api/progress', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
keepalive,
body: JSON.stringify({
videoId: active.videoId,
ranges,
lastPos: Math.floor(lastPosRef.current),
duration,
}),
});
const stillActive = activeRef.current?.videoId === active.videoId;
if (!res.ok) {
if (res.status === 404) {
// Video was deleted: stop tracking it.
if (stillActive) reset(null);
} else if (stillActive) {
dirtyRef.current = true;
}
currentRangeStartRef.current = now;
if (debug) console.debug("[tracker] seek detected, start:", now);
return;
}
lastTimeRef.current = now;
// we do not addRange here to avoid flooding; commit timer handles mid-play commits
};
const onEnded = () => {
if (currentRangeStartRef.current !== null) {
addRange(currentRangeStartRef.current, el.duration ?? lastTimeRef.current ?? 0);
currentRangeStartRef.current = null;
}
if (debug) console.debug("[tracker] ended, ranges:", rangesRef.current);
};
const json = await res.json().catch(() => null);
if (json) onResultRef.current?.(active.videoId, json);
} catch {
if (activeRef.current?.videoId === active.videoId) dirtyRef.current = true;
}
}, [reset]);
el.addEventListener("play", onPlay);
el.addEventListener("pause", onPause);
el.addEventListener("timeupdate", onTimeUpdate);
el.addEventListener("ended", onEnded);
/** Begin tracking a video. Anything recorded for a previous video is dropped. */
const start = React.useCallback((video: TrackedVideo) => reset(video), [reset]);
/** Send what is left for the current video, then stop tracking it. */
const stop = React.useCallback(() => {
void flush(true);
reset(null);
}, [flush, reset]);
React.useEffect(() => {
const sampleTimer = window.setInterval(sample, sampleIntervalMs);
const sendTimer = window.setInterval(() => void flush(), sendIntervalMs);
// Leaving or hiding the page: get the latest progress out while we can.
const flushNow = () => {
sample();
void flush(true);
};
const onVisibility = () => {
if (document.visibilityState === 'hidden') flushNow();
};
document.addEventListener('visibilitychange', onVisibility);
window.addEventListener('pagehide', flushNow);
return () => {
el.removeEventListener("play", onPlay);
el.removeEventListener("pause", onPause);
el.removeEventListener("timeupdate", onTimeUpdate);
el.removeEventListener("ended", onEnded);
stopAutoSend();
window.clearInterval(sampleTimer);
window.clearInterval(sendTimer);
document.removeEventListener('visibilitychange', onVisibility);
window.removeEventListener('pagehide', flushNow);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [videoRef.current]);
}, [sample, flush, sampleIntervalMs, sendIntervalMs]);
return {
getUniqueWatchedSec,
addRange,
sendProgress,
startAutoSend,
stopAutoSend,
};
return { start, stop };
}
export default function Page() {
const search = useSearchParams();
const router = useRouter();
const playlistId = search.get('playlistId') ?? undefined;
const playlistIdParam = search.get('playlistId') ?? undefined;
const videoId = search.get('videoId') ?? undefined;
const { playlist, isLoading: playlistLoading } = usePlaylist(playlistId);
const { video, next, isLoading: videoLoading } = useVideo(videoId);
const { video, isLoading: videoLoading, error: videoError } = useVideo(videoId);
// Links from watch history and liked videos carry no playlist; use the video's own.
const { playlist, isLoading: playlistLoading } = usePlaylist(
playlistIdParam ?? (video as any)?.playlistId ?? undefined
);
// Segments state for current video and playlist videos
const [currentSegments, setCurrentSegments] = React.useState<WatchSegment[]>([]);
const [playlistSegments, setPlaylistSegments] = React.useState<Record<string, WatchSegment[]>>({});
const [currentProgress, setCurrentProgress] = React.useState(0);
// Per-user unlock state
const [userUnlocks, setUserUnlocks] = React.useState<Set<string>>(new Set()); // Set of unlocked videoIds
const [videoInstantAccess, setVideoInstantAccess] = React.useState<Record<string, boolean>>({}); // videoId -> instantAccess
const [unlockingVideo, setUnlockingVideo] = React.useState<string | null>(null); // videoId being unlocked
// inside your videoplayer component
const videoRef = React.useRef<HTMLVideoElement | null>(null);
const tracker = useWatchTracker(videoRef, { sendIntervalMs: 5000 });
const videoRef = React.useRef<HTMLVideoElement | null>(null);
const trackedVideoIdRef = React.useRef<string | null>(null);
const userUnlocksRef = React.useRef(userUnlocks);
React.useEffect(() => {
userUnlocksRef.current = userUnlocks;
}, [userUnlocks]);
// Fetch segments for current video
// Every progress update returns the server's view: percentage, merged
// segments, and the next video if this one just unlocked it.
const handleProgressResult = React.useCallback((id: string, result: ProgressResult) => {
const segments = result.segments;
if (segments) {
setPlaylistSegments((prev) => ({ ...prev, [id]: segments }));
}
if (id === trackedVideoIdRef.current) {
if (segments) setCurrentSegments(segments);
if (typeof result.progress?.percent === 'number') setCurrentProgress(result.progress.percent);
}
const unlocked = result.unlockedNext;
if (unlocked && !userUnlocksRef.current.has(unlocked.id)) {
setUserUnlocks((prev) => new Set(prev).add(unlocked.id));
toast.success(`Unlocked: ${unlocked.title}`);
}
}, []);
const tracker = useWatchTracker(videoRef, { onResult: handleProgressResult });
const { start: startTracking, stop: stopTracking } = tracker;
// Load saved progress for the current video. Live updates then arrive with
// each progress save, so there is no need to poll.
React.useEffect(() => {
if (!videoId) return;
let cancelled = false;
const fetchSegments = async () => {
try {
const res = await fetch(`/api/progress/segments?videoId=${videoId}`);
const [res, progressRes] = await Promise.all([
fetch(`/api/progress/segments?videoId=${videoId}`),
fetch(`/api/progress?videoId=${videoId}`),
]);
if (cancelled) return;
if (res.ok) {
const data = await res.json();
setCurrentSegments(data.segments ?? []);
if (!cancelled) setCurrentSegments(data.segments ?? []);
}
const progressRes = await fetch(`/api/progress?videoId=${videoId}`);
if (progressRes.ok) {
const data = await progressRes.json();
setCurrentProgress(data.percent ?? 0);
if (!cancelled) setCurrentProgress(data.percent ?? 0);
}
} catch (err) {
console.error('Failed to fetch segments:', err);
}
};
setCurrentSegments([]);
setCurrentProgress(0);
fetchSegments();
// Refresh segments every 5 seconds
const interval = setInterval(fetchSegments, 5000);
return () => clearInterval(interval);
return () => {
cancelled = true;
};
}, [videoId]);
// Fetch segments for all playlist videos
// Fetch watched segments for every video in the playlist in one request
const loadedPlaylistId: string | undefined = playlist?.id;
React.useEffect(() => {
if (!playlist?.videos || playlist.videos.length === 0) return;
if (!loadedPlaylistId) return;
let cancelled = false;
const fetchPlaylistSegments = async () => {
const segments: Record<string, WatchSegment[]> = {};
const instantAccess: Record<string, boolean> = {};
for (const video of playlist.videos) {
// Fetch segments
try {
const res = await fetch(`/api/progress/segments?videoId=${video.id}`);
if (res.ok) {
const data = await res.json();
segments[video.id] = data.segments ?? [];
}
} catch (err) {
console.error(`Failed to fetch segments for video ${video.id}:`, err);
try {
const res = await fetch(`/api/progress/all?playlistId=${encodeURIComponent(loadedPlaylistId)}`);
if (!res.ok || cancelled) return;
const data = await res.json();
const segments: Record<string, WatchSegment[]> = {};
for (const [id, entry] of Object.entries<any>(data.progress ?? {})) {
segments[id] = entry.segments ?? [];
}
// Track instantAccess status from video object
instantAccess[video.id] = (video as any).instantAccess ?? false;
// Keep anything newer that arrived from a progress save in the meantime.
if (!cancelled) setPlaylistSegments((prev) => ({ ...segments, ...prev }));
} catch (err) {
console.error('Failed to fetch playlist progress:', err);
}
setPlaylistSegments(segments);
setVideoInstantAccess(instantAccess);
};
setPlaylistSegments({});
fetchPlaylistSegments();
}, [playlist?.videos]);
return () => {
cancelled = true;
};
}, [loadedPlaylistId]);
// Fetch user's VideoUnlock records
React.useEffect(() => {
@@ -345,8 +339,9 @@ React.useEffect(() => {
const res = await fetch('/api/user/unlocks');
if (res.ok) {
const data = await res.json();
const unlockedVideoIds = new Set((data.unlocks?.map((u: any) => u.videoId) ?? []) as string[]);
setUserUnlocks(unlockedVideoIds);
const unlockedVideoIds = (data.unlocks?.map((u: any) => u.videoId) ?? []) as string[];
// Merge, in case a progress save unlocked something while this loaded.
setUserUnlocks((prev) => new Set([...prev, ...unlockedVideoIds]));
}
} catch (err) {
console.error('Failed to fetch user unlocks:', err);
@@ -356,55 +351,20 @@ React.useEffect(() => {
fetchUserUnlocks();
}, []);
// Track whichever video is loaded; switching videos flushes the old one first.
const loadedVideoId = video?.id ? String(video.id) : null;
const loadedVideoDuration: number | null = video?.durationSec ?? null;
React.useEffect(() => {
if (!video) return;
if (!loadedVideoId) return;
let didCancel = false;
let poll: number | null = null;
const startWhenReady = () => {
if (didCancel) return;
const el = videoRef.current;
const duration = (video.durationSec ?? Math.floor(el?.duration ?? 0)) || 0;
if (el) {
tracker.startAutoSend({
videoId: String(video.id),
playlistId: playlist?.id,
duration,
});
} else {
// poll until the video element mounts (should be quick)
poll = window.setInterval(() => {
if (videoRef.current) {
if (poll) {
window.clearInterval(poll);
poll = null;
}
tracker.startAutoSend({
videoId: String(video.id),
playlistId: playlist?.id,
duration: (video.durationSec ?? Math.floor(videoRef.current?.duration ?? 0)) || 0,
});
}
}, 150) as unknown as number;
}
};
startWhenReady();
trackedVideoIdRef.current = loadedVideoId;
startTracking({ videoId: loadedVideoId, durationSec: loadedVideoDuration });
return () => {
didCancel = true;
if (poll) {
window.clearInterval(poll);
poll = null;
}
const duration = video?.durationSec ?? Math.floor(videoRef.current?.duration ?? 0);
// send final snapshot
tracker.stopAutoSend({ sendFinal: true, videoId: String(video?.id ?? ''), playlistId: playlist?.id, duration });
stopTracking();
trackedVideoIdRef.current = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [video?.id, playlist?.id]);
}, [loadedVideoId, loadedVideoDuration, startTracking, stopTracking]);
const current: VideoFromApi = (video as VideoFromApi) ?? {
@@ -425,84 +385,40 @@ React.useEffect(() => {
return `${mins}:${secs}`;
};
// A video is open when it is the first in the playlist, marked instant
// access, or unlocked for this user. The lecturer's lock overrides all three.
const firstVideoId: string | undefined = playlist?.videos?.[0]?.id;
const isAccessible = (v: VideoFromApi) =>
!v.locked &&
(!!(v as any).instantAccess || v.id === firstVideoId || userUnlocks.has(v.id));
const openVideo = (v: VideoFromApi) => {
router.push(`/videoplayer?playlistId=${playlist?.id}&videoId=${v.id}`);
};
// The server decides whether the unlock is earned and explains when it is not.
const handleUnlock = async (v: VideoFromApi) => {
console.log('unlock request for', v.id);
// Prevent multiple unlock attempts
if (unlockingVideo === v.id) return;
if (unlockingVideo === v.id || v.locked) return;
setUnlockingVideo(v.id);
try {
// Can't unlock if globally locked
if ((v as any).locked) {
console.log('Video is globally locked, cannot unlock');
return;
}
// Can't unlock if already unlocked or has instant access
if ((v as any).instantAccess || userUnlocks.has(v.id)) {
console.log('Video already accessible');
router.push(`/videoplayer?playlistId=${playlist?.id}&videoId=${v.id}`);
return;
}
// Check if previous video in sequence is completed
if (!playlist?.videos) return;
const videoIndex = (v as any).index;
if (videoIndex <= 0) {
// First video should always be accessible
router.push(`/videoplayer?playlistId=${playlist?.id}&videoId=${v.id}`);
return;
}
// Find previous video
const previousVideo = playlist.videos.find((pv: any) => pv.index === videoIndex - 1);
if (!previousVideo) {
console.log('Previous video not found');
return;
}
// Check if previous video is completed
const progressRes = await fetch(`/api/progress?videoId=${previousVideo.id}`);
if (!progressRes.ok) {
console.error('Failed to fetch previous video progress');
alert('Error checking unlock requirements. Please try again.');
return;
}
const progressData = await progressRes.json();
const isCompleted = progressData.percent >= 90; // Consider 90% as completed
if (!isCompleted) {
console.log('Previous video not completed yet - need', Math.ceil(90 - progressData.percent), '% more');
// TODO: Show toast notification explaining unlock requirements
alert(`You need to complete ${Math.ceil(90 - progressData.percent)}% more of the previous video to unlock this one.`);
return;
}
// Previous video is completed, unlock this video
const unlockRes = await fetch('/api/user/unlocks', {
const res = await fetch('/api/user/unlocks', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ videoId: v.id }),
});
if (unlockRes.ok) {
// Update local state
setUserUnlocks(prev => new Set(prev).add(v.id));
console.log('Video unlocked successfully');
// Redirect to the unlocked video
router.push(`/videoplayer?playlistId=${playlist?.id}&videoId=${v.id}`);
} else {
const errorData = await unlockRes.json().catch(() => ({ error: 'Unknown error' }));
console.error('Failed to unlock video:', errorData.error);
alert(`Failed to unlock video: ${errorData.error}`);
const data = await res.json().catch(() => ({}));
if (!res.ok) {
toast.error(data?.error ?? 'Could not unlock this video. Please try again.');
return;
}
setUserUnlocks((prev) => new Set(prev).add(v.id));
openVideo(v);
} catch (err) {
console.error('Error checking unlock conditions:', err);
alert('Error checking if video can be unlocked. Please try again.');
console.error('Error unlocking video:', err);
toast.error('Could not unlock this video. Please try again.');
} finally {
setUnlockingVideo(null);
}
@@ -585,7 +501,11 @@ React.useEffect(() => {
ref={videoRef}
/>
) : (
<div className="flex items-center justify-center h-80">Video not available</div>
<div className="flex items-center justify-center h-80 px-6 text-center">
{videoError?.message && !/^HTTP \d+$/.test(videoError.message)
? videoError.message
: 'Video not available'}
</div>
)}
</div>
</CardContent>
@@ -698,7 +618,7 @@ React.useEffect(() => {
) : (
playlist?.videos?.map((v: VideoFromApi) => {
const active = String(v.id) === String(current.id);
const watchedPercent = 0; // Replace with actual watched % from your state/API
const accessible = isAccessible(v);
return (
<div
key={v.id}
@@ -707,29 +627,13 @@ React.useEffect(() => {
active ? 'border-primary bg-muted' : 'border-transparent hover:border-border'
}`}
onClick={() => {
// Check if video is accessible based on the logic:
// 1. If locked at schema level, always deny
if ((v as any).locked) return;
// 2. If instantAccess, always allow
if ((v as any).instantAccess) {
router.push(`/videoplayer?playlistId=${playlist.id}&videoId=${v.id}`);
return;
}
// 3. If first video (index 0), always allow
if ((v as any).index === 0) {
router.push(`/videoplayer?playlistId=${playlist.id}&videoId=${v.id}`);
return;
}
// 4. Otherwise, check if user has unlocked it
if (userUnlocks.has(v.id)) {
router.push(`/videoplayer?playlistId=${playlist.id}&videoId=${v.id}`);
}
if (accessible) openVideo(v);
}}
>
<div className="flex items-center gap-3">
<div className="relative w-20 h-12 shrink-0 overflow-hidden rounded">
<img src={v.thumbnail ?? '/01.jpg'} alt={v.title} className="w-full h-full object-cover" loading="lazy" />
{(v as any).locked || (!(v as any).instantAccess && (v as any).index !== 0 && !userUnlocks.has(v.id)) ? (
{!accessible ? (
<div className="absolute inset-0 bg-black/40 flex items-center justify-center">
<Lock className="w-5 h-5 text-white" />
</div>
@@ -742,28 +646,28 @@ React.useEffect(() => {
<Badge>{fmt(v)}</Badge>
</div>
<div className="text-xs text-muted-foreground truncate mt-1">
{(v as any).locked ? 'Locked' : (v as any).instantAccess || (v as any).index === 0 ? 'Available' : userUnlocks.has(v.id) ? 'Available' : active ? 'Now playing' : 'Locked'}
{active ? 'Now playing' : accessible ? 'Available' : 'Locked'}
</div>
</div>
{(v as any).locked || (!(v as any).instantAccess && (v as any).index !== 0 && !userUnlocks.has(v.id)) ? (
{!accessible ? (
<div className="ml-2">
<Button
size="sm"
variant={(v as any).locked ? "secondary" : "default"}
disabled={(v as any).locked || unlockingVideo === v.id}
variant={v.locked ? "secondary" : "default"}
disabled={v.locked || unlockingVideo === v.id}
onClick={(e) => {
e.stopPropagation();
handleUnlock(v);
}}
>
{unlockingVideo === v.id ? 'Unlocking...' : (v as any).locked ? 'Locked' : 'Unlock'}
{unlockingVideo === v.id ? 'Unlocking...' : v.locked ? 'Locked' : 'Unlock'}
</Button>
</div>
) : null}
</div>
<SegmentedProgressBar
segments={playlistSegments[v.id] ?? []}
duration={v.durationSec ?? 1}
+42 -19
View File
@@ -9,36 +9,51 @@ services:
restart: unless-stopped
ports:
- "3000:3000"
# Hardening: the app can't modify its own files, gain privileges, or use
# Linux capabilities. Only /tmp, the Next.js cache and /uploads are writable.
read_only: true
tmpfs:
- /tmp
- /app/.next/cache:uid=1000,gid=1000
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
# Environment variables required for production
environment:
# Database connection (must match PostgreSQL service)
DATABASE_URL: postgresql://cms_user:changeme@postgres:5432/cms_db
DATABASE_URL: postgresql://cms_user:${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}@postgres:5432/cms_db
# NextAuth configuration
NEXTAUTH_URL: http://localhost:3000 # Update to your production domain
NEXTAUTH_SECRET: ${NEXTAUTH_SECRET} # Set via .env file
# Google OAuth (from .env)
GOOGLE_CLIENT_ID: ${GOOGLE_CLIENT_ID}
GOOGLE_CLIENT_SECRET: ${GOOGLE_CLIENT_SECRET}
# Admin email whitelist (comma-separated)
ALLOWED_ADMINS: ${ALLOWED_ADMINS:-admin@university.edu}
# Shared secret for the remote transcoder API
TRANSCODER_SECRET: ${TRANSCODER_SECRET}
# Upload directory (external to Next.js public folder)
UPLOADS_DIR: /uploads
# Mounted volumes for persistent data
volumes:
# Mount uploads directory as bind mount for real-time file sync
# Must be writable by uid 1000: chown -R 1000:1000 /mnt/lab/configs/owicms/uploads
- /mnt/lab/configs/owicms/uploads:/uploads
# Wait for database to be ready before starting
depends_on:
postgres:
condition: service_healthy
# Health check
healthcheck:
test: ["CMD", "wget", "--quiet", "--tries=1", "--spider", "http://localhost:3000"]
@@ -51,16 +66,20 @@ services:
image: postgres:16-alpine
container_name: college-postgres
restart: unless-stopped
security_opt:
- no-new-privileges:true
environment:
POSTGRES_USER: cms_user
POSTGRES_PASSWORD: changeme # Change in production!
# Only applied when the data volume is first created. To change the
# password of an existing database, also run ALTER USER cms_user PASSWORD '...'
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}
POSTGRES_DB: cms_db
# Mounted volume for database persistence
volumes:
- postgres_data:/var/lib/postgresql/data
# Health check
healthcheck:
test: ["CMD-SHELL", "pg_isready -U cms_user"]
@@ -74,28 +93,32 @@ services:
dockerfile: Dockerfile.transcoder
container_name: college-transcoder
restart: on-failure
security_opt:
- no-new-privileges:true
cap_drop:
- ALL
# Environment variables
environment:
# Database connection (must match PostgreSQL service)
DATABASE_URL: postgresql://cms_user:changeme@postgres:5432/cms_db
DATABASE_URL: postgresql://cms_user:${POSTGRES_PASSWORD:?Set POSTGRES_PASSWORD in .env}@postgres:5432/cms_db
# Transcoder configuration
UPLOADS_DIR: /uploads
NODE_ENV: production
# Mounted volumes for access to uploads
volumes:
# Mount uploads directory using bind mount for real-time file sync
- /mnt/lab/configs/owicms/uploads:/uploads
# Wait for database and CMS to be ready
depends_on:
postgres:
condition: service_healthy
cms:
condition: service_healthy
# Note: This transcoder runs as a batch job.
# It processes all 'uploaded' videos and exits.
# With restart: on-failure, it will restart and process new videos.
+11 -2
View File
@@ -24,7 +24,11 @@ export function useVideo(id?: string) {
fetch(`/api/videos/${id}`)
.then(async (res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
if (!res.ok) {
// The server explains refusals (e.g. which video to finish first).
const body = await res.json().catch(() => null);
throw new Error(body?.error ?? `HTTP ${res.status}`);
}
const json = await res.json();
if (!cancelled) {
setVideo(json.video ?? null);
@@ -32,7 +36,12 @@ export function useVideo(id?: string) {
}
})
.catch((err) => {
if (!cancelled) setError(err);
if (!cancelled) {
// Never leave the previous video on screen under a new address.
setVideo(null);
setNext(null);
setError(err);
}
})
.finally(() => {
if (!cancelled) setIsLoading(false);
+55
View File
@@ -0,0 +1,55 @@
// lib/account-status.ts
// Whether an account may currently use the platform, checked against the
// database rather than whatever was true when the user signed in.
import { prisma } from './prisma';
import { normalizeEmail } from './normalize-email';
export type AccountStatus = {
/** False once a student is removed from, or deactivated on, the allow-list. */
allowed: boolean;
role: string;
/** Comma-separated course codes from the allow-list. */
levels: string;
};
// Sessions are checked on every request, so results are held briefly. A change
// to a student's access therefore takes effect within this window.
const TTL_MS = 60_000;
const MAX_ENTRIES = 5000;
const cache = new Map<string, { expires: number; status: AccountStatus }>();
export async function getAccountStatus(
userId: string,
opts: { fresh?: boolean } = {}
): Promise<AccountStatus> {
const hit = cache.get(userId);
if (!opts.fresh && hit && hit.expires > Date.now()) return hit.status;
const user = await prisma.user.findUnique({
where: { id: userId },
select: { email: true, role: true },
});
let status: AccountStatus = { allowed: false, role: 'user', levels: '' };
if (user) {
const role = user.role ?? 'user';
const email = normalizeEmail(user.email);
const student = email
? await prisma.allowedStudent.findFirst({
where: { email: { equals: email, mode: 'insensitive' } },
select: { levels: true, active: true },
})
: null;
const isAdmin = role === 'admin' || role === 'superadmin';
status = {
allowed: isAdmin || !!student?.active,
role,
levels: student?.levels ?? '',
};
}
if (cache.size >= MAX_ENTRIES) cache.clear();
cache.set(userId, { expires: Date.now() + TTL_MS, status });
return status;
}
+17
View File
@@ -1,4 +1,5 @@
// lib/admin-check.ts
import { NextResponse } from 'next/server';
import { getServerSession } from 'next-auth';
import { authOptions } from './auth-options';
@@ -9,3 +10,19 @@ export async function requireAdmin() {
if (!(role === 'admin' || role === 'superadmin')) throw { status: 403, message: 'Forbidden' };
return session;
}
/**
* Route-handler variant: returns a JSON error response when the caller is not
* an admin, or null when they are.
*/
export async function adminGuard(): Promise<NextResponse | null> {
try {
await requireAdmin();
return null;
} catch (err: any) {
return NextResponse.json(
{ error: err?.message ?? 'Unauthorized' },
{ status: err?.status ?? 401 }
);
}
}
+25 -37
View File
@@ -4,6 +4,7 @@ import GoogleProvider from "next-auth/providers/google";
import { PrismaAdapter } from "@next-auth/prisma-adapter";
import { prisma } from "./prisma";
import { normalizeEmail } from "./normalize-email";
import { getAccountStatus } from "./account-status";
export const authOptions: NextAuthOptions = {
adapter: PrismaAdapter(prisma),
@@ -125,48 +126,30 @@ export const authOptions: NextAuthOptions = {
/**
* JWT CALLBACK
* Token is the source of truth for session data.
* Role must ALWAYS come from the database.
* Runs on sign-in and again every time the session is read. Role, course
* levels and whether the account is still allowed always come from the
* database, so removing a student takes effect without waiting for their
* 30-day session to expire.
*/
async jwt({ token, user }) {
// On first login
if (user?.id) {
const dbUser = await prisma.user.findUnique({
where: { id: user.id },
select: {
id: true,
role: true,
email: true,
},
});
if (user?.id) token.id = user.id;
if (dbUser) {
token.id = dbUser.id;
token.role = dbUser.role ?? "user";
// Attach student levels if applicable
let levels = "";
const normalizedDbEmail = normalizeEmail(dbUser.email);
if (normalizedDbEmail) {
const allowedStudent = await prisma.allowedStudent.findFirst({
where: {
email: {
equals: normalizedDbEmail,
mode: "insensitive",
},
},
select: { levels: true },
});
levels = allowedStudent?.levels ?? "";
}
token.levels = levels;
const userId = (token.id ?? token.sub) as string | undefined;
if (userId) {
try {
// A fresh sign-in always re-reads the database.
const status = await getAccountStatus(userId, { fresh: !!user });
token.id = userId;
token.role = status.role;
token.levels = status.levels;
token.disabled = !status.allowed;
} catch (err) {
// Database hiccup: keep what the token already says rather than
// signing everyone out.
console.error('[auth] account status check failed', err);
}
}
// Safety fallback
token.role = token.role ?? "user";
return token;
@@ -174,9 +157,14 @@ export const authOptions: NextAuthOptions = {
/**
* SESSION CALLBACK
* Only exposes data already trusted in the token.
* Only exposes data already trusted in the token. A disabled account gets
* a session with no user, which every route treats as signed out.
*/
async session({ session, token }) {
if (token.disabled) {
return { expires: session.expires };
}
if (session.user) {
(session.user as any).id = token.id ?? null;
(session.user as any).role = token.role ?? "user";
+69
View File
@@ -0,0 +1,69 @@
// lib/video-access.ts
// Who is asking, and may they watch this video? Shared by the video API and
// the routes that serve the media files themselves.
import { getServerSession } from 'next-auth';
import { authOptions } from './auth-options';
import { prisma } from './prisma';
import { checkUnlockEligibility } from './watch-progress';
export type Viewer = { id: string; isAdmin: boolean };
/** The signed-in user, or null. Does not touch the database. */
export async function getViewer(): Promise<Viewer | null> {
const session = await getServerSession(authOptions);
const user = session?.user as { id?: string; email?: string; role?: string } | undefined;
if (!user?.email || !user.id) return null;
return { id: user.id, isAdmin: user.role === 'admin' || user.role === 'superadmin' };
}
export type AccessResult = { ok: true } | { ok: false; status: number; error: string };
// A playing video requests a media chunk every few seconds, so a positive
// answer is remembered briefly. Refusals are never cached: an unlock applies
// straight away, while a newly applied lock can take up to this long to bite.
const TTL_MS = 60_000;
const MAX_ENTRIES = 20_000;
const allowed = new Map<string, number>();
/**
* Admins may watch anything. Students need to be enrolled, and the video must
* be open to them: first in its playlist, instant access, already unlocked, or
* earned by completing the video before it.
*/
export async function checkVideoAccess(
viewer: Viewer,
videoId: string,
// `peek` only answers the question (for listings); it records nothing.
opts: { peek?: boolean } = {}
): Promise<AccessResult> {
if (viewer.isAdmin) return { ok: true };
const key = `${viewer.id}:${videoId}`;
const expires = allowed.get(key);
if (expires && expires > Date.now()) return { ok: true };
const check = await checkUnlockEligibility(viewer.id, videoId);
const where = { userId_videoId: { userId: viewer.id, videoId } };
if (check.ok) {
if (!check.free && !opts.peek) {
// Earned but never recorded (e.g. completed before unlocks were tracked).
await prisma.videoUnlock.upsert({
where,
update: {},
create: { userId: viewer.id, videoId },
});
}
} else {
const unlocked =
check.reason === 'not_earned' &&
(await prisma.videoUnlock.findUnique({ where, select: { id: true } }));
if (!unlocked) return { ok: false, status: check.status, error: check.error };
}
if (!opts.peek) {
if (allowed.size >= MAX_ENTRIES) allowed.clear();
allowed.set(key, Date.now() + TTL_MS);
}
return { ok: true };
}
+224
View File
@@ -0,0 +1,224 @@
// lib/watch-progress.ts
// Single source of truth for watch-progress maths and the sequential unlock rule.
import { prisma } from './prisma';
/** A video counts as completed (and unlocks the next one) at this percentage. */
export const UNLOCK_THRESHOLD_PERCENT = 80;
export type SecRange = { startSec: number; endSec: number };
/**
* Merges overlapping or touching ranges. Returns a new array sorted by start.
* When ranges carry a `watchedAt`, the merged range keeps the most recent one.
*/
export function mergeRanges<T extends SecRange & { watchedAt?: Date }>(
ranges: T[]
): Array<SecRange & { watchedAt?: Date }> {
const sorted = ranges
.filter((r) => r.endSec > r.startSec)
.sort((a, b) => a.startSec - b.startSec);
const merged: Array<SecRange & { watchedAt?: Date }> = [];
for (const r of sorted) {
const last = merged[merged.length - 1];
if (last && r.startSec <= last.endSec) {
last.endSec = Math.max(last.endSec, r.endSec);
if (r.watchedAt && (!last.watchedAt || r.watchedAt > last.watchedAt)) {
last.watchedAt = r.watchedAt;
}
} else {
merged.push({ startSec: r.startSec, endSec: r.endSec, watchedAt: r.watchedAt });
}
}
return merged;
}
export function totalSeconds(ranges: SecRange[]): number {
return ranges.reduce((sum, r) => sum + Math.max(0, r.endSec - r.startSec), 0);
}
/**
* Validates client-reported [start, end] pairs and snaps them to whole seconds
* inside the video. Anything malformed is dropped.
*/
export function sanitizeRanges(input: unknown, durationSec: number): SecRange[] {
if (!Array.isArray(input)) return [];
const max = Math.ceil(durationSec);
const out: SecRange[] = [];
for (const item of input.slice(0, 500)) {
if (!Array.isArray(item) || item.length < 2) continue;
const s = Number(item[0]);
const e = Number(item[1]);
if (!Number.isFinite(s) || !Number.isFinite(e) || e <= s) continue;
const startSec = Math.min(max, Math.max(0, Math.floor(s)));
const endSec = Math.min(max, Math.max(0, Math.ceil(e)));
if (endSec > startSec) out.push({ startSec, endSec });
}
return out;
}
type RestrictableVideo = {
videoCourses?: Array<{ courseId: string; exclusive: boolean }>;
};
/** Drops videos that are exclusive to courses the user is not enrolled in. */
export function filterAccessibleVideos<T extends RestrictableVideo>(
videos: T[],
userCourseIds: string[]
): T[] {
return videos.filter((video) => {
const restricted = (video.videoCourses ?? [])
.filter((a) => a.exclusive)
.map((a) => a.courseId);
if (restricted.length === 0) return true;
return restricted.some((courseId) => userCourseIds.includes(courseId));
});
}
type PlaylistVideo = {
id: string;
title: string;
index: number;
locked: boolean;
instantAccess: boolean;
};
/**
* The playlist's videos, in order, as this user sees them. Returns null when
* the user is not enrolled in any course the playlist belongs to.
*/
export async function getPlaylistVideosForUser(
userId: string,
playlistId: string
): Promise<PlaylistVideo[] | null> {
const [enrollments, playlist] = await Promise.all([
prisma.enrollment.findMany({ where: { userId }, select: { courseId: true } }),
prisma.playlist.findUnique({
where: { id: playlistId },
select: {
courseId: true,
courses: { select: { courseId: true } },
videos: {
orderBy: { index: 'asc' },
select: {
id: true,
title: true,
index: true,
locked: true,
instantAccess: true,
videoCourses: { select: { courseId: true, exclusive: true } },
},
},
},
}),
]);
if (!playlist) return null;
const userCourseIds = enrollments.map((e) => e.courseId);
const playlistCourseIds = [playlist.courseId, ...playlist.courses.map((c) => c.courseId)];
if (!playlistCourseIds.some((id) => userCourseIds.includes(id))) return null;
return filterAccessibleVideos(playlist.videos, userCourseIds).map(
({ id, title, index, locked, instantAccess }) => ({ id, title, index, locked, instantAccess })
);
}
/**
* Called when a user completes `video`: unlocks the next video in the playlist
* that needs unlocking (skipping lecturer-locked and instant-access videos).
*/
export async function unlockNextVideo(
userId: string,
video: { id: string; playlistId: string }
): Promise<{ id: string; title: string } | null> {
const videos = await getPlaylistVideosForUser(userId, video.playlistId);
if (!videos) return null;
const pos = videos.findIndex((v) => v.id === video.id);
if (pos === -1) return null;
const next = videos.slice(pos + 1).find((v) => !v.locked && !v.instantAccess);
if (!next) return null;
await prisma.videoUnlock.upsert({
where: { userId_videoId: { userId, videoId: next.id } },
update: {},
create: { userId, videoId: next.id },
});
return { id: next.id, title: next.title };
}
export type UnlockCheck =
| { ok: true; free: boolean }
| {
ok: false;
status: number;
reason: 'not_found' | 'locked' | 'no_access' | 'not_earned';
error: string;
};
/**
* Decides whether a user may unlock a video by hand. Mirrors unlockNextVideo:
* a video opens once any watchable video between it and the previous
* unlock-gated video (inclusive) has been completed.
* `free` means the video never needed an unlock record.
*/
export async function checkUnlockEligibility(
userId: string,
videoId: string
): Promise<UnlockCheck> {
const video = await prisma.video.findUnique({
where: { id: videoId },
select: { id: true, playlistId: true, locked: true },
});
if (!video) return { ok: false, status: 404, reason: 'not_found', error: 'Video not found' };
if (video.locked) {
return {
ok: false,
status: 403,
reason: 'locked',
error: 'This video is locked by your lecturer.',
};
}
const videos = await getPlaylistVideosForUser(userId, video.playlistId);
const pos = videos ? videos.findIndex((v) => v.id === videoId) : -1;
if (!videos || pos === -1) {
return {
ok: false,
status: 403,
reason: 'no_access',
error: 'You do not have access to this video.',
};
}
if (videos[pos].instantAccess || pos === 0) return { ok: true, free: true };
// Walk back to the previous video that itself needs unlocking.
let gateStart = 0;
for (let i = pos - 1; i >= 0; i--) {
if (!videos[i].locked && !videos[i].instantAccess) {
gateStart = i;
break;
}
}
const gate = videos.slice(gateStart, pos).filter((v) => !v.locked);
if (gate.length === 0) return { ok: true, free: true };
const progress = await prisma.videoProgress.findMany({
where: { userId, videoId: { in: gate.map((v) => v.id) } },
select: { videoId: true, percent: true, completed: true },
});
const done = progress.some(
(p) => p.completed || p.percent >= UNLOCK_THRESHOLD_PERCENT
);
if (done) return { ok: true, free: false };
const previous = gate[gate.length - 1];
const watched = progress.find((p) => p.videoId === previous.id)?.percent ?? 0;
return {
ok: false,
status: 403,
reason: 'not_earned',
error: `Watch at least ${UNLOCK_THRESHOLD_PERCENT}% of "${previous.title}" to unlock this video (you are at ${watched}%).`,
};
}
+20
View File
@@ -1,6 +1,23 @@
import type { NextConfig } from "next";
const securityHeaders = [
{ key: "X-Content-Type-Options", value: "nosniff" },
{ key: "X-Frame-Options", value: "DENY" },
{ key: "Referrer-Policy", value: "strict-origin-when-cross-origin" },
{ key: "Permissions-Policy", value: "camera=(), microphone=(), geolocation=()" },
// Limited to directives that don't conflict with Next.js inline scripts:
// blocks <base> tag hijacking, plugin content, and framing (clickjacking)
{
key: "Content-Security-Policy",
value: "base-uri 'self'; object-src 'none'; frame-ancestors 'none'",
},
];
const nextConfig: NextConfig = {
poweredByHeader: false,
// Emit a self-contained server with only the files it needs, so the Docker
// image does not have to ship the whole node_modules tree.
output: "standalone",
images: {
remotePatterns: [
{
@@ -10,6 +27,9 @@ const nextConfig: NextConfig = {
},
],
},
async headers() {
return [{ source: "/:path*", headers: securityHeaders }];
},
};
export default nextConfig;
-9575
View File
File diff suppressed because it is too large Load Diff
+6 -6
View File
@@ -45,11 +45,11 @@
"formidable": "^3.5.4",
"hls.js": "^1.6.15",
"lucide-react": "^0.554.0",
"next": "16.0.3",
"next-auth": "^4.24.13",
"next": "16.4.6",
"next-auth": "^4.24.15",
"next-themes": "^0.4.6",
"react": "19.2.0",
"react-dom": "19.2.0",
"react": "19.3.0",
"react-dom": "19.3.0",
"recharts": "2.15.4",
"sonner": "^2.0.7",
"swr": "^2.3.6",
@@ -61,13 +61,13 @@
"devDependencies": {
"@tailwindcss/postcss": "^4.1.17",
"@types/formidable": "^3.4.6",
"@types/unzipper": "^0.10.10",
"@types/node": "^20.19.25",
"@types/react": "^19",
"@types/react-dom": "^19",
"@types/unzipper": "^0.10.10",
"autoprefixer": "^10.4.22",
"eslint": "^9",
"eslint-config-next": "16.0.3",
"eslint-config-next": "16.4.6",
"postcss": "^8.5.6",
"prisma": "^6.19.0",
"tailwindcss": "^4.1.17",
+900 -843
View File
File diff suppressed because it is too large Load Diff