🐛 Use search param for object key

This commit is contained in:
Luke Vella 2024-09-08 23:28:49 +01:00
parent 322f4c5261
commit cd8d61e64b
No known key found for this signature in database
GPG key ID: 469CAD687F0D784C
2 changed files with 58 additions and 1 deletions

View file

@ -0,0 +1,57 @@
import { GetObjectCommand } from "@aws-sdk/client-s3";
import * as Sentry from "@sentry/nextjs";
import { NextRequest, NextResponse } from "next/server";
import { env } from "@/env";
import { getS3Client } from "@/utils/s3";
async function getAvatar(key: string) {
const s3Client = getS3Client();
if (!s3Client) {
throw new Error("S3 client not initialized");
}
const command = new GetObjectCommand({
Bucket: env.S3_BUCKET_NAME,
Key: key,
});
const response = await s3Client.send(command);
if (!response.Body) {
throw new Error("Object not found");
}
const arrayBuffer = await response.Body.transformToByteArray();
const buffer = Buffer.from(arrayBuffer);
return {
buffer,
contentType: response.ContentType || "application/octet-stream",
};
}
export async function GET(req: NextRequest) {
const imageKey = req.nextUrl.searchParams.get("key");
if (!imageKey) {
return new Response("No key provided", { status: 400 });
}
try {
const { buffer, contentType } = await getAvatar(imageKey);
return new NextResponse(buffer, {
headers: {
"Content-Type": contentType,
"Cache-Control": "public, max-age=3600",
},
});
} catch (error) {
Sentry.captureException(error);
return NextResponse.json(
{ error: "Failed to fetch object" },
{ status: 500 },
);
}
}

View file

@ -8,7 +8,7 @@ function getAvatarUrl(imageKey: string) {
return imageKey;
}
return `/api/storage/${imageKey}`;
return `/api/storage?key=${encodeURIComponent(imageKey)}`;
}
export const OptimizedAvatarImage = ({