-
Notifications
You must be signed in to change notification settings - Fork 0
fix: Enable image viewing in volunteer dashboard using presigned URLs #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ import { verifyToken } from "@/lib/auth"; | |
| import { pendingReportsQuerySchema } from "@/lib/validation/volunteerSchemas"; | ||
| import { ERROR_CODES } from "@/lib/errorCodes"; | ||
| import { ZodError } from "zod"; | ||
| import { generateReadPresignedUrl } from "@/lib/s3Client"; // ADDED | ||
|
|
||
| export async function GET(req: NextRequest) { | ||
| try { | ||
|
|
@@ -42,9 +43,7 @@ export async function GET(req: NextRequest) { | |
| name: true, | ||
| }, | ||
| }, | ||
| images: { | ||
| take: 1, | ||
| }, | ||
| images: true, // CHANGED from take: 1 | ||
| }, | ||
| orderBy: { | ||
| createdAt: "asc", | ||
|
|
@@ -53,13 +52,27 @@ export async function GET(req: NextRequest) { | |
| take: validatedQuery.limit, | ||
| }); | ||
|
|
||
| // ADDED: Generate presigned URLs | ||
| const reportsWithSignedUrls = await Promise.all( | ||
| reports.map(async (report) => ({ | ||
| ...report, | ||
| imageUrl: await generateReadPresignedUrl(report.imageUrl), | ||
| images: await Promise.all( | ||
| report.images.map(async (img) => ({ | ||
| ...img, | ||
| url: await generateReadPresignedUrl(img.url), | ||
| })) | ||
| ), | ||
| })) | ||
|
Comment on lines
+56
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block generates a presigned URL for |
||
| ); | ||
|
|
||
| const totalCount = await prisma.report.count({ | ||
| where: { status: "PENDING" }, | ||
| }); | ||
|
|
||
| return sendSuccess( | ||
| { | ||
| reports, | ||
| reports: reportsWithSignedUrls, // CHANGED from reports | ||
| pagination: { | ||
| page: validatedQuery.page, | ||
| limit: validatedQuery.limit, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,8 @@ | ||
| import { S3Client, PutObjectCommand } from "@aws-sdk/client-s3"; | ||
| import { | ||
| S3Client, | ||
| PutObjectCommand, | ||
| GetObjectCommand, | ||
| } from "@aws-sdk/client-s3"; | ||
| import { getSignedUrl } from "@aws-sdk/s3-request-presigner"; | ||
|
|
||
| const s3Client = new S3Client({ | ||
|
|
@@ -29,3 +33,25 @@ export async function generatePresignedUrl( | |
| export function getPublicS3Url(filename: string): string { | ||
| return `${process.env.NEXT_PUBLIC_S3_URL}/${filename}`; | ||
| } | ||
|
|
||
| export async function generateReadPresignedUrl( | ||
| imageUrl: string, | ||
| expiresIn: number = 3600 | ||
| ): Promise<string> { | ||
| try { | ||
| // Extract key from S3 URL | ||
| const url = new URL(imageUrl); | ||
| const key = url.pathname.substring(1); | ||
|
|
||
| const command = new GetObjectCommand({ | ||
| Bucket: process.env.AWS_BUCKET_NAME!, | ||
| Key: key, | ||
| }); | ||
|
Comment on lines
+46
to
+49
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using the non-null assertion operator ( const bucketName = process.env.AWS_BUCKET_NAME;
if (!bucketName) {
throw new Error("AWS_BUCKET_NAME environment variable is not set.");
}
const command = new GetObjectCommand({
Bucket: bucketName,
Key: key,
}); |
||
|
|
||
| const signedUrl = await getSignedUrl(s3Client, command, { expiresIn }); | ||
| return signedUrl; | ||
| } catch (error) { | ||
| console.error("Error generating read presigned URL:", error); | ||
| return imageUrl; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This block generates a presigned URL for
report.imageUrland then again for each image inreport.images. Sincereport.imageUrlis also present in thereport.imagesarray (based on the creation logic), you are generating a presigned URL for the same image twice for every report. This is redundant. Consider refactoring to avoid the duplicate signing operation, for example by signing all images in theimagesarray and then finding the corresponding new URL forimageUrlfrom that array.