source: Klonkt/src/routes/admin-audio.js@ 3487567

main
Last change on this file since 3487567 was 3487567, checked in by Robin Genis <roboburr@…>, 2 months ago

fix(i18n): translate admin page titles (pageTitleKey instead of hardcoded strings)

Admin page/tab titles were hardcoded (mostly Dutch: Beheer, Instellingen, Nieuwsbrief, …).
renderPage now accepts pageTitleKey (+ pageTitleVars) and translates it with the resolved
language; the 16 admin routes pass keys. Adds admin.t_* keys in nl/en/de.

  • middleware/render.js — pageTitleKey support
  • services/i18n.js — admin.t_* title keys (nl/en/de)
  • routes/admin*.js — pageTitle string -> pageTitleKey
  • Property mode set to 100644
File size: 24.0 KB
Line 
1/**
2 * Admin: Audio Tracks management — Phase C MP3 player.
3 *
4 * GET /admin/audio -> list site tracks + upload form
5 * POST /admin/audio/upload -> multer upload, insert media + audio_tracks
6 * POST /admin/audio/:id/delete -> remove track row + file on disk
7 *
8 * Files land in storage/media/audio/ (NOT served by /media static handler —
9 * everything goes through the signed /audio/stream/ route).
10 */
11
12import express from 'express';
13import multer from 'multer';
14import path from 'path';
15import fs from 'fs';
16import { fileURLToPath } from 'url';
17import { v4 as uuid } from 'uuid';
18import db from '../config/database.js';
19import { renderPage } from '../middleware/render.js';
20import { toWebp } from '../services/ImageWebpService.js';
21import { requireGod } from '../middleware/auth.js';
22import { transcodeToMp3, retagMp3 } from '../services/AudioTranscoder.js';
23import { audioUrl } from '../services/AudioStreamService.js';
24
25const __dirname = path.dirname(fileURLToPath(import.meta.url));
26// Audio files live OUTSIDE storage/media so the public /media static
27// handler can't serve them — they must go through the signed /audio/stream/
28// endpoint (anti-hotlink). Covers are public and stay in /media.
29const AUDIO_DIR = path.resolve(
30 process.env.AUDIO_PATH || path.join(__dirname, '..', '..', 'storage', 'audio')
31);
32const COVER_DIR = path.resolve(
33 process.env.COVER_PATH || path.join(__dirname, '..', '..', 'storage', 'media', 'audio-covers')
34);
35fs.mkdirSync(AUDIO_DIR, { recursive: true });
36fs.mkdirSync(COVER_DIR, { recursive: true });
37
38const ALLOWED_AUDIO_EXT = new Set(['.mp3', '.m4a', '.mp4', '.aac', '.oga', '.ogg', '.opus', '.flac', '.wav', '.webm']);
39const ALLOWED_COVER_EXT = new Set(['.jpg', '.jpeg', '.png', '.webp', '.gif']);
40const MAX_AUDIO_BYTES = 50 * 1024 * 1024; // 50 MB — compressed formats (mp3/m4a/ogg/…)
41const MAX_WAV_BYTES = 100 * 1024 * 1024; // 100 MB — WAV is uncompressed, so a higher limit
42const MAX_COVER_BYTES = 5 * 1024 * 1024; // 5 MB
43
44// Per-file upper limit based on extension. multer's global limit is the
45// highest (WAV); the real per-type check happens in the upload handler.
46const audioByteLimitFor = (ext) => (ext.toLowerCase() === '.wav' ? MAX_WAV_BYTES : MAX_AUDIO_BYTES);
47
48// Multer routes audio + cover into separate dirs based on field name.
49const storage = multer.diskStorage({
50 destination: (req, file, cb) => {
51 cb(null, file.fieldname === 'cover' ? COVER_DIR : AUDIO_DIR);
52 },
53 filename: (req, file, cb) => {
54 const ext = path.extname(file.originalname).toLowerCase();
55 cb(null, `${uuid()}${ext}`);
56 },
57});
58
59const upload = multer({
60 storage,
61 limits: { fileSize: MAX_WAV_BYTES }, // highest upper bound (WAV) — per-type check in the handler
62 fileFilter: (req, file, cb) => {
63 const ext = path.extname(file.originalname).toLowerCase();
64 if (file.fieldname === 'cover') {
65 if (!ALLOWED_COVER_EXT.has(ext)) return cb(new Error('Cover must be jpg/png/webp/gif'));
66 } else {
67 if (!ALLOWED_AUDIO_EXT.has(ext)) return cb(new Error('Unsupported audio type: ' + ext));
68 }
69 cb(null, true);
70 },
71});
72
73const router = express.Router();
74
75// "Open in" platform links per track: only https + the correct host accepted
76// (href arrives unescaped in the view → scheme/host guard against abuse).
77const LINK_DOMAINS = {
78 spotify: ['spotify.com'],
79 youtube: ['youtube.com', 'youtu.be', 'music.youtube.com'],
80 soundcloud: ['soundcloud.com'],
81};
82function platformLink(url, domains) {
83 const u = String(url || '').trim();
84 if (!u || !/^https:\/\//i.test(u)) return null;
85 try {
86 const h = new URL(u).hostname.toLowerCase();
87 if (domains.some((d) => h === d || h.endsWith('.' + d))) return u;
88 } catch (e) { /* invalid URL */ }
89 return null;
90}
91
92router.get('/', requireGod, (req, res) => {
93 const site = res.locals.site;
94 if (!site) return res.status(404).send('Site required');
95
96 const rows = db.prepare(`
97 SELECT t.id, t.title, t.artist, t.album, t.duration, t.cover_url,
98 t.position, t.created_at, t.downloadable, m.filename, m.size, m.mime_type
99 FROM audio_tracks t
100 LEFT JOIN media m ON m.id = t.media_id
101 WHERE t.site_id = ?
102 ORDER BY t.position ASC, t.created_at ASC
103 `).all(site.id);
104
105 // Build each track's stream URL so admins can preview audio inline.
106 const tracks = rows.map(t => ({
107 ...t,
108 stream_url: t.filename ? audioUrl(t.filename) : null,
109 }));
110
111 const base = (process.env.PUBLIC_BASE_URL || ('https://' + (req.get('host') || ''))).replace(/\/$/, '');
112 const embedUrl = base + (res.locals.siteUrlBase || '') + '/embed';
113 renderPage(req, res, 'pages/admin-audio', {
114 pageTitleKey: 'admin.t_audio',
115 bodyClass: 'on-admin',
116 tracks,
117 embedUrl,
118 error: req.query.error || null,
119 success: req.query.success || null,
120 maxBytesMb: Math.round(MAX_AUDIO_BYTES / 1024 / 1024),
121 maxWavMb: Math.round(MAX_WAV_BYTES / 1024 / 1024),
122 });
123});
124
125router.post('/upload', requireGod, (req, res) => {
126 // Helper: respond appropriately to JSON-accepting callers (the bulk
127 // uploader fetch() calls) vs traditional form posts (redirect).
128 // Both code paths cover identical errors below.
129 const wantsJson = req.get('Accept')?.includes('application/json') || req.xhr;
130 const fail = (status, message) => wantsJson
131 ? res.status(status).json({ ok: false, error: message })
132 : res.redirect('/admin/audio?error=' + encodeURIComponent(message));
133 const ok = (data) => wantsJson
134 ? res.json({ ok: true, ...data })
135 : res.redirect('/admin/audio?success=' + encodeURIComponent('Uploaded: ' + data.title));
136
137 upload.fields([{ name: 'audio', maxCount: 1 }, { name: 'cover', maxCount: 1 }])(req, res, async (err) => {
138 if (err) return fail(400, err.message);
139
140 const site = res.locals.site;
141 const audioFile = req.files?.audio?.[0];
142 const coverFile = req.files?.cover?.[0];
143
144 if (!site || !audioFile) {
145 // Clean up any cover that snuck through without an audio file
146 if (coverFile) try { fs.unlinkSync(coverFile.path); } catch {}
147 return fail(400, 'missing audio file');
148 }
149
150 // Per-type audio size check. multer's global limit was the WAV upper bound
151 // (100MB); compressed formats stay at 50MB.
152 const audioExt = path.extname(audioFile.originalname).toLowerCase();
153 const audioLimit = audioByteLimitFor(audioExt);
154 if (audioFile.size > audioLimit) {
155 try { fs.unlinkSync(audioFile.path); } catch {}
156 if (coverFile) try { fs.unlinkSync(coverFile.path); } catch {}
157 return fail(400, `audio te groot (max ${Math.round(audioLimit / 1024 / 1024)}MB voor ${audioExt || 'dit type'})`);
158 }
159
160 // Cover size check (multer's global limit was the audio upper bound)
161 if (coverFile && coverFile.size > MAX_COVER_BYTES) {
162 try { fs.unlinkSync(audioFile.path); } catch {}
163 try { fs.unlinkSync(coverFile.path); } catch {}
164 return fail(400, 'cover too large (max 5MB)');
165 }
166
167 const { title, artist, album } = req.body;
168 const trackId = uuid();
169 const mediaId = uuid();
170 const coverUrl = coverFile ? `/media/audio-covers/${coverFile.filename}` : null;
171
172 // ── TRANSCODE ────────────────────────────────────────────────
173 // Convert whatever the user uploaded to a uniform 192kbps stereo mp3.
174 // The original file (whatever its format) is deleted on success.
175 // multer named the upload <uuid>.<ext>; we re-use that uuid stem so
176 // the final file is just <uuid>.mp3, keeping things tidy.
177 const inputBaseName = path.basename(audioFile.filename, path.extname(audioFile.filename));
178 // Title fallback strategy:
179 // 1. Explicit `title` form field (single-upload form)
180 // 2. Original filename minus extension, with underscores → spaces
181 // (cleans up "Track_01_-_Title.mp3" patterns common from CD rips)
182 const fallbackTitle = path.basename(audioFile.originalname, path.extname(audioFile.originalname))
183 .replace(/_/g, ' ').trim();
184 const finalTitle = title?.trim() || fallbackTitle;
185 const finalArtist = artist?.trim() || null;
186 const finalAlbum = album?.trim() || null;
187 // Ownership/licence. credit falls back to the artist; these go both into the
188 // DB and into the ID3 tags of the mp3 (copyright + comment).
189 const finalCredit = (req.body.credit || '').trim() || finalArtist || null;
190 const finalLicense = (req.body.license || '').trim() || null;
191 const finalLinkSpotify = platformLink(req.body.link_spotify, LINK_DOMAINS.spotify);
192 const finalLinkYoutube = platformLink(req.body.link_youtube, LINK_DOMAINS.youtube);
193 const finalLinkSoundcloud = platformLink(req.body.link_soundcloud, LINK_DOMAINS.soundcloud);
194
195 console.log('[admin-audio] upload received:', {
196 original: audioFile.originalname,
197 tempPath: audioFile.path,
198 size: audioFile.size,
199 hasC: !!coverFile,
200 });
201
202 let transcoded;
203 try {
204 transcoded = await transcodeToMp3({
205 inputPath: audioFile.path,
206 outputDir: AUDIO_DIR,
207 outputBaseName: inputBaseName,
208 tags: {
209 title: finalTitle,
210 artist: finalArtist || undefined,
211 album: finalAlbum || undefined,
212 copyright: finalCredit || undefined,
213 comment: finalLicense || undefined,
214 },
215 });
216 console.log('[admin-audio] transcode OK:', transcoded);
217 } catch (transcodeErr) {
218 console.error('[admin-audio] Transcode failed:', transcodeErr);
219 // Transcoder kept the original on failure — clean it up ourselves
220 // since the upload as a whole has failed.
221 try { fs.unlinkSync(audioFile.path); } catch {}
222 if (coverFile) try { fs.unlinkSync(coverFile.path); } catch {}
223 return fail(500, 'Conversie mislukt: ' + transcodeErr.message);
224 }
225
226 try {
227 console.log('[admin-audio] inserting media row');
228 db.prepare(`
229 INSERT INTO media (id, site_id, filename, mime_type, size, storage_path)
230 VALUES (?, ?, ?, ?, ?, ?)
231 `).run(mediaId, site.id, transcoded.filename, transcoded.mimeType, transcoded.size, transcoded.path);
232
233 // Duration automatically: primarily from the transcode (ffmpeg codecData), then
234 // an optional client-side value (bulk uploader reads <audio>.duration),
235 // otherwise NULL (UI then shows '—:—', editable manually in the editor).
236 const clientDur = req.body.duration != null ? parseInt(req.body.duration, 10) : NaN;
237 const finalDuration =
238 (transcoded.durationSec != null && transcoded.durationSec > 0) ? transcoded.durationSec
239 : (Number.isFinite(clientDur) && clientDur > 0) ? clientDur
240 : null;
241
242 console.log('[admin-audio] inserting audio_tracks row (duration=' + finalDuration + ')');
243 db.prepare(`
244 INSERT INTO audio_tracks (id, site_id, title, artist, album, duration, cover_url, credit, license, link_spotify, link_youtube, link_soundcloud, media_id, position)
245 VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, COALESCE(
246 (SELECT MAX(position) + 1 FROM audio_tracks WHERE site_id = ?),
247 0
248 ))
249 `).run(
250 trackId, site.id,
251 finalTitle, finalArtist, finalAlbum,
252 finalDuration,
253 coverUrl,
254 finalCredit, finalLicense,
255 finalLinkSpotify, finalLinkYoutube, finalLinkSoundcloud,
256 mediaId, site.id
257 );
258 console.log('[admin-audio] DB inserts OK — track', trackId);
259 } catch (dbErr) {
260 console.error('[admin-audio] DB insert failed:', dbErr);
261 // DB failed — clean up the transcoded mp3 so we don't leak files
262 try { fs.unlinkSync(transcoded.path); } catch {}
263 if (coverFile) try { fs.unlinkSync(coverFile.path); } catch {}
264 return fail(500, dbErr.message);
265 }
266
267 return ok({
268 id: trackId,
269 title: finalTitle,
270 artist: finalArtist,
271 album: finalAlbum,
272 size: transcoded.size,
273 });
274 });
275});
276
277// Download-for-email per track on/off (premium #2). No-JS toggle from the
278// audio admin list → flip + back.
279router.post('/:id/downloadable', requireGod, (req, res) => {
280 const site = res.locals.site;
281 if (!site) return res.status(404).send('Site required');
282 const row = db.prepare('SELECT downloadable FROM audio_tracks WHERE id = ? AND site_id = ?').get(req.params.id, site.id);
283 if (row) {
284 db.prepare('UPDATE audio_tracks SET downloadable = ? WHERE id = ? AND site_id = ?')
285 .run(row.downloadable ? 0 : 1, req.params.id, site.id);
286 }
287 res.redirect('/admin/audio');
288});
289
290router.post('/:id/delete', requireGod, (req, res) => {
291 const site = res.locals.site;
292 if (!site) return res.status(404).send('Site required');
293
294 const track = db.prepare(`
295 SELECT t.id AS track_id, m.id AS media_id, m.storage_path
296 FROM audio_tracks t LEFT JOIN media m ON m.id = t.media_id
297 WHERE t.id = ? AND t.site_id = ?
298 `).get(req.params.id, site.id);
299
300 if (!track) return res.redirect('/admin/audio?error=Not+found');
301
302 db.prepare('DELETE FROM audio_tracks WHERE id = ?').run(track.track_id);
303 if (track.media_id) {
304 db.prepare('DELETE FROM media WHERE id = ?').run(track.media_id);
305 }
306 if (track.storage_path) {
307 try { fs.unlinkSync(track.storage_path); } catch {}
308 }
309 res.redirect('/admin/audio?success=Deleted');
310});
311
312// ─── Orphan cleanup: rows whose file is missing on disk ───────────
313//
314// Two-phase to prevent accidental data loss:
315// GET /admin/audio/cleanup → dry-run report (no changes, JSON list)
316// POST /admin/audio/cleanup → actually deletes the orphan rows
317//
318// "Orphan" = an audio_tracks row whose media_id either points nowhere or
319// points to a media row whose storage_path file doesn't exist on disk.
320// This is the recovery path when DB and disk drift apart (e.g. AUDIO_PATH
321// changed between uploads, disk was wiped, or migration left stragglers).
322function findOrphans(siteId) {
323 const rows = db.prepare(`
324 SELECT t.id AS track_id, t.title, t.artist, t.album,
325 m.id AS media_id, m.storage_path
326 FROM audio_tracks t
327 LEFT JOIN media m ON m.id = t.media_id
328 WHERE t.site_id = ?
329 `).all(siteId);
330 const orphans = [];
331 for (const r of rows) {
332 if (!r.storage_path) {
333 orphans.push({ ...r, reason: 'no media row' });
334 continue;
335 }
336 try { fs.statSync(r.storage_path); }
337 catch { orphans.push({ ...r, reason: 'file missing on disk' }); }
338 }
339 return { total: rows.length, orphans };
340}
341
342router.get('/cleanup', requireGod, (req, res) => {
343 const site = res.locals.site;
344 if (!site) return res.status(404).json({ error: 'Site required' });
345 const result = findOrphans(site.id);
346 res.json({
347 ok: true,
348 siteId: site.id,
349 totalTracks: result.total,
350 orphanCount: result.orphans.length,
351 orphans: result.orphans.map(o => ({
352 track_id: o.track_id,
353 title: o.title || '(zonder titel)',
354 artist: o.artist || '—',
355 reason: o.reason,
356 storage_path: o.storage_path || null,
357 })),
358 note: 'POST to this same URL to actually delete these rows.',
359 });
360});
361
362router.post('/cleanup', requireGod, (req, res) => {
363 const site = res.locals.site;
364 if (!site) return res.status(404).json({ error: 'Site required' });
365 const { orphans } = findOrphans(site.id);
366
367 // Wrap in a transaction so a partial failure doesn't leave half-deleted state
368 const deleteOne = db.transaction((o) => {
369 db.prepare('DELETE FROM audio_tracks WHERE id = ?').run(o.track_id);
370 if (o.media_id) db.prepare('DELETE FROM media WHERE id = ?').run(o.media_id);
371 });
372 for (const o of orphans) deleteOne(o);
373
374 res.json({ ok: true, deleted: orphans.length });
375});
376
377
378//
379// All write endpoints expect to be hit by the track-editor modal which
380// sends X-CSRF-Token and JSON. They return { ok: true, ... } on success
381// or { error: '...' } with a 4xx status on failure.
382
383/** GET /admin/audio/api/albums — distinct list of album names (for datalist) */
384router.get('/api/albums', requireGod, (req, res) => {
385 const site = res.locals.site;
386 if (!site) return res.status(404).json({ error: 'Site required' });
387 const rows = db.prepare(`
388 SELECT DISTINCT album FROM audio_tracks
389 WHERE site_id = ? AND album IS NOT NULL AND album != ''
390 ORDER BY album COLLATE NOCASE
391 `).all(site.id);
392 res.json({ ok: true, albums: rows.map(r => r.album) });
393});
394
395/** GET /admin/audio/api/:id — single track with all metadata */
396// Create a track WITHOUT an audio file (title + open-in links only). Appears
397// in albums/playlists in the list, with open-in icons but no play button.
398router.post('/create-link', requireGod, express.json(), (req, res) => {
399 const site = res.locals.site;
400 if (!site) return res.status(404).json({ error: 'Site required' });
401 const trackId = uuid();
402 const title = ((req.body && req.body.title) || 'Nieuwe track').toString().trim().slice(0, 200) || 'Nieuwe track';
403 try {
404 db.prepare(`
405 INSERT INTO audio_tracks (id, site_id, title, media_id, position)
406 VALUES (?, ?, ?, NULL, COALESCE((SELECT MAX(position) + 1 FROM audio_tracks WHERE site_id = ?), 0))
407 `).run(trackId, site.id, title, site.id);
408 } catch (e) {
409 return res.status(500).json({ error: e.message });
410 }
411 res.json({ ok: true, id: trackId });
412});
413
414router.get('/api/:id', requireGod, (req, res) => {
415 const site = res.locals.site;
416 if (!site) return res.status(404).json({ error: 'Site required' });
417 const t = db.prepare(`
418 SELECT t.id, t.title, t.artist, t.album, t.duration, t.cover_url,
419 t.credit, t.license, t.link_spotify, t.link_youtube, t.link_soundcloud,
420 t.position, t.created_at, m.filename
421 FROM audio_tracks t LEFT JOIN media m ON m.id = t.media_id
422 WHERE t.id = ? AND t.site_id = ?
423 `).get(req.params.id, site.id);
424 if (!t) return res.status(404).json({ error: 'Track niet gevonden' });
425 // Stream URL so the modal can render an inline preview player.
426 const stream_url = t.filename ? audioUrl(t.filename) : null;
427 res.json({ ok: true, track: { ...t, stream_url } });
428});
429
430/**
431 * POST /admin/audio/api/:id — update track metadata.
432 * Accepts JSON body with any subset of: title, artist, album, duration, cover_url.
433 * `title` is required if present (can't be blanked). Empty strings on optional
434 * fields are stored as NULL so the audio embed renderer's `t.artist || ''`
435 * fallback keeps working.
436 */
437router.post('/api/:id', requireGod, express.json(), async (req, res) => {
438 const site = res.locals.site;
439 if (!site) return res.status(404).json({ error: 'Site required' });
440
441 const exists = db.prepare(
442 'SELECT id FROM audio_tracks WHERE id = ? AND site_id = ?'
443 ).get(req.params.id, site.id);
444 if (!exists) return res.status(404).json({ error: 'Track niet gevonden' });
445
446 const fields = [];
447 const values = [];
448 const body = req.body || {};
449
450 if (Object.prototype.hasOwnProperty.call(body, 'title')) {
451 const v = String(body.title || '').trim();
452 if (!v) return res.status(400).json({ error: 'Titel is verplicht' });
453 fields.push('title = ?'); values.push(v);
454 }
455 if (Object.prototype.hasOwnProperty.call(body, 'artist')) {
456 fields.push('artist = ?'); values.push(String(body.artist || '').trim() || null);
457 }
458 if (Object.prototype.hasOwnProperty.call(body, 'album')) {
459 fields.push('album = ?'); values.push(String(body.album || '').trim() || null);
460 }
461 if (Object.prototype.hasOwnProperty.call(body, 'duration')) {
462 const d = parseInt(body.duration, 10);
463 fields.push('duration = ?');
464 values.push(Number.isFinite(d) && d > 0 ? d : null);
465 }
466 if (Object.prototype.hasOwnProperty.call(body, 'cover_url')) {
467 // Accept either a /media/... path or an absolute https URL.
468 // Anything else (javascript:, data:, etc) gets blanked for safety.
469 const raw = String(body.cover_url || '').trim();
470 let safe = null;
471 if (raw === '') {
472 safe = null;
473 } else if (raw.startsWith('/media/') || raw.startsWith('https://') || raw.startsWith('http://')) {
474 safe = raw;
475 }
476 fields.push('cover_url = ?'); values.push(safe);
477 }
478
479 if (Object.prototype.hasOwnProperty.call(body, 'downloadable')) {
480 fields.push('downloadable = ?'); values.push(body.downloadable ? 1 : 0);
481 }
482 if (Object.prototype.hasOwnProperty.call(body, 'credit')) {
483 fields.push('credit = ?'); values.push(String(body.credit || '').trim() || null);
484 }
485 if (Object.prototype.hasOwnProperty.call(body, 'license')) {
486 fields.push('license = ?'); values.push(String(body.license || '').trim() || null);
487 }
488 if (Object.prototype.hasOwnProperty.call(body, 'link_spotify')) {
489 fields.push('link_spotify = ?'); values.push(platformLink(body.link_spotify, LINK_DOMAINS.spotify));
490 }
491 if (Object.prototype.hasOwnProperty.call(body, 'link_youtube')) {
492 fields.push('link_youtube = ?'); values.push(platformLink(body.link_youtube, LINK_DOMAINS.youtube));
493 }
494 if (Object.prototype.hasOwnProperty.call(body, 'link_soundcloud')) {
495 fields.push('link_soundcloud = ?'); values.push(platformLink(body.link_soundcloud, LINK_DOMAINS.soundcloud));
496 }
497
498 if (fields.length === 0) {
499 return res.status(400).json({ error: 'Niks om te updaten' });
500 }
501
502 try {
503 db.prepare(`UPDATE audio_tracks SET ${fields.join(', ')} WHERE id = ? AND site_id = ?`)
504 .run(...values, req.params.id, site.id);
505 } catch (err) {
506 return res.status(500).json({ error: err.message });
507 }
508
509 // Fresh row + (if tag fields changed) retag the mp3, so that the owner/
510 // licence is also IN the file (ID3) and travels with it on download.
511 const fresh = db.prepare(`
512 SELECT t.id, t.title, t.artist, t.album, t.duration, t.cover_url, t.credit, t.license, m.storage_path
513 FROM audio_tracks t LEFT JOIN media m ON m.id = t.media_id
514 WHERE t.id = ? AND t.site_id = ?
515 `).get(req.params.id, site.id);
516
517 const tagsChanged = ['title', 'artist', 'album', 'credit', 'license']
518 .some((f) => Object.prototype.hasOwnProperty.call(body, f));
519 if (fresh && fresh.storage_path && tagsChanged) {
520 try {
521 await retagMp3({ filePath: fresh.storage_path, tags: {
522 title: fresh.title || undefined,
523 artist: fresh.artist || undefined,
524 album: fresh.album || undefined,
525 copyright: fresh.credit || undefined,
526 comment: fresh.license || undefined,
527 } });
528 } catch (e) {
529 console.warn('[admin-audio] ID3 retag failed (DB was still updated):', e.message);
530 }
531 }
532 const { storage_path, ...trackOut } = fresh || {};
533 res.json({ ok: true, track: trackOut });
534});
535
536/**
537 * POST /admin/audio/api/:id/cover — upload a new cover image and set it on
538 * the track in one go. Returns { ok, url } so the modal can preview.
539 *
540 * Reuses the same multer config as the upload form (5MB limit, jpg/png/webp/gif).
541 * If the track already had a cover stored under /media/audio-covers/, the old
542 * file is deleted to avoid orphaned bytes piling up.
543 */
544router.post('/api/:id/cover', requireGod, (req, res) => {
545 const site = res.locals.site;
546 if (!site) return res.status(404).json({ error: 'Site required' });
547
548 const exists = db.prepare(
549 'SELECT id, cover_url FROM audio_tracks WHERE id = ? AND site_id = ?'
550 ).get(req.params.id, site.id);
551 if (!exists) return res.status(404).json({ error: 'Track niet gevonden' });
552
553 upload.single('cover')(req, res, (err) => {
554 if (err) return res.status(400).json({ error: err.message });
555 const file = req.file;
556 if (!file) return res.status(400).json({ error: 'Geen bestand' });
557 if (file.size > MAX_COVER_BYTES) {
558 try { fs.unlinkSync(file.path); } catch {}
559 return res.status(413).json({ error: 'Te groot (max 5 MB)' });
560 }
561
562 const newUrl = `/media/audio-covers/${toWebp(file)}`;
563 try {
564 db.prepare('UPDATE audio_tracks SET cover_url = ? WHERE id = ? AND site_id = ?')
565 .run(newUrl, req.params.id, site.id);
566 } catch (dbErr) {
567 try { fs.unlinkSync(file.path); } catch {}
568 return res.status(500).json({ error: dbErr.message });
569 }
570
571 // Clean up the previous cover if it lived in our covers dir
572 if (exists.cover_url && exists.cover_url.startsWith('/media/audio-covers/')) {
573 const oldName = exists.cover_url.replace(/^\/media\/audio-covers\//, '');
574 const oldPath = path.join(COVER_DIR, oldName);
575 try { fs.unlinkSync(oldPath); } catch {}
576 }
577
578 // Return both keys so any caller using j.url OR j.cover_url works.
579 // Frontend (track-editor.ejs) reads j.cover_url — keep this in sync.
580 res.json({ ok: true, url: newUrl, cover_url: newUrl });
581 });
582});
583
584export default router;
Note: See TracBrowser for help on using the repository browser.