diff --git a/src/app/[username]/(profile-page)/opengraph-image.tsx b/src/app/[username]/(profile-page)/opengraph-image.tsx new file mode 100644 index 0000000..c31023c --- /dev/null +++ b/src/app/[username]/(profile-page)/opengraph-image.tsx @@ -0,0 +1,173 @@ +import { getUserByUsername } from "@/backend/services/user.action"; +import getFileUrl from "@/utils/getFileUrl"; +import { sanitizedUsername } from "@/lib/utils"; +import { ImageResponse } from "next/og"; +import { readFile } from "node:fs/promises"; +import { join } from "node:path"; + +interface ProfilePageProps { + params: Promise<{ + username: string; + }>; +} + +export const size = { + width: 1200, + height: 630, +}; +export const contentType = "image/png"; + +const getFileLocation = async (relativePath: string) => { + const fileData = await readFile(join(process.cwd(), "public", relativePath)); + return Uint8Array.from(fileData).buffer; +}; + +export default async function Image(options: ProfilePageProps) { + const { username } = await options.params; + const sanitized = sanitizedUsername(username); + const profile = await getUserByUsername(sanitized, [ + "name", + "username", + "bio", + "profile_photo", + "designation", + ]); + + const displayName = profile?.name ?? sanitized; + const displayUsername = profile?.username ?? sanitized; + const bio = profile?.bio ?? null; + const designation = profile?.designation ?? null; + const profilePhotoUrl = profile?.profile_photo + ? getFileUrl(profile.profile_photo) + : null; + + return new ImageResponse( + ( +
+ @{displayUsername} +
+ {designation ? ( ++ {designation} +
+ ) : null} + {bio ? ( ++ {bio.length > 120 ? bio.slice(0, 117) + "..." : bio} +
+ ) : null} +TechDiary
+