Skip to content

Commit

Permalink
🔀 Merge #472 ✨ Add image rendering function for embed button
Browse files Browse the repository at this point in the history
  • Loading branch information
williamchong authored Mar 6, 2023
2 parents cb03d90 + 2cff2e3 commit ff6158f
Show file tree
Hide file tree
Showing 7 changed files with 658 additions and 20 deletions.
4 changes: 4 additions & 0 deletions firebase.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
}
],
"rewrites": [
{
"source": "/in/embed/**/image**",
"function": "imagessr"
},
{
"source": "**",
"function": "ssrapp"
Expand Down
1 change: 1 addition & 0 deletions functions/.env.likecoin-button
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
1 change: 1 addition & 0 deletions functions/.env.likecoin-button-develop
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
48 changes: 48 additions & 0 deletions functions/imagessr.js
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);
4 changes: 4 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ if (!process.env.K_SERVICE || process.env.K_SERVICE === 'ssrapp') {
if (!process.env.K_SERVICE || process.env.K_SERVICE === 'superlike') {
exports.superlike = require('./superlike');
}

if (!process.env.K_SERVICE || process.env.K_SERVICE === 'imagessr') {
exports.imagessr = require('./imagessr');
}
Loading

0 comments on commit ff6158f

Please sign in to comment.