import { NextRequest, NextResponse } from "next/server"; import { db } from "@/db"; import { images } from "@/db/schema"; import { eq, and } from "drizzle-orm"; import { auth } from "@/app/lib/auth"; import { unlink } from "fs/promises"; import { join } from "path"; async function getUserId(request: NextRequest): Promise { try { const session = await auth.api.getSession({ headers: request.headers, }); return session?.user?.id || null; } catch { return null; } } export async function DELETE( request: NextRequest, { params }: { params: Promise<{ id: string }> | { id: string } } ) { try { const userId = await getUserId(request); if (!userId) { return NextResponse.json( { message: "Yetkisiz erişim" }, { status: 401 } ); } // Next.js 15'te params async olabilir const resolvedParams = await Promise.resolve(params); const imageId = resolvedParams.id; // Input validation if (!imageId || typeof imageId !== "string" || imageId.length > 255) { return NextResponse.json( { message: "Geçersiz resim ID" }, { status: 400 } ); } // Resmi veritabanından bul const image = await db .select() .from(images) .where(and(eq(images.id, imageId), eq(images.userId, userId))) .limit(1); if (image.length === 0) { return NextResponse.json( { message: "Resim bulunamadı veya yetkiniz yok" }, { status: 404 } ); } const imageData = image[0]; // Dosyayı sil try { await unlink(imageData.filePath); } catch (error) { // Dosya bulunamazsa devam et (log production'da kaldırıldı) } // Veritabanından sil await db.delete(images).where(eq(images.id, imageId)); return NextResponse.json({ message: "Resim başarıyla silindi", }); } catch (error: any) { return NextResponse.json( { message: "Silme işlemi başarısız" }, { status: 500 } ); } }