chore: Saved temp png in memory instead of writing to fs - screenshot.ts (#913)
This commit is contained in:
+18
-7
@@ -5,9 +5,19 @@ import { join } from "https://deno.land/std@0.137.0/path/mod.ts";
|
||||
const url = Deno.args[0];
|
||||
const id = Deno.args[1];
|
||||
|
||||
if (!url || !id) {
|
||||
if (Deno.args.length == 0) {
|
||||
console.log("Usage: screenshot <url> <id>");
|
||||
Deno.exit(1);
|
||||
Deno.exit(0);
|
||||
}
|
||||
|
||||
if (!(url.match(/^http[s]?:\/\//)) || !url) {
|
||||
console.log("Provided URL is Broken or Wrong");
|
||||
Deno.exit(0);
|
||||
}
|
||||
|
||||
if (!id) {
|
||||
console.log("Provide id to Process");
|
||||
Deno.exit(0);
|
||||
}
|
||||
|
||||
const outDir = "./www/static/showcase";
|
||||
@@ -16,18 +26,19 @@ const browser = await puppeteer.launch({
|
||||
});
|
||||
const page = await browser.newPage();
|
||||
await page.goto(url, { waitUntil: "networkidle2" });
|
||||
const png2x = join(outDir, `${id}.png`);
|
||||
await page.screenshot({ path: png2x });
|
||||
const raw = await page.screenshot();
|
||||
|
||||
await browser.close();
|
||||
|
||||
if (!(raw instanceof Uint8Array)) {
|
||||
console.log("Invalid Image");
|
||||
Deno.exit(0);
|
||||
}
|
||||
// convert to jpeg
|
||||
const image2x = await Image.decode(await Deno.readFile(png2x));
|
||||
const image2x = await Image.decode(raw);
|
||||
const jpeg2x = join(outDir, `${id}2x.jpg`);
|
||||
await Deno.writeFile(jpeg2x, await image2x.encodeJPEG(80));
|
||||
|
||||
const jpeg1x = join(outDir, `${id}1x.jpg`);
|
||||
const image1x = image2x.resize(image2x.width / 2, Image.RESIZE_AUTO);
|
||||
await Deno.writeFile(jpeg1x, await image1x.encodeJPEG(80));
|
||||
|
||||
await Deno.removeSync(png2x);
|
||||
|
||||
Reference in New Issue
Block a user