feat(admin): add /api/music/fix-content-types to correct MIME types; ensure sync sets proper contentType for FLAC and others
This commit is contained in:
parent
5144df2e93
commit
3bd110884c
@ -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 };
|
||||
Loading…
x
Reference in New Issue
Block a user