-
-
Notifications
You must be signed in to change notification settings - Fork 347
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ Add support for uploading profile pictures (#1332)
- Loading branch information
Showing
24 changed files
with
1,615 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
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, | ||
context: { params: { key: string[] } }, | ||
) { | ||
const imageKey = context.params.key.join("/"); | ||
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 }, | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,40 @@ | ||
"use client"; | ||
import { Avatar, AvatarFallback, AvatarImage } from "@rallly/ui/avatar"; | ||
import { Avatar, AvatarFallback } from "@rallly/ui/avatar"; | ||
import Image from "next/image"; | ||
|
||
import { useUser } from "@/components/user-provider"; | ||
|
||
export const CurrentUserAvatar = ({ className }: { className?: string }) => { | ||
function getAvatarUrl(imageKey: string) { | ||
// Some users have avatars that come from external providers (e.g. Google). | ||
if (imageKey.startsWith("https://")) { | ||
return imageKey; | ||
} | ||
|
||
return `/api/storage/${imageKey}`; | ||
} | ||
|
||
export const CurrentUserAvatar = ({ | ||
size, | ||
className, | ||
}: { | ||
size: number; | ||
className?: string; | ||
}) => { | ||
const { user } = useUser(); | ||
return ( | ||
<Avatar className={className}> | ||
<AvatarImage src={user.image ?? undefined} /> | ||
<AvatarFallback>{user.name[0]}</AvatarFallback> | ||
<Avatar className={className} style={{ width: size, height: size }}> | ||
{user.image ? ( | ||
<Image | ||
src={getAvatarUrl(user.image)} | ||
width={128} | ||
height={128} | ||
alt={user.name} | ||
style={{ objectFit: "cover" }} | ||
objectFit="cover" | ||
/> | ||
) : ( | ||
<AvatarFallback>{user.name[0]}</AvatarFallback> | ||
)} | ||
</Avatar> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.