Files
v2.app.gaertan.art/src/app/api/image/[...key]/route.ts
2026-01-31 16:04:29 +01:00

34 lines
962 B
TypeScript

import { s3 } from "@/lib/s3";
import { GetObjectCommand } from "@aws-sdk/client-s3";
import type { NextRequest } from "next/server";
export async function GET(_req: NextRequest, context: { params: Promise<{ key: string[] }> }) {
const { key } = await context.params;
const s3Key = key.join("/");
try {
const command = new GetObjectCommand({
Bucket: `${process.env.BUCKET_NAME}`,
Key: s3Key,
});
const response = await s3.send(command);
if (!response.Body) {
return new Response("No body", { status: 500 });
}
const contentType = response.ContentType ?? "application/octet-stream";
return new Response(response.Body as ReadableStream, {
headers: {
"Content-Type": contentType,
"Cache-Control": "public, max-age=3600",
"Content-Disposition": "inline",
},
});
} catch (err) {
console.log(err)
return new Response("Image not found", { status: 404 });
}
}