forked from RemiixInc/screenshot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
26 lines (24 loc) · 1.03 KB
/
index.ts
File metadata and controls
26 lines (24 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import { getScreenshot } from "./_lib/puppeteer";
module.exports = async (req, res) => {
if (process.env.demo == "yes") return res.status(403).send("The public instance of this API is currently disabled. You can deploy your own instance for free on Vercel here: https://s.vercel.app/deploy.");
if (!req.query.url) return res.status(400).send("No url query specified.");
// if (!checkUrl(req.query.url)) return res.status(400).send("Invalid url query specified.");
try {
const file = await getScreenshot(req.query.url, req.query.width, req.query.height);
res.setHeader("Content-Type", "image/png");
res.setHeader("Cache-Control", "public, immutable, no-transform, s-maxage=86400, max-age=86400");
res.status(200).end(file);
} catch (error) {
console.error(error);
res.status(500).send("The server encountered an error. You may have inputted an invalid query.");
}
};
function checkUrl(string, hostname) {
var url = "";
try {
url = new URL(string);
} catch (error) {
return false;
}
return true;
}