-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path[slug].png.ts
32 lines (29 loc) · 1.1 KB
/
[slug].png.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import type { APIRoute } from 'astro'
import type { CollectionEntry } from 'astro:content'
import { existsSync } from 'node:fs'
import { dirname } from 'node:path'
import { fileURLToPath } from 'node:url'
import { resolve } from 'pathe'
import { getPostList } from '~/lib/server'
import { getPostImageBuffer } from '~/lib/server/og'
export async function getStaticPaths() {
const _PROD_PUBLIC_OG_DIR = resolve(dirname(fileURLToPath(import.meta.url)), '../../og')
const blogEntries = await getPostList()
return blogEntries
.filter((post) => {
const existedPublic = existsSync(resolve(_PROD_PUBLIC_OG_DIR, `${post.slug}.png`))
const hasExternalImage = !post.data.image?.startsWith('/og/')
return !(existedPublic || hasExternalImage)
})
.map(post => ({
params: { slug: post.slug },
props: { ...post },
}))
}
export const GET: APIRoute = async ({ props }) =>
new Response(
await getPostImageBuffer(props as CollectionEntry<'blog'>),
{
headers: { 'Content-Type': 'image/png' },
},
)