feat(training): rich-text lessons with inline images and 25 MB manuals
CI / build (push) Has been cancelled
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:
+31
-1
@@ -9,7 +9,22 @@ import { mkdir } from "node:fs/promises";
|
||||
|
||||
export const UPLOAD_DIR = process.env.UPLOAD_DIR ?? "/tmp/uploads";
|
||||
|
||||
export const MAX_UPLOAD_BYTES = 5 * 1024 * 1024;
|
||||
export const MAX_UPLOAD_BYTES = 25 * 1024 * 1024;
|
||||
|
||||
const MIME_BY_EXTENSION: Record<string, string> = {
|
||||
".pdf": "application/pdf",
|
||||
".doc": "application/msword",
|
||||
".docx": "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
|
||||
".txt": "text/plain",
|
||||
".md": "text/markdown",
|
||||
".markdown": "text/markdown",
|
||||
".png": "image/png",
|
||||
".jpg": "image/jpeg",
|
||||
".jpeg": "image/jpeg",
|
||||
".gif": "image/gif",
|
||||
".webp": "image/webp",
|
||||
".avif": "image/avif",
|
||||
};
|
||||
|
||||
const ALLOWED_MIME_PREFIXES = ["image/"];
|
||||
const ALLOWED_MIME_TYPES = [
|
||||
@@ -21,12 +36,27 @@ const ALLOWED_MIME_TYPES = [
|
||||
];
|
||||
|
||||
export function isAllowedUpload(mimeType: string): boolean {
|
||||
// SVG is an active document: served inline it can run script in this origin.
|
||||
if (mimeType === "image/svg+xml") return false;
|
||||
return (
|
||||
ALLOWED_MIME_PREFIXES.some((prefix) => mimeType.startsWith(prefix)) ||
|
||||
ALLOWED_MIME_TYPES.includes(mimeType)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Browsers sometimes leave File.type empty for a PDF or document. Fall back to
|
||||
* the extension so a legitimate manual is not rejected as octet-stream.
|
||||
*/
|
||||
export function resolveUploadMimeType(
|
||||
filename: string,
|
||||
declaredType: string,
|
||||
): string {
|
||||
const declared = declaredType.split(";")[0].trim().toLowerCase();
|
||||
if (declared && declared !== "application/octet-stream") return declared;
|
||||
return MIME_BY_EXTENSION[path.extname(filename).toLowerCase()] ?? "application/octet-stream";
|
||||
}
|
||||
|
||||
function startsWith(bytes: Uint8Array, signature: number[]): boolean {
|
||||
return signature.every((value, index) => bytes[index] === value);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user