wip on using local path

This commit is contained in:
Thomas Camlong
2025-04-18 18:17:24 +02:00
parent bb97a00273
commit 8c0d46330a

View File

@@ -1,6 +1,7 @@
import { BASE_URL } from "@/constants"
import { getAllIcons } from "@/lib/api" import { getAllIcons } from "@/lib/api"
import { ImageResponse } from "next/og" import { ImageResponse } from "next/og"
import { readFile } from "node:fs/promises"
import { join } from "node:path"
export const dynamic = "force-static" export const dynamic = "force-static"
@@ -20,8 +21,6 @@ export default async function Image({ params }: { params: { icon: string } }) {
const iconsData = await getAllIcons() const iconsData = await getAllIcons()
const totalIcons = Object.keys(iconsData).length const totalIcons = Object.keys(iconsData).length
const index = Object.keys(iconsData).indexOf(icon) 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 // Format the icon name for display
const formattedIconName = icon 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)) .map((word) => word.charAt(0).toUpperCase() + word.slice(1))
.join(" ") .join(" ")
// Get the icon URL // Read the icon file from local filesystem
// Check if the image exists if the return type of the image is of image type let iconData: Buffer | null = null
try { try {
const response = await fetch(iconUrl) const iconPath = join(process.cwd(), `../png/${icon}.png`)
if (!response.ok) { console.log(`Generating opengraph image for ${icon} (${index + 1} / ${totalIcons}) from path ${iconPath}`)
console.error(`Icon ${icon} was not found at ${iconUrl}`) iconData = await readFile(iconPath)
iconUrl = `https://placehold.co/600x400?text=${formattedIconName}`
}
} catch (error) { } catch (error) {
console.error(`Icon ${icon} was not found at ${iconUrl}`) console.error(`Icon ${icon} was not found locally`)
iconUrl = `https://placehold.co/600x400?text=${formattedIconName}`
} }
// 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( return new ImageResponse(
<div <div
style={{ style={{