import { s3 } from "@/lib/s3";
import { GetObjectCommand } from "@aws-sdk/client-s3";

export async function GET(req: Request, { params }: { params: { key: string[] } }) {
  const { key } = await params;
  const s3Key = key.join("/"); 

  try {
    const command = new GetObjectCommand({
      Bucket: "gaertan",
      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", // use 'attachment' to force download
      },
    });
  } catch (err) {
    console.log(err)
    return new Response("Image not found", { status: 404 });
  }
}