feat(training): rich-text lessons with inline images and 25 MB manuals
CI / build (push) Has been cancelled

Admins were authoring plain-text lessons, which made illustrated tutorials
and reordering images impossible. Lesson bodies are now sanitized HTML from a
Tiptap editor with alignment, sizing, drag-reorder and alt text, and inline
images reuse the training_resources table so no migration is needed.
Attachments grow to 25 MB, PDFs stream inline, and SVG is rejected because it
can execute as a document. Legacy plain-text bodies are normalized on read.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-20 12:18:24 +00:00
parent e8759cdee8
commit cb5976edb2
18 changed files with 1799 additions and 114 deletions
+18 -2
View File
@@ -11,6 +11,7 @@ import {
listResourceStoredNames,
updateLesson,
} from "@/db/training";
import { trainingBodyToHtml } from "@/lib/rich-text";
import { uploadPath } from "@/lib/uploads";
const JSON_HEADERS = { "content-type": "application/json" };
@@ -61,8 +62,14 @@ export const GET = async () => {
);
}
const canAuthor = hasPermission(user.systemRole, "training.author");
const lessons = await listLessons({ includeUnpublished: canAuthor });
return new Response(
JSON.stringify({ lessons: await listLessons({ includeUnpublished: canAuthor }) }),
JSON.stringify({
lessons: lessons.map((lesson) => ({
...lesson,
body: trainingBodyToHtml(lesson.body),
})),
}),
{ status: 200, headers: JSON_HEADERS },
);
};
@@ -78,7 +85,13 @@ export const POST = async (req: NextRequest) => {
headers: JSON_HEADERS,
});
}
const lesson = await createLesson(parsed.data);
const lesson = await createLesson({
...parsed.data,
body:
parsed.data.body === undefined
? undefined
: trainingBodyToHtml(parsed.data.body),
});
return new Response(JSON.stringify({ lesson }), {
status: 201,
headers: JSON_HEADERS,
@@ -104,6 +117,9 @@ export const PATCH = async (req: NextRequest) => {
});
}
const { id, ...patch } = parsed.data;
if (patch.body !== undefined) {
patch.body = trainingBodyToHtml(patch.body);
}
const lesson = await updateLesson(id, patch);
if (!lesson) {
return new Response(JSON.stringify({ error: "unknown lesson" }), {
+8 -5
View File
@@ -8,6 +8,7 @@ import {
MAX_UPLOAD_BYTES,
ensureUploadDir,
isAllowedUpload,
resolveUploadMimeType,
storedNameFor,
uploadPath,
uploadSignatureMatches,
@@ -65,12 +66,14 @@ export const POST = async (req: NextRequest) => {
});
}
if (file.size > MAX_UPLOAD_BYTES) {
return new Response(JSON.stringify({ error: "file is larger than 5 MB" }), {
status: 400,
headers: JSON_HEADERS,
});
return new Response(
JSON.stringify({
error: `file is larger than ${Math.round(MAX_UPLOAD_BYTES / (1024 * 1024))} MB`,
}),
{ status: 400, headers: JSON_HEADERS },
);
}
const mimeType = file.type || "application/octet-stream";
const mimeType = resolveUploadMimeType(file.name, file.type);
if (!isAllowedUpload(mimeType)) {
return new Response(
JSON.stringify({ error: `file type not allowed: ${mimeType}` }),
+7 -4
View File
@@ -30,15 +30,18 @@ export const GET = async (
try {
const data = await readFile(uploadPath(resource.storedName));
const safeName = resource.filename.replaceAll('"', "");
const safeName = resource.filename.replace(/[\r\n"\\]/g, "").trim() || "download";
const inline =
resource.mimeType.startsWith("image/") ||
resource.mimeType === "application/pdf";
return new Response(new Uint8Array(data), {
status: 200,
headers: {
"content-type": resource.mimeType,
"content-length": String(data.length),
// Only images render inline; everything else is a download, so an
// uploaded HTML/SVG payload cannot execute in this origin.
"content-disposition": `${resource.mimeType.startsWith("image/") ? "inline" : "attachment"}; filename="${safeName}"; filename*=UTF-8''${encodeURIComponent(resource.filename)}`,
// Images and PDF manuals render in the browser; everything else is a
// download, so an uploaded HTML/SVG payload cannot execute here.
"content-disposition": `${inline ? "inline" : "attachment"}; filename="${safeName}"; filename*=UTF-8''${encodeURIComponent(resource.filename)}`,
"cache-control": "private, max-age=0, must-revalidate",
"x-content-type-options": "nosniff",
},