mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
feat: add project thumbnail generator
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
import { Router } from "express";
|
||||
import preview from "./preview";
|
||||
import trpc from "./trpc/handler";
|
||||
import { thumbnail } from "./thumbnail";
|
||||
|
||||
const api = Router();
|
||||
|
||||
api.use("/trpc", trpc);
|
||||
api.use("/preview", preview);
|
||||
|
||||
api.get("/thumbnail/:slug", thumbnail);
|
||||
|
||||
export default api;
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
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);
|
||||
};
|
||||
Reference in New Issue
Block a user