43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { prisma } from '@/lib/db';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function GET() {
|
|
try {
|
|
const songs = await prisma.mediaItem.findMany({
|
|
where: {
|
|
mediaType: 'AUDIO',
|
|
published: true,
|
|
},
|
|
orderBy: {
|
|
filename: 'asc', // Or sortOrder
|
|
},
|
|
});
|
|
|
|
const mediaRoot = process.env.MEDIA_ROOT || path.join(process.cwd(), 'media');
|
|
|
|
const result = songs.map((song) => {
|
|
const title = song.filename.replace(/\.[^/.]+$/, "");
|
|
|
|
const txtPath = path.join(mediaRoot, song.directory, title + '.txt');
|
|
const hasLyrics = fs.existsSync(txtPath);
|
|
|
|
return {
|
|
id: song.id,
|
|
title,
|
|
relativePath: song.relativePath,
|
|
duration: song.duration,
|
|
hasLyrics
|
|
};
|
|
});
|
|
|
|
return NextResponse.json(result);
|
|
} catch (error) {
|
|
console.error('Error fetching music:', error);
|
|
return NextResponse.json({ error: 'Internal Server Error' }, { status: 500 });
|
|
}
|
|
}
|