first commit
This commit is contained in:
53
app/api/images/route.ts
Normal file
53
app/api/images/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { db } from "@/db";
|
||||
import { images } from "@/db/schema";
|
||||
import { eq, desc } from "drizzle-orm";
|
||||
import { auth } from "@/app/lib/auth";
|
||||
|
||||
async function getUserId(request: NextRequest): Promise<string | null> {
|
||||
try {
|
||||
const session = await auth.api.getSession({
|
||||
headers: request.headers,
|
||||
});
|
||||
return session?.user?.id || null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const userId = await getUserId(request);
|
||||
if (!userId) {
|
||||
return NextResponse.json(
|
||||
{ message: "Yetkisiz erişim" },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
const userImages = await db
|
||||
.select()
|
||||
.from(images)
|
||||
.where(eq(images.userId, userId))
|
||||
.orderBy(desc(images.createdAt));
|
||||
|
||||
return NextResponse.json({
|
||||
images: userImages.map((img) => ({
|
||||
id: img.id,
|
||||
originalName: img.originalName,
|
||||
url: img.url, // R2 URL'leri zaten tam URL olarak kaydedildi
|
||||
width: img.width,
|
||||
height: img.height,
|
||||
quality: img.quality,
|
||||
format: img.format,
|
||||
fileSize: img.fileSize,
|
||||
createdAt: img.createdAt.toISOString(),
|
||||
})),
|
||||
});
|
||||
} catch (error: any) {
|
||||
return NextResponse.json(
|
||||
{ message: "Resimler yüklenemedi" },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user