feat: MUSIC PLAYER!
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
// app/api/music/tracks/route.ts
|
||||
import { NextResponse } from 'next/server';
|
||||
import { readdir } from 'fs/promises';
|
||||
import { join } from 'path';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
|
||||
interface Track {
|
||||
title: string;
|
||||
fileName: string;
|
||||
artist: string;
|
||||
theme: string;
|
||||
}
|
||||
|
||||
function parseFileName(fileName: string): Track {
|
||||
// Remove file extension
|
||||
const nameWithoutExt = fileName.replace(/\.(mp4|mp3|wav|ogg)$/i, '');
|
||||
|
||||
// Parse format: [music_name]_[theme/genre].[format]
|
||||
const parts = nameWithoutExt.split('_');
|
||||
|
||||
if (parts.length >= 2) {
|
||||
const theme = parts[parts.length - 1].toLowerCase();
|
||||
const title = parts.slice(0, -1).join(' ');
|
||||
|
||||
return {
|
||||
title: title.replace(/-/g, ' ').replace(/\b\w/g, l => l.toUpperCase()),
|
||||
fileName: fileName,
|
||||
artist: theme.charAt(0).toUpperCase() + theme.slice(1) + ' Collection',
|
||||
theme: theme
|
||||
};
|
||||
}
|
||||
|
||||
// Fallback for files that don't follow the naming convention
|
||||
return {
|
||||
title: nameWithoutExt.replace(/-|_/g, ' ').replace(/\b\w/g, l => l.toUpperCase()),
|
||||
fileName: fileName,
|
||||
artist: 'Unknown Artist',
|
||||
theme: 'general'
|
||||
};
|
||||
}
|
||||
|
||||
export async function GET() {
|
||||
try {
|
||||
// Path to your music directory
|
||||
const musicDir = join(process.cwd(), 'public', 'files', 'music');
|
||||
|
||||
// Read all files in the directory
|
||||
const files = await readdir(musicDir);
|
||||
|
||||
// Filter for audio/video files and parse them
|
||||
const tracks = files
|
||||
.filter(file => /\.(mp4|mp3|wav|ogg)$/i.test(file))
|
||||
.map(file => parseFileName(file))
|
||||
.sort((a, b) => {
|
||||
// Sort by theme first, then by title
|
||||
if (a.theme !== b.theme) {
|
||||
return a.theme.localeCompare(b.theme);
|
||||
}
|
||||
return a.title.localeCompare(b.title);
|
||||
});
|
||||
|
||||
return NextResponse.json({ tracks });
|
||||
} catch (error) {
|
||||
console.error('Error reading music directory:', error);
|
||||
|
||||
// Return fallback tracks if directory reading fails
|
||||
return NextResponse.json({
|
||||
tracks: [
|
||||
]
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user