1
0
Fork 0
mirror of https://github.com/homarr-labs/dashboard-icons.git synced 2025-09-11 02:01:39 +02:00

wip on using local path

This commit is contained in:
Thomas Camlong 2025-04-18 18:17:24 +02:00
parent bb97a00273
commit 8c0d46330a
No known key found for this signature in database
GPG key ID: A678F374F428457B

View file

@ -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(
<div
style={{