import { NextResponse } from 'next/server'; import fs from 'fs-extra'; import path from 'path'; import { getSpectrogramsPath } from '@/lib/utils/paths'; export async function POST(request: Request) { try { const { albumPath, upc } = await request.json(); if (!albumPath || !(await fs.pathExists(albumPath))) { return NextResponse.json({ error: 'Invalid album path' }, { status: 400 }); } // 1. Delete the album directory await fs.remove(albumPath); // 2. Delete the spectrograms directory for this album const specRoot = getSpectrogramsPath(); const albumName = path.basename(albumPath); // Try UPC first, then album name let specDir = path.join(specRoot, upc || albumName); if (await fs.pathExists(specDir)) { await fs.remove(specDir); } else if (upc && await fs.pathExists(path.join(specRoot, albumName))) { // Fallback: check album name if UPC dir didn't exist but UPC was provided await fs.remove(path.join(specRoot, albumName)); } // 3. Conditionally delete the generated torrent file if (process.env.DELETE_GENERATED_TORRENTS === 'true') { const parentDir = path.dirname(albumPath); const torrentPath = path.join(parentDir, `${albumName}.torrent`); if (await fs.pathExists(torrentPath)) { await fs.remove(torrentPath); console.log(`Deleted generated torrent: ${torrentPath}`); } } return NextResponse.json({ success: true }); } catch (error) { console.error('Error deleting album:', error); return NextResponse.json({ error: 'Failed to delete album' }, { status: 500 }); } }