mirror of
https://github.com/khairul169/code-share.git
synced 2026-10-24 11:53:58 +07:00
feat: update thumbnail api
This commit is contained in:
@@ -3,7 +3,7 @@ import puppeteer, { Browser } from "puppeteer";
|
||||
let browser: Browser | null = null;
|
||||
let closeHandler: any;
|
||||
|
||||
export const screenshot = async (slug: string) => {
|
||||
export const screenshot = async (url: string) => {
|
||||
try {
|
||||
if (!browser) {
|
||||
browser = await puppeteer.launch({
|
||||
@@ -15,12 +15,15 @@ export const screenshot = async (slug: string) => {
|
||||
const page = await browser.newPage();
|
||||
|
||||
await page.setViewport({ width: 512, height: 340 });
|
||||
await page.goto(`http://localhost:3000/api/preview/${slug}/index.html`, {
|
||||
await page.goto(url, {
|
||||
waitUntil: "networkidle0",
|
||||
timeout: 5000,
|
||||
});
|
||||
const result = await page.screenshot();
|
||||
await page.close();
|
||||
|
||||
setTimeout(() => {
|
||||
page.close();
|
||||
}, 500);
|
||||
|
||||
if (closeHandler) {
|
||||
clearTimeout(closeHandler);
|
||||
@@ -30,7 +33,7 @@ export const screenshot = async (slug: string) => {
|
||||
browser?.close();
|
||||
browser = null;
|
||||
closeHandler = null;
|
||||
}, 60000);
|
||||
}, 30000);
|
||||
|
||||
return result;
|
||||
} catch (err) {
|
||||
|
||||
+5
-1
@@ -12,8 +12,12 @@ export const fileExists = (path: string) => {
|
||||
}
|
||||
};
|
||||
|
||||
export const getStorageDir = (...args: string[]) => {
|
||||
return path.join(process.cwd(), "storage", ...args);
|
||||
};
|
||||
|
||||
export const getProjectDir = (project: ProjectSchema) => {
|
||||
return path.resolve(process.cwd(), "storage/tmp", project.slug);
|
||||
return path.join(process.cwd(), "storage/tmp", project.slug);
|
||||
};
|
||||
|
||||
export const uid = cuid2.init({
|
||||
|
||||
Reference in New Issue
Block a user