Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove url query param from screenshot #22

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions env.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const {
PORT = '3005',
UPSTREAM_IPFS_URL = 'http://127.0.0.1:5001',
UPSTREAM_IPFS_CLUSTER_URL = 'http://127.0.0.1:9095',
IPFS_GATEWAY_URL = 'http://127.0.0.1:8080',
} = process.env;

const GRAPH_API_ENDPOINT =
Expand All @@ -11,5 +12,6 @@ module.exports = {
PORT,
UPSTREAM_IPFS_URL,
UPSTREAM_IPFS_CLUSTER_URL,
IPFS_GATEWAY_URL,
GRAPH_API_ENDPOINT,
};
19 changes: 9 additions & 10 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const {
PORT,
UPSTREAM_IPFS_URL,
UPSTREAM_IPFS_CLUSTER_URL,
IPFS_GATEWAY_URL,
GRAPH_API_ENDPOINT,
} = require('./env');

Expand Down Expand Up @@ -845,15 +846,10 @@ app.use(
);

app.get('/api/screenshot', authenticateToken, async (req, res, next) => {
const { url } = req.query;
const { ipns } = req.query;

if (!url) {
return next(new HttpError('URL parameter is required', 400));
}

const urlPattern = /^(https?:\/\/[a-zA-Z0-9.-]+(:\d+)?\/ipns\/[a-zA-Z0-9\/_-]+)$/;
if (!urlPattern.test(url)) {
return next(new HttpError('Invalid url', 400));
if (!ipns) {
return next(new HttpError('IPNS parameter is required', 400));
}

let browser;
Expand All @@ -864,13 +860,16 @@ app.get('/api/screenshot', authenticateToken, async (req, res, next) => {
});
const page = await browser.newPage();
await page.setViewport({ width: 800, height: 600 });
await page.goto(url, { waitUntil: 'networkidle2', timeout: 120000 });
await page.goto(`${IPFS_GATEWAY_URL}/ipns/${ipns}/`, {
waitUntil: 'networkidle2',
timeout: 120000,
});
const screenshotBase64 = await page.screenshot({
encoding: 'base64',
});
await browser.close();

res.json({ image: `data:image/png;base64,${screenshotBase64}` });
res.json({ screenshotBase64 });
} catch {
next(new HttpError('Failed to generate screenshot', 500));
} finally {
Expand Down