From 8c0d46330a4af2b99952c71e842e1a2d0f82effc Mon Sep 17 00:00:00 2001 From: Thomas Camlong Date: Fri, 18 Apr 2025 18:17:24 +0200 Subject: [PATCH] wip on using local path --- web/src/app/icons/[icon]/opengraph-image.tsx | 25 ++++++++++---------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/web/src/app/icons/[icon]/opengraph-image.tsx b/web/src/app/icons/[icon]/opengraph-image.tsx index de91a167..b7321e7b 100644 --- a/web/src/app/icons/[icon]/opengraph-image.tsx +++ b/web/src/app/icons/[icon]/opengraph-image.tsx @@ -1,6 +1,7 @@ -import { BASE_URL } from "@/constants" import { getAllIcons } from "@/lib/api" import { ImageResponse } from "next/og" +import { readFile } from "node:fs/promises" +import { join } from "node:path" export const dynamic = "force-static" @@ -20,8 +21,6 @@ export default async function Image({ params }: { params: { icon: string } }) { const iconsData = await getAllIcons() const totalIcons = Object.keys(iconsData).length const index = Object.keys(iconsData).indexOf(icon) - let iconUrl = `${BASE_URL}/png/${icon}.png` - console.log(`Generating opengraph image for ${icon} (${index + 1} / ${totalIcons}) with url ${iconUrl}`) // Format the icon name for display const formattedIconName = icon @@ -29,19 +28,21 @@ export default async function Image({ params }: { params: { icon: string } }) { .map((word) => word.charAt(0).toUpperCase() + word.slice(1)) .join(" ") - // Get the icon URL - // Check if the image exists if the return type of the image is of image type + // Read the icon file from local filesystem + let iconData: Buffer | null = null try { - const response = await fetch(iconUrl) - if (!response.ok) { - console.error(`Icon ${icon} was not found at ${iconUrl}`) - iconUrl = `https://placehold.co/600x400?text=${formattedIconName}` - } + const iconPath = join(process.cwd(), `../png/${icon}.png`) + console.log(`Generating opengraph image for ${icon} (${index + 1} / ${totalIcons}) from path ${iconPath}`) + iconData = await readFile(iconPath) } catch (error) { - console.error(`Icon ${icon} was not found at ${iconUrl}`) - iconUrl = `https://placehold.co/600x400?text=${formattedIconName}` + console.error(`Icon ${icon} was not found locally`) } + // Convert the image data to a data URL or use placeholder + const iconUrl = iconData + ? `data:image/png;base64,${iconData.toString("base64")}` + : `https://placehold.co/600x400?text=${formattedIconName}` + return new ImageResponse(