first commit
This commit is contained in:
80
app/api/v1/images/[id]/route.ts
Normal file
80
app/api/v1/images/[id]/route.ts
Normal file
@@ -0,0 +1,80 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { authenticateAPIRequest } from "@/app/lib/api-auth";
|
||||
import { hasPermission, PERMISSIONS } from "@/app/lib/permissions";
|
||||
import { db } from "@/db";
|
||||
import { images } from "@/db/schema";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
import { deleteFromR2 } from "@/app/lib/r2-storage";
|
||||
|
||||
/**
|
||||
* DELETE /api/v1/images/[id]
|
||||
* Resim sil
|
||||
* Kullanıcılar sadece kendi resimlerini silebilir
|
||||
* Moderator ve adminler herhangi bir resmi silebilir
|
||||
*
|
||||
* Headers:
|
||||
* - Authorization: Bearer <jwt_token>
|
||||
*/
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: Promise<{ id: string }> }
|
||||
) {
|
||||
const auth = await authenticateAPIRequest(request);
|
||||
|
||||
if (!auth.authenticated) {
|
||||
return NextResponse.json({ error: auth.error }, { status: 401 });
|
||||
}
|
||||
|
||||
try {
|
||||
const { id } = await params;
|
||||
|
||||
// Permission kontrolü - moderator ve admin herhangi bir resmi silebilir
|
||||
const canDeleteAny = hasPermission(auth.role!, PERMISSIONS.IMAGE_DELETE_ANY);
|
||||
|
||||
// Resmi bul
|
||||
const imageRecords = await db
|
||||
.select()
|
||||
.from(images)
|
||||
.where(eq(images.id, id))
|
||||
.limit(1);
|
||||
|
||||
if (imageRecords.length === 0) {
|
||||
return NextResponse.json(
|
||||
{ error: "Resim bulunamadı" },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
const image = imageRecords[0];
|
||||
|
||||
// Yetki kontrolü - kendi resmi değilse ve delete any yetkisi yoksa reddedilir
|
||||
if (!canDeleteAny && image.userId !== auth.userId) {
|
||||
return NextResponse.json(
|
||||
{ error: "Bu resmi silme yetkiniz yok" },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
// R2'den dosyayı sil
|
||||
try {
|
||||
await deleteFromR2(image.fileName);
|
||||
} catch (fileError) {
|
||||
console.error("R2'den silme hatası:", fileError);
|
||||
// Devam et, veritabanından sil
|
||||
}
|
||||
|
||||
// Veritabanından sil
|
||||
await db.delete(images).where(eq(images.id, id));
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: "Resim başarıyla silindi",
|
||||
});
|
||||
} catch (error: any) {
|
||||
console.error("API - Resim silme hatası:", error);
|
||||
return NextResponse.json(
|
||||
{ error: "Resim silinemedi" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user