-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🔀 Merge #472 ✨ Add image rendering function for embed button
- Loading branch information
Showing
7 changed files
with
658 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
EXTERNAL_HOSTNAME=button.like.co | ||
SENTRY_REPORT_URI=https://sentry.io/api/1228748/security/?sentry_key=1c744ab5538a47d7abfff406840f93b2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
IS_TESTNET=TRUE | ||
EXTERNAL_HOSTNAME=button.rinkeby.like.co | ||
SENTRY_REPORT_URI=https://sentry.io/api/1228748/security/?sentry_key=1c744ab5538a47d7abfff406840f93b2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// TODO: eslint import cannot handle firebase module | ||
// eslint-disable-next-line import/no-unresolved | ||
const { onRequest } = require('firebase-functions/v2/https'); | ||
const express = require('express'); | ||
const axios = require('axios'); | ||
const nodeHtmlToImage = require('node-html-to-image'); | ||
const sharp = require('sharp'); | ||
|
||
const app = express(); | ||
|
||
app.get(['/in/embed/**', '/in/like/**'], async (req, res) => { | ||
try { | ||
if (!req.path.includes('/image')) { | ||
res.status(400).send('INVALID_URL'); | ||
return; | ||
} | ||
const scale = Number(req.query.scale) || 1; | ||
const { EXTERNAL_HOSTNAME } = process.env; | ||
const url = `https://${EXTERNAL_HOSTNAME}${req.originalUrl.replace('/image', '')}`; | ||
const { data } = await axios.get(url); | ||
const image = await nodeHtmlToImage({ | ||
html: data, | ||
quality: 100, | ||
type: 'jpeg', | ||
selector: '#__layout > div', | ||
puppeteerArgs: { | ||
defaultViewport: { | ||
width: 360, | ||
height: 480, | ||
deviceScaleFactor: scale, | ||
}, | ||
}, | ||
}); | ||
const withMetadata = await sharp(image) | ||
.withMetadata({ density: 77 * scale }) | ||
.toBuffer(); | ||
res.writeHead(200, { | ||
'Content-Type': 'image/jpeg', | ||
'Cache-Control': 'public, max-age=3600, stale-while-revalidate=3600, stale-if-error=3600', | ||
}); | ||
res.end(withMetadata, 'binary'); | ||
} catch (err) { | ||
console.error(JSON.stringify(err)); | ||
res.sendStatus(500); | ||
} | ||
}); | ||
|
||
module.exports = onRequest({ region: ['us-west1'], memory: '512MB' }, app); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.