1- import { mkdir , readFile , writeFile } from 'node:fs/promises'
2- import { tmpdir } from 'node:os'
3- import { join } from 'node:path'
41import type { APIRoute } from 'astro'
52import { getCollection } from 'astro:content'
63import { generateOpenGraphImage } from 'astro-og-canvas'
@@ -14,8 +11,10 @@ const ROUTE = '/api/social-card'
1411const DEFAULT_TITLE = 'Platform Engineering by Kevin Brown'
1512const DEFAULT_DESCRIPTION =
1613 'Platform engineer helping teams harden delivery, modernize cloud platforms, and improve developer experience.'
17- const AVATAR_CACHE_DIR = join ( tmpdir ( ) , 'webstackbuilders-social-card' )
18- const AVATAR_CACHE_PATH = join ( AVATAR_CACHE_DIR , 'kevin-brown.webp' )
14+
15+ type ImageMetadataWithFsPath = typeof kevinBrownAvatar & {
16+ fsPath ?: string
17+ }
1918
2019type CollectionKey = 'articles' | 'caseStudies' | 'services' | 'downloads'
2120type PaletteKey = 'articles' | 'case-studies' | 'services' | 'downloads' | 'default'
@@ -61,24 +60,14 @@ const gradientPalette: Record<PaletteKey, [number, number, number][]> = {
6160 ] ,
6261}
6362
64- const getAvatarUrl = ( requestUrl : URL ) : URL => new URL ( kevinBrownAvatar . src , requestUrl )
65-
66- const ensureAvatarFile = async ( requestUrl : URL ) : Promise < string > => {
67- try {
68- await readFile ( AVATAR_CACHE_PATH )
69- return AVATAR_CACHE_PATH
70- } catch {
71- const response = await fetch ( getAvatarUrl ( requestUrl ) )
63+ const getAvatarFilePath = ( ) : string => {
64+ const avatarFilePath = ( kevinBrownAvatar as ImageMetadataWithFsPath ) . fsPath
7265
73- if ( ! response . ok ) {
74- throw new Error ( `Unable to load avatar image: ${ response . status } ${ response . statusText } ` )
75- }
76-
77- const avatarBuffer = Buffer . from ( await response . arrayBuffer ( ) )
78- await mkdir ( AVATAR_CACHE_DIR , { recursive : true } )
79- await writeFile ( AVATAR_CACHE_PATH , avatarBuffer )
80- return AVATAR_CACHE_PATH
66+ if ( ! avatarFilePath ) {
67+ throw new Error ( 'Kevin Brown avatar asset is missing a local file path' )
8168 }
69+
70+ return avatarFilePath
8271}
8372
8473/** Normalize slug parameters to a consistent format */
@@ -144,7 +133,7 @@ export const GET: APIRoute = async ({ request, clientAddress, cookies }) => {
144133 const slug = normalizeSlug ( url . searchParams . get ( 'slug' ) )
145134 const titleOverride = url . searchParams . get ( 'title' )
146135 const descriptionOverride = url . searchParams . get ( 'description' )
147- const avatarPath = await ensureAvatarFile ( url )
136+ const avatarPath = getAvatarFilePath ( )
148137
149138 const contentIndex = await buildContentIndex ( )
150139 const matchedEntry = contentIndex [ slug ]
0 commit comments