diff --git a/public/admin.js b/public/admin.js index 40a1e16..bd766ec 100644 --- a/public/admin.js +++ b/public/admin.js @@ -620,13 +620,15 @@ $('#import-go-btn').addEventListener('click', async () => { method: 'POST', body: { base64, originalName: file.name }, }); - const link = `📄 ${result.originalName}`; + const embed = result.htmlUrl + ? `\n

⬇️ Download ${result.originalName}

` + : `📄 ${result.originalName}`; if (type === 'assignment') { - $('#ie-body-assignment').value = link; + $('#ie-body-assignment').value = embed; } else { - $('#ie-body-lesson').innerHTML = link; + $('#ie-body-lesson').innerHTML = embed; } - $('#import-status').textContent = '✅ File uploaded. Link inserted into body.'; + $('#import-status').textContent = '✅ File uploaded. Document embedded in body.'; } catch (err) { $('#import-status').textContent = `❌ ${err.message}`; } finally { diff --git a/server.js b/server.js index d146eac..21ea7e6 100644 --- a/server.js +++ b/server.js @@ -5,6 +5,7 @@ import { spawnSync } from 'node:child_process'; import { existsSync, mkdirSync, readFileSync, readdirSync, statSync } from 'node:fs'; import { readFile, writeFile } from 'node:fs/promises'; import { createHash, randomUUID } from 'node:crypto'; +import mammoth from 'mammoth'; import { db, sha256 } from './db.js'; const ROOT = dirname(fileURLToPath(import.meta.url)); @@ -1539,14 +1540,30 @@ app.post('/api/admin/upload-attachment', requirePin, async (req, res) => { catch (err) { return res.status(400).json({ error: 'Invalid base64' }); } const ext = extname(originalName).toLowerCase() || '.docx'; - const filename = `${randomUUID()}${ext}`; + const uuid = randomUUID(); + const filename = `${uuid}${ext}`; try { await writeFile(join(ATTACHMENTS_DIR, filename), buffer); } catch (err) { return res.status(500).json({ error: `Could not save file: ${err.message}` }); } - return res.json({ url: `/api/attachments/${filename}`, originalName }); + // Convert to HTML so it can be shown in an iframe + let htmlUrl = null; + try { + const { value: bodyHtml } = await mammoth.convertToHtml({ buffer }); + const htmlFilename = `${uuid}.html`; + const page = ` + +${bodyHtml}`; + await writeFile(join(ATTACHMENTS_DIR, htmlFilename), page); + htmlUrl = `/api/attachments/${htmlFilename}`; + } catch (_) { + // HTML preview generation failed — download link still works + } + + return res.json({ url: `/api/attachments/${filename}`, htmlUrl, originalName }); }); app.get('/health', (req, res) => res.json({ ok: true }));