fix: fix multer syntax error and unify upload routes

This commit is contained in:
2026-05-26 10:47:12 +07:00
parent 2719cd4a6f
commit 401a0d51a6
3 changed files with 13 additions and 5 deletions
-2
View File
@@ -1,6 +1,5 @@
import { Router } from 'express';
import authRoutes from './auths.js';
import adminRoutes from './admins.js';
import trackRoutes from './tracks.js';
import playlistRoutes from './playlists.js';
import artistRoutes from './artists.js';
@@ -20,7 +19,6 @@ router.get('/health', (req, res) => {
});
router.use('/auth', authRoutes);
router.use('/admin', adminRoutes);
router.use('/tracks', trackRoutes);
router.use('/playlists', playlistRoutes);
router.use('/artists', artistRoutes);
+11
View File
@@ -1,11 +1,22 @@
import { Router } from 'express';
import { trackController } from '../controllers/trackController.js';
import { uploadController } from '../controllers/uploadController.js';
import { auth } from '../middlewares/auth.js';
import { uploadTrackAssets } from '../utils/multer.js';
const router = Router();
router.get('/', trackController.list);
router.get('/liked', auth, trackController.listLiked);
router.post(
'/',
auth,
uploadTrackAssets.fields([
{ name: 'cover', maxCount: 1 },
{ name: 'audio', maxCount: 1 },
]),
uploadController.uploadTrack
);
router.post('/:trackId/like', auth, trackController.like);
router.delete('/:trackId/like', auth, trackController.unlike);
+2 -3
View File
@@ -75,7 +75,7 @@ const imageFilter = (req, file, cb) => {
return cb(null, true);
}
if (file.fieldname === 'playlistCover') {
if (file.fieldname === 'playlistCover' || file.fieldname === 'avatar') {
const allowed = ['image/jpeg', 'image/png', 'image/webp'];
if (!allowed.includes(file.mimetype)) {
return cb(new Error('Only JPG, PNG, or WEBP images are allowed'));
@@ -84,8 +84,7 @@ const imageFilter = (req, file, cb) => {
return cb(null, true);
}
return cb(new Error('Invalid upload field'));
};
return cb(new Error('Invalid upload field'));
};
export const uploadAvatar = multer({