Skip to content
This repository was archived by the owner on Sep 11, 2025. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion pages/api/thumbnail.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,51 @@
import { NextApiRequest, NextApiResponse } from 'next'
import type { NextApiRequest, NextApiResponse } from 'next'
import * as playwright from 'playwright-aws-lambda';
import fs from 'fs'
import { promisify } from 'util'
import https from 'https'
import os from 'os'
import path from 'path'

import { getAbsoluteURL } from 'utils/utils';

const FONTS_DIR = path.join(os.tmpdir(), "fonts")

const loadFont = (input: string) => new Promise(async (resolve, reject) => {
const url = new URL(input);
const output = path.join(FONTS_DIR, url.pathname.split('/').pop());
if (await promisify(fs.exists)(output)) {
resolve()
return
}
await promisify(fs.mkdir)(FONTS_DIR);
await promisify(fs.writeFile)(path.join(FONTS_DIR, "fonts.conf"), `<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<dir>${FONTS_DIR}</dir>
<cachedir>/tmp/fonts-cache/</cachedir>
<config></config>
</fontconfig>`)
const stream = fs.createWriteStream(output);
stream.once('error', (error) => {
return reject(error);
});
https.get(input, response => {
response.on('data', (chunk) => {
stream.write(chunk);
});

response.once('end', () => {
stream.end(() => {
return resolve(url.pathname.split('/').pop());
});
});
})
})

export default async (req: NextApiRequest, res: NextApiResponse) => {
await loadFont("https://raw.githack.com/googlei18n/noto-emoji/master/fonts/NotoColorEmoji.ttf")
process.env.FONTCONFIG_PATH = FONTS_DIR

const browser = await playwright.launchChromium();
const page = await browser.newPage({
viewport: {
Expand Down