From 42cec52e21a9d96e3642167d92b1327766103192 Mon Sep 17 00:00:00 2001 From: ssbg04 Date: Thu, 17 Sep 2026 22:28:17 +0800 Subject: [PATCH] fix(documents): fix thumbnail generation by selecting doc id and clean cache on delete --- backend/src/controllers/documentController.js | 21 ++++++++++++++++++- frontend/AGENTS.md | 4 ++-- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/backend/src/controllers/documentController.js b/backend/src/controllers/documentController.js index eb4a7ba..f1c8e65 100644 --- a/backend/src/controllers/documentController.js +++ b/backend/src/controllers/documentController.js @@ -284,7 +284,7 @@ exports.viewDocument = (req, res) => { exports.getThumbnail = async (req, res) => { try { - const doc = db.prepare('SELECT student_id, file_path, file_name FROM documents WHERE id = ?').get(req.params.id); + const doc = db.prepare('SELECT id, student_id, file_path, file_name FROM documents WHERE id = ?').get(req.params.id); if (!doc) return res.status(404).json({ message: 'Document not found' }); const isTeacher = req.user?.role?.toLowerCase() === 'teacher'; @@ -1295,6 +1295,16 @@ exports.permanentDeleteDocument = (req, res) => { fs.unlinkSync(doc.file_path); } + const thumbnailDir = path.resolve('./uploads/thumbnails'); + const imgThumb = path.join(thumbnailDir, `thumb_img_${req.params.id}.webp`); + const pdfThumb = path.join(thumbnailDir, `thumb_${req.params.id}.png`); + if (fs.existsSync(imgThumb)) { + try { fs.unlinkSync(imgThumb); } catch (_) {} + } + if (fs.existsSync(pdfThumb)) { + try { fs.unlinkSync(pdfThumb); } catch (_) {} + } + db.transaction(() => { db.prepare('DELETE FROM documents WHERE id = ?').run(req.params.id); db.prepare('DELETE FROM recent_deleted WHERE document_id = ?').run(req.params.id); @@ -1323,6 +1333,7 @@ exports.bulkPermanentDelete = (req, res) => { const getStmt = db.prepare('SELECT file_path, file_name FROM documents WHERE id = ?'); const deleteStmt = db.prepare('DELETE FROM documents WHERE id = ?'); const deleteTrashStmt = db.prepare('DELETE FROM recent_deleted WHERE document_id = ?'); + const thumbnailDir = path.resolve('./uploads/thumbnails'); db.transaction(() => { for (const id of ids) { @@ -1331,6 +1342,14 @@ exports.bulkPermanentDelete = (req, res) => { if (fs.existsSync(doc.file_path)) { fs.unlinkSync(doc.file_path); } + const imgThumb = path.join(thumbnailDir, `thumb_img_${id}.webp`); + const pdfThumb = path.join(thumbnailDir, `thumb_${id}.png`); + if (fs.existsSync(imgThumb)) { + try { fs.unlinkSync(imgThumb); } catch (_) {} + } + if (fs.existsSync(pdfThumb)) { + try { fs.unlinkSync(pdfThumb); } catch (_) {} + } deleteStmt.run(id); deleteTrashStmt.run(id); deletedNames.push(doc.file_name); diff --git a/frontend/AGENTS.md b/frontend/AGENTS.md index fdeca46..98c2337 100644 --- a/frontend/AGENTS.md +++ b/frontend/AGENTS.md @@ -619,7 +619,7 @@ Files backup/restore - mirror mode or zip --> - in android app, screen header move the print list icon, upload icon - in file preview screen add icon button of all dropdown menu from the more menu --> -# STUDENT SCREEN + - in filter documents only add document type, make it dropdown not modal # ARCHIVE SCREEN -- do the same from document screen design from this chat today +- do the same from document screen design from this chat today --> ---