From 042a6ae0215f05fc54936f711f2f931fd95fe45c Mon Sep 17 00:00:00 2001 From: apatil Date: Wed, 7 May 2025 21:12:08 +0100 Subject: [PATCH] Make API route accept all music files --- src/app/api/tracks/route.ts | 9 ++------- src/app/api/upload/route.ts | 8 ++++++-- src/app/music/page.tsx | 6 ++++-- src/lib/music.ts | 4 ++++ 4 files changed, 16 insertions(+), 11 deletions(-) create mode 100644 src/lib/music.ts diff --git a/src/app/api/tracks/route.ts b/src/app/api/tracks/route.ts index d0a4d2b..082ff40 100644 --- a/src/app/api/tracks/route.ts +++ b/src/app/api/tracks/route.ts @@ -1,14 +1,9 @@ +import { checkIfMusicFile } from '@/lib/music'; import fs from 'fs'; import { NextResponse } from 'next/server'; const MUSIC_DIR = '/app/music'; export async function GET() { - const files = fs - .readdirSync(MUSIC_DIR) - .filter((f) => - ['.mp3', '.m4a', 'audio/mpeg', 'audio/mp4', 'audio/x-m4a'].some((suffix) => - f.endsWith(suffix), - ), - ); + const files = fs.readdirSync(MUSIC_DIR).filter((f) => checkIfMusicFile(f)); return NextResponse.json(files); } diff --git a/src/app/api/upload/route.ts b/src/app/api/upload/route.ts index e9c5045..fcf4948 100644 --- a/src/app/api/upload/route.ts +++ b/src/app/api/upload/route.ts @@ -1,14 +1,18 @@ import fs from 'fs'; import path from 'path'; import { NextRequest, NextResponse } from 'next/server'; +import { checkIfMusicFile } from '@/lib/music'; const MUSIC_DIR = '/app/music'; export async function POST(req: NextRequest) { const formData = await req.formData(); const file = formData.get('file') as File; - if (!file || !file.name.endsWith('.mp3')) { - return NextResponse.json({ error: 'Only .mp3 files are allowed.' }, { status: 400 }); + if (checkIfMusicFile(file.name)) { + return NextResponse.json( + { error: 'Only music files are allowed to be uploaded' }, + { status: 400 }, + ); } const arrayBuffer = await file.arrayBuffer(); const buffer = Buffer.from(arrayBuffer); diff --git a/src/app/music/page.tsx b/src/app/music/page.tsx index 0918859..e94133c 100644 --- a/src/app/music/page.tsx +++ b/src/app/music/page.tsx @@ -34,6 +34,8 @@ export default function Home() { setUploading(false); if (res.ok) { fetchTracks(); + } else { + console.error(res.status, res.text()) } }; @@ -43,9 +45,9 @@ export default function Home() { Music Player -