All checks were successful
Verify and publish V2 container / build-and-push (push) Successful in 30s
Generation - Every quiz is NCLEX style; "Practice exam" writes 50 questions. - Calculation questions only when the material involves calculations (max 5 per exam). - Broad topics: the class's topics are listed in the prompt, a quick grouping call maps narrow labels onto broad ones, and leftover variants are merged. - Exact select-all count per batch; option order shuffled. - Python check: at most 3 runs, fixed problems count as passed, cosmetic remarks ignored. Assistant - Text, tool steps and cards appear in the order they happen, with paragraph breaks between steps. - Optional thinking box: fixed height, follows the newest text, can be scrolled back. - "+" opens a material picker listing recent items; items can be dragged in from class pages; shorter placeholder. MCP servers - Streamable HTTP client; encrypted headers. - Per-server instructions and switches for chat, Explain, generation and the quiz check. - Settings UI. Share links - Optional "Explain this" and "Ask about this material" per link, off by default. - Items are loaded on the server and checked against the share; no tools; rate-limited. - Migration 4 stores the switches. Study and documents - Standard flashcard mode (optionally repeat missed cards until known) next to spaced repetition. - Streak visuals restyled in the app palette. - Side by side / Original document views, with sandboxed previews of Word and PowerPoint files. Other - Built-in embedding model baked into the image; sharp stubbed if it cannot load; real load error shown in Settings. - Python runner blocks network and process modules in JavaScript too. - "View questions and answers" removed from the quiz start screen. - Docs updated. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
222 lines
11 KiB
TypeScript
222 lines
11 KiB
TypeScript
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import crypto from 'node:crypto';
|
|
import { Hono } from 'hono';
|
|
import { bodyLimit } from 'hono/body-limit';
|
|
import { z } from 'zod';
|
|
import { titleSchema } from '../../shared/content.ts';
|
|
import { isAuthenticated } from '../auth.ts';
|
|
import { badRequest, HttpError, idParam, readBody, type AppContext } from '../http.ts';
|
|
import { getClass, mapClass, resolveFolder } from '../services/content.ts';
|
|
import { deleteDocument, documentPath, enqueue, getDocument, ingestUpload, listChunks, mapDocument, sourceLabel } from '../services/documents.ts';
|
|
import { now } from '../util.ts';
|
|
import { officePreviewHtml, PREVIEW_CSP } from '../documents/preview.ts';
|
|
import { shareFor } from './library.ts';
|
|
|
|
const IMAGE_TYPES: Record<string, { ext: string; test: (b: Uint8Array) => boolean }> = {
|
|
'image/png': { ext: 'png', test: (b) => b[0] === 0x89 && b[1] === 0x50 && b[2] === 0x4e && b[3] === 0x47 },
|
|
'image/jpeg': { ext: 'jpg', test: (b) => b[0] === 0xff && b[1] === 0xd8 && b[2] === 0xff },
|
|
'image/gif': { ext: 'gif', test: (b) => b[0] === 0x47 && b[1] === 0x49 && b[2] === 0x46 },
|
|
'image/webp': { ext: 'webp', test: (b) => b[0] === 0x52 && b[1] === 0x49 && b[8] === 0x57 && b[9] === 0x45 },
|
|
};
|
|
|
|
export function documentRoutes(app: AppContext): Hono {
|
|
const { db } = app;
|
|
const r = new Hono();
|
|
|
|
const uploadLimit = bodyLimit({
|
|
maxSize: app.config.maxUploadBytes + 1024 * 1024,
|
|
onError: () => {
|
|
throw new HttpError(413, 'too_large', `That file is larger than the ${Math.round(app.config.maxUploadBytes / 1048576)} MB upload limit.`);
|
|
},
|
|
});
|
|
|
|
r.post('/documents', uploadLimit, async (c) => {
|
|
const form = await c.req.parseBody({ all: true }).catch(() => {
|
|
throw badRequest('Upload could not be read');
|
|
});
|
|
const classId = typeof form.classId === 'string' ? form.classId : '';
|
|
const folderId = typeof form.folderId === 'string' && form.folderId ? form.folderId : null;
|
|
const raw = form.file ?? form.files;
|
|
const files = (Array.isArray(raw) ? raw : [raw]).filter((f): f is File => typeof f === 'object' && f !== null && 'arrayBuffer' in f);
|
|
if (!classId) throw badRequest('classId is required');
|
|
if (files.length === 0) throw badRequest('Choose a file to upload');
|
|
const created = [];
|
|
for (const file of files) {
|
|
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
created.push(mapDocument(ingestUpload(app, { classId, folderId, filename: file.name || 'upload', mime: file.type || undefined, bytes })));
|
|
}
|
|
return c.json({ documents: created }, 201);
|
|
});
|
|
|
|
/** Pasted notes become a Markdown document, so they are searchable and usable by AI like any upload. */
|
|
r.post('/documents/text', async (c) => {
|
|
const body = await readBody(c, z.object({ classId: z.string(), folderId: z.string().nullish(), title: titleSchema, text: z.string().trim().min(1).max(2_000_000) }));
|
|
const doc = ingestUpload(app, {
|
|
classId: body.classId,
|
|
folderId: body.folderId,
|
|
filename: `${body.title.replace(/[^\w\- ]+/g, '').trim().slice(0, 80) || 'notes'}.md`,
|
|
mime: 'text/markdown',
|
|
bytes: new TextEncoder().encode(body.text),
|
|
title: body.title,
|
|
});
|
|
return c.json({ document: mapDocument(doc) }, 201);
|
|
});
|
|
|
|
r.get('/documents/:id', (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
const outline = db
|
|
.all<{ seq: number; heading: string; page_start: number | null }>(`SELECT MIN(seq) AS seq, heading, MIN(page_start) AS page_start FROM chunks WHERE document_id = ? AND heading != '' GROUP BY heading ORDER BY seq LIMIT 200`, doc.id)
|
|
.map((o) => ({ seq: o.seq, heading: o.heading, page: o.page_start }));
|
|
return c.json({ document: mapDocument(doc), class: mapClass(getClass(db, doc.class_id)), outline, share: shareFor(app, 'document', doc.id) });
|
|
});
|
|
|
|
r.get('/documents/:id/chunks', (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
let offset = Math.max(0, Number(c.req.query('offset') ?? 0) || 0);
|
|
const limit = Math.min(100, Math.max(1, Number(c.req.query('limit') ?? 40) || 40));
|
|
// Deep link to one passage (from search or a citation): start just before it.
|
|
const around = Number(c.req.query('around'));
|
|
if (Number.isFinite(around) && around > 0) {
|
|
const hit = db.get<{ seq: number }>('SELECT seq FROM chunks WHERE id = ? AND document_id = ?', around, doc.id);
|
|
if (hit) offset = Math.max(0, hit.seq - 1);
|
|
}
|
|
const chunks = listChunks(db, doc.id, offset, limit);
|
|
return c.json({
|
|
chunks: chunks.map((ch) => ({ id: ch.id, seq: ch.seq, text: ch.text, heading: ch.heading, pageStart: ch.pageStart, pageEnd: ch.pageEnd, label: sourceLabel(ch) })),
|
|
total: doc.chunk_count,
|
|
offset,
|
|
nextOffset: chunks.length === limit && offset + limit < doc.chunk_count ? offset + limit : null,
|
|
});
|
|
});
|
|
|
|
r.get('/documents/:id/status', (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
return c.json({ document: mapDocument(doc) });
|
|
});
|
|
|
|
r.get('/documents/:id/file', (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
const file = documentPath(app, doc);
|
|
if (!fs.existsSync(file)) throw new HttpError(404, 'not_found', 'The original file is missing from storage');
|
|
const inline = doc.kind === 'pdf' || doc.kind === 'image';
|
|
const download = c.req.query('download') === '1';
|
|
return new Response(fs.readFileSync(file), {
|
|
headers: {
|
|
'Content-Type': inline ? doc.mime : 'application/octet-stream',
|
|
'Content-Disposition': `${inline && !download ? 'inline' : 'attachment'}; filename*=UTF-8''${encodeURIComponent(doc.filename)}`,
|
|
'X-Content-Type-Options': 'nosniff',
|
|
// Uploaded files are untrusted. PDFs and images are served with their verified type (sniffing off);
|
|
// everything else is forced to download.
|
|
...(inline ? {} : { 'Content-Security-Policy': "default-src 'none'; sandbox" }),
|
|
'Cache-Control': 'private, max-age=3600',
|
|
},
|
|
});
|
|
});
|
|
|
|
// Word and PowerPoint files as a sandboxed HTML page, to read the original next to the extracted text.
|
|
r.get('/documents/:id/preview', async (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
if (doc.kind !== 'docx' && doc.kind !== 'pptx') throw new HttpError(404, 'not_found', 'Only Word and PowerPoint files have a preview');
|
|
const file = documentPath(app, doc);
|
|
if (!fs.existsSync(file)) throw new HttpError(404, 'not_found', 'The original file is missing from storage');
|
|
let html: string;
|
|
try {
|
|
html = await officePreviewHtml(doc.kind, new Uint8Array(await fs.promises.readFile(file)), doc.title);
|
|
} catch (err) {
|
|
app.log('warn', 'Document preview failed', err instanceof Error ? err.message : err);
|
|
throw new HttpError(422, 'preview_failed', 'This file could not be shown. Download it to open it instead.');
|
|
}
|
|
return new Response(html, {
|
|
headers: {
|
|
'Content-Type': 'text/html; charset=utf-8',
|
|
'Content-Security-Policy': PREVIEW_CSP,
|
|
'X-Content-Type-Options': 'nosniff',
|
|
'Cache-Control': 'private, max-age=3600',
|
|
},
|
|
});
|
|
});
|
|
|
|
r.patch('/documents/:id', async (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
const body = await readBody(c, z.object({ title: titleSchema.optional(), folderId: z.string().nullish() }));
|
|
db.run(
|
|
'UPDATE documents SET title = ?, folder_id = ?, updated_at = ? WHERE id = ?',
|
|
body.title ?? doc.title,
|
|
body.folderId === undefined ? doc.folder_id : resolveFolder(db, doc.class_id, body.folderId),
|
|
now(),
|
|
doc.id,
|
|
);
|
|
return c.json({ document: mapDocument(getDocument(db, doc.id)) });
|
|
});
|
|
|
|
r.post('/documents/:id/reprocess', async (c) => {
|
|
const doc = getDocument(db, idParam(c));
|
|
const body = (await c.req.json().catch(() => ({}))) as { ocr?: unknown };
|
|
db.run(`UPDATE documents SET status = 'pending', error = '', progress = 0, updated_at = ? WHERE id = ?`, now(), doc.id);
|
|
enqueue(app, doc.id, { ocr: body.ocr === true });
|
|
return c.json({ document: mapDocument(getDocument(db, doc.id)) });
|
|
});
|
|
|
|
r.delete('/documents/:id', (c) => {
|
|
deleteDocument(app, idParam(c));
|
|
return c.json({ ok: true });
|
|
});
|
|
|
|
/* ------------------------------ images embedded in questions, cards and notes ------------------------------ */
|
|
|
|
r.post('/media', bodyLimit({ maxSize: 12 * 1024 * 1024 }), async (c) => {
|
|
const form = await c.req.parseBody().catch(() => {
|
|
throw badRequest('Upload could not be read');
|
|
});
|
|
const file = form.file;
|
|
if (!file || typeof file === 'string') throw badRequest('Choose an image');
|
|
const bytes = new Uint8Array(await file.arrayBuffer());
|
|
if (bytes.length > 10 * 1024 * 1024) throw new HttpError(413, 'too_large', 'Images can be up to 10 MB.');
|
|
// Trust the bytes, not the declared type. SVG is deliberately not accepted (it can carry script).
|
|
const match = Object.entries(IMAGE_TYPES).find(([, t]) => t.test(bytes));
|
|
if (!match) throw new HttpError(415, 'unsupported_type', 'Use a PNG, JPEG, GIF or WebP image.');
|
|
const [mime, { ext }] = match;
|
|
const id = `${crypto.randomBytes(16).toString('base64url')}.${ext}`;
|
|
fs.writeFileSync(path.join(app.config.mediaDir, id), bytes);
|
|
db.run('INSERT INTO media (id, filename, mime, size, created_at) VALUES (?, ?, ?, ?, ?)', id, (file.name || 'image').slice(0, 200), mime, bytes.length, now());
|
|
return c.json({ url: `/media/${id}`, markdown: `![${(file.name || 'image').replace(/[[\]]/g, '')}](/media/${id})` }, 201);
|
|
});
|
|
|
|
return r;
|
|
}
|
|
|
|
/**
|
|
* Images are private unless an enabled share contains a reference to them.
|
|
* Re-check on each request so revoking a share also revokes its embedded images.
|
|
*/
|
|
export function mediaRoute(app: AppContext): Hono {
|
|
const r = new Hono();
|
|
r.get('/:name', (c) => {
|
|
const name = c.req.param('name');
|
|
if (!/^[A-Za-z0-9_-]{16,40}\.(png|jpg|gif|webp)$/.test(name)) throw new HttpError(404, 'not_found', 'Not found');
|
|
if (!isAuthenticated(app, c)) {
|
|
const reference = `/media/${name}`;
|
|
const shared = app.db.get(`
|
|
SELECT 1 FROM shares s WHERE s.enabled = 1 AND (
|
|
EXISTS (SELECT 1 FROM questions q JOIN quizzes z ON z.id = q.quiz_id
|
|
WHERE ((s.kind = 'quiz' AND s.target_id = z.id) OR (s.kind = 'class' AND s.target_id = z.class_id) OR (s.kind = 'folder' AND s.target_id = z.folder_id))
|
|
AND instr(q.stem || q.options || q.rationale, ?) > 0)
|
|
OR EXISTS (SELECT 1 FROM cards c JOIN decks d ON d.id = c.deck_id
|
|
WHERE ((s.kind = 'deck' AND s.target_id = d.id) OR (s.kind = 'class' AND s.target_id = d.class_id) OR (s.kind = 'folder' AND s.target_id = d.folder_id))
|
|
AND instr(c.front || c.back || c.hint, ?) > 0)
|
|
OR EXISTS (SELECT 1 FROM notes n
|
|
WHERE ((s.kind = 'note' AND s.target_id = n.id) OR (s.kind = 'class' AND s.target_id = n.class_id) OR (s.kind = 'folder' AND s.target_id = n.folder_id))
|
|
AND instr(n.content, ?) > 0)
|
|
) LIMIT 1`, reference, reference, reference);
|
|
if (!shared) throw new HttpError(404, 'not_found', 'Not found');
|
|
}
|
|
const row = app.db.get<{ mime: string }>('SELECT mime FROM media WHERE id = ?', name);
|
|
const file = path.join(app.config.mediaDir, name);
|
|
if (!row || !fs.existsSync(file)) throw new HttpError(404, 'not_found', 'Not found');
|
|
return new Response(fs.readFileSync(file), {
|
|
headers: { 'Content-Type': row.mime, 'Cache-Control': 'private, no-store', 'X-Content-Type-Options': 'nosniff', 'Content-Security-Policy': "default-src 'none'; sandbox" },
|
|
});
|
|
});
|
|
return r;
|
|
}
|