mirror of
https://github.com/khairul169/code-share.git
synced 2025-04-28 16:49:36 +07:00
25 lines
572 B
TypeScript
25 lines
572 B
TypeScript
import { Request, Response } from "express";
|
|
import { screenshot } from "../lib/screenshot";
|
|
|
|
const cache = new Map<string, Buffer>();
|
|
|
|
export const thumbnail = async (req: Request, res: Response) => {
|
|
const { slug } = req.params;
|
|
const cacheData = cache.get(slug);
|
|
|
|
if (cacheData) {
|
|
res.contentType("image/jpeg");
|
|
return res.send(cacheData);
|
|
}
|
|
|
|
const result = await screenshot(slug);
|
|
if (!result) {
|
|
return res.status(400).send("Cannot generate thumbnail!");
|
|
}
|
|
|
|
cache.set(slug, result);
|
|
|
|
res.contentType("image/jpeg");
|
|
res.send(result);
|
|
};
|