From 3bd110884c29f145641ada85759b60d7570cdea5 Mon Sep 17 00:00:00 2001 From: Geert Rademakes Date: Fri, 8 Aug 2025 12:04:05 +0200 Subject: [PATCH] feat(admin): add /api/music/fix-content-types to correct MIME types; ensure sync sets proper contentType for FLAC and others --- packages/backend/src/routes/music.ts | 38 ++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/packages/backend/src/routes/music.ts b/packages/backend/src/routes/music.ts index c7627c8..118452d 100644 --- a/packages/backend/src/routes/music.ts +++ b/packages/backend/src/routes/music.ts @@ -416,4 +416,42 @@ router.post('/fix-orphaned', async (req, res) => { } }); +/** + * Fix incorrect or missing content types for existing MusicFile documents + */ +router.post('/fix-content-types', async (req, res) => { + try { + const guessContentType = (fileName: string): string => { + const ext = (fileName.split('.').pop() || '').toLowerCase(); + switch (ext) { + case 'mp3': return 'audio/mpeg'; + case 'wav': return 'audio/wav'; + case 'flac': return 'audio/flac'; + case 'm4a': return 'audio/mp4'; + case 'aac': return 'audio/aac'; + case 'ogg': return 'audio/ogg'; + case 'opus': return 'audio/opus'; + case 'wma': return 'audio/x-ms-wma'; + default: return 'application/octet-stream'; + } + }; + + const files = await MusicFile.find({}); + let updated = 0; + for (const mf of files) { + const expected = guessContentType(mf.originalName || mf.s3Key); + if (!mf.contentType || mf.contentType !== expected) { + mf.contentType = expected; + await mf.save(); + updated++; + } + } + + res.json({ message: 'Content types fixed', updated }); + } catch (error) { + console.error('Error fixing content types:', error); + res.status(500).json({ message: 'Error fixing content types', error }); + } +}); + export { router as musicRouter }; \ No newline at end of file