Changeset 97bcf7e in Klonkt for src/routes/admin-media.js
- Timestamp:
- 07/30/2026 07:47:53 AM (6 weeks ago)
- Branches:
- main
- Children:
- 7d01696
- Parents:
- 6089c53
- File:
-
- 1 edited
-
src/routes/admin-media.js (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/routes/admin-media.js
r6089c53 r97bcf7e 24 24 25 25 const IMG_EXT = /\.(jpe?g|png|webp|gif|avif)$/i; 26 const VIDEO_EXT = /\.(mp4|webm|m4v|mov)$/i; 27 // C2S uploads (Shaer's composer and the help buoy) land here; the videos among 28 // them are what the Video tab shows. 29 const REPLY_MEDIA_DIR = path.resolve( 30 process.env.REPLY_MEDIA_PATH || path.join(__dirname, '..', '..', 'storage', 'media', 'reply-media') 31 ); 26 32 const isSibling = (f) => /-v\.(mp4|jpg)$/i.test(f); // an animated cover's video/poster sibling 27 33 … … 90 96 91 97 // Delete one image + its loop-MP4 / poster siblings. Basename-only + within-dir → no traversal. 98 // ── The Video tab (Robins opdracht, 30-7) ───────────────────────────────── 99 // Videos live in reply-media (C2S uploads: Shaer's composer, the help buoy). 100 // Usage is a content/attachment reference from a post, exactly like images. 101 102 function videoEntries(siteId) { 103 const posts = db.prepare('SELECT id, content, c2s_attachments FROM posts WHERE site_id = ?').all(siteId); 104 const used = new Map(); 105 const add = (fn, id) => { if (!fn) return; if (!used.has(fn)) used.set(fn, new Set()); used.get(fn).add(id); }; 106 for (const p of posts) { 107 for (const m of String(p.content || '').matchAll(/\/media\/reply-media\/([^/?#"'\s)]+)/g)) add(m[1], p.id); 108 try { for (const a of JSON.parse(p.c2s_attachments || '[]')) { const m = String(a.url || '').match(/\/media\/reply-media\/([^/?#"'\s)]+)/); if (m) add(m[1], p.id); } } catch { /* malformed never blocks the list */ } 109 } 110 let all = []; 111 try { all = fs.readdirSync(REPLY_MEDIA_DIR).filter(f => !f.startsWith('.')); } catch { /* dir may not exist yet */ } 112 const vstat = (name, key) => { try { const st = fs.statSync(path.join(REPLY_MEDIA_DIR, name)); return key === 'size' ? st.size : st.mtimeMs; } catch { return 0; } }; 113 return all 114 .filter(f => VIDEO_EXT.test(f)) 115 .map(f => ({ 116 file: f, 117 url: `/media/reply-media/${f}`, 118 kb: Math.round(vstat(f, 'size') / 1024), 119 usedCount: (used.get(f) || new Set()).size, 120 _mtime: vstat(f, 'mtime'), 121 })) 122 .sort((a, b) => b._mtime - a._mtime); 123 } 124 125 router.get('/videos', requireGod, (req, res) => { 126 const site = res.locals.site; 127 if (!site) return res.status(404).send('Site required'); 128 renderPage(req, res, 'pages/admin-videos', { 129 pageTitleKey: 'admin.t_media', 130 bodyClass: 'on-admin', 131 items: videoEntries(site.id), 132 audioOn: audioEnabled(), 133 success: req.query.success || null, 134 }); 135 }); 136 137 // Delete one video. Basename-only + within-dir, and only when no post uses it: 138 // the same guardrails the image delete has. 139 router.post('/videos/delete', requireGod, (req, res) => { 140 const site = res.locals.site; 141 if (!site) return res.status(404).json({ error: 'site' }); 142 const file = path.basename(String(req.body?.file || '')); 143 if (!file || !VIDEO_EXT.test(file)) return res.status(400).json({ error: 'bad_file' }); 144 const entry = videoEntries(site.id).find(e => e.file === file); 145 if (!entry) return res.status(404).json({ error: 'not_found' }); 146 if (entry.usedCount) return res.status(409).json({ error: 'in_use' }); 147 try { fs.unlinkSync(path.join(REPLY_MEDIA_DIR, file)); } catch { /* already gone is gone */ } 148 res.json({ ok: true }); 149 }); 150 92 151 router.post('/delete', requireGod, (req, res) => { 93 152 if (!res.locals.site) return res.status(404).json({ ok: false, error: 'Site required' });
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)