Changeset f2943c1 in Klonkt for src/routes
- Timestamp:
- 06/30/2026 07:50:05 PM (2 months ago)
- Branches:
- main
- Children:
- 65a0697
- Parents:
- 01e3678
- File:
-
- 1 edited
-
src/routes/admin-media.js (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/routes/admin-media.js
r01e3678 rf2943c1 48 48 function statMtime(name) { try { return fs.statSync(path.join(POST_IMAGES_DIR, name)).mtimeMs; } catch { return 0; } } 49 49 50 router.get('/', requireGod, (req, res) => { 51 const site = res.locals.site; 52 if (!site) return res.status(404).send('Site required'); 53 const used = usageMap(site .id);50 // All non-sibling images, each with its loop-MP4 sibling + how many posts use it. Shared by the 51 // list view and the cleanup route so the readdir/filter/usage logic lives in one place. 52 function imageEntries(siteId) { 53 const used = usageMap(siteId); 54 54 let all = []; 55 55 try { all = fs.readdirSync(POST_IMAGES_DIR).filter(f => !f.startsWith('.')); } catch { /* dir may not exist yet */ } 56 56 const present = new Set(all); 57 const items =all57 return all 58 58 .filter(f => IMG_EXT.test(f) && !isSibling(f)) 59 59 .map(f => { … … 62 62 const hasVideo = present.has(mp4); 63 63 const ids = new Set([...(used.get(f) || []), ...(hasVideo ? (used.get(mp4) || []) : [])]); 64 return { 65 file: f, 66 url: `/media/post-images/${f}`, 67 kb: Math.round((statSize(f) + (hasVideo ? statSize(mp4) : 0)) / 1024), 68 hasVideo, 69 usedCount: ids.size, 70 _mtime: statMtime(f), 71 }; 72 }) 64 return { file: f, stem, mp4, hasVideo, usedCount: ids.size }; 65 }); 66 } 67 68 router.get('/', requireGod, (req, res) => { 69 const site = res.locals.site; 70 if (!site) return res.status(404).send('Site required'); 71 const items = imageEntries(site.id) 72 .map(e => ({ 73 file: e.file, 74 url: `/media/post-images/${e.file}`, 75 kb: Math.round((statSize(e.file) + (e.hasVideo ? statSize(e.mp4) : 0)) / 1024), 76 hasVideo: e.hasVideo, 77 usedCount: e.usedCount, 78 _mtime: statMtime(e.file), 79 })) 73 80 .sort((a, b) => b._mtime - a._mtime); // newest first 74 81 renderPage(req, res, 'pages/admin-media', { … … 101 108 const site = res.locals.site; 102 109 if (!site) return res.status(404).json({ ok: false, error: 'Site required' }); 103 const used = usageMap(site.id);104 let all = [];105 try { all = fs.readdirSync(POST_IMAGES_DIR).filter(f => !f.startsWith('.')); } catch { /* */ }106 const present = new Set(all);107 110 let removed = 0; 108 for (const f of all.filter(x => IMG_EXT.test(x) && !isSibling(x))) { 109 const stem = f.replace(/\.[^.]+$/, ''); 110 const mp4 = `${stem}-v.mp4`; 111 const ids = new Set([...(used.get(f) || []), ...(present.has(mp4) ? (used.get(mp4) || []) : [])]); 112 if (ids.size) continue; // still in use 113 for (const name of [f, mp4, `${stem}-v.jpg`]) { 111 for (const e of imageEntries(site.id)) { 112 if (e.usedCount) continue; // still in use 113 for (const name of [e.file, e.mp4, `${e.stem}-v.jpg`]) { 114 114 try { fs.unlinkSync(path.join(POST_IMAGES_DIR, name)); removed++; } catch { /* */ } 115 115 }
Note:
See TracChangeset
for help on using the changeset viewer.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)