2020-10-31 13:33:59 -07:00
|
|
|
import { Router } from 'express';
|
|
|
|
import SQL from 'sql-template-strings';
|
|
|
|
import {createCanvas, loadImage, registerFont} from "canvas";
|
|
|
|
import translations from "../translations";
|
|
|
|
import {gravatar} from "../../src/helpers";
|
|
|
|
import {buildTemplate, parseTemplates} from "../../src/buildTemplate";
|
|
|
|
import {loadTsv} from "../../src/tsv";
|
2020-10-16 14:32:51 -07:00
|
|
|
|
|
|
|
const drawCircle = (context, image, x, y, size) => {
|
|
|
|
context.save();
|
|
|
|
context.beginPath();
|
|
|
|
context.arc(x + size / 2, y + size / 2, size / 2, 0, Math.PI * 2, true);
|
|
|
|
context.closePath();
|
|
|
|
context.clip();
|
|
|
|
|
|
|
|
context.drawImage(image, x, y, size, size)
|
|
|
|
|
|
|
|
context.beginPath();
|
|
|
|
context.arc(x, y, size / 2, 0, Math.PI * 2, true);
|
|
|
|
context.clip();
|
|
|
|
context.closePath();
|
|
|
|
context.restore();
|
|
|
|
}
|
2020-07-26 03:59:49 -07:00
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const router = Router();
|
2020-07-22 13:19:23 -07:00
|
|
|
|
2020-11-01 03:48:12 -08:00
|
|
|
router.get('/banner/:templateName*.png', async (req, res) => {
|
2020-07-22 13:19:23 -07:00
|
|
|
const width = 1200
|
|
|
|
const height = 600
|
|
|
|
const mime = 'image/png';
|
|
|
|
const imageSize = 200;
|
2020-10-16 14:32:51 -07:00
|
|
|
let leftRatio = 4;
|
2020-07-22 13:19:23 -07:00
|
|
|
|
|
|
|
registerFont('static/fonts/quicksand-v21-latin-ext_latin-regular.ttf', { family: 'Quicksand', weight: 'regular'});
|
|
|
|
registerFont('static/fonts/quicksand-v21-latin-ext_latin-700.ttf', { family: 'Quicksand', weight: 'bold'});
|
|
|
|
|
|
|
|
const canvas = createCanvas(width, height)
|
|
|
|
const context = canvas.getContext('2d')
|
|
|
|
|
|
|
|
context.fillStyle = '#fff'
|
|
|
|
context.fillRect(0, 0, width, height)
|
|
|
|
context.fillStyle = '#000'
|
|
|
|
|
2020-10-16 14:32:51 -07:00
|
|
|
const fallback = async _ => {
|
|
|
|
const logo = await loadImage('node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg');
|
|
|
|
leftRatio = 5;
|
|
|
|
context.drawImage(logo, width / leftRatio - imageSize / 2, height / 2 - imageSize / 1.25 / 2, imageSize, imageSize / 1.25);
|
|
|
|
context.font = 'regular 120pt Quicksand';
|
|
|
|
context.fillText(translations.title, width / leftRatio + imageSize / 1.5, height / 2 + 48);
|
|
|
|
}
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
if (req.params.templateName.startsWith('@')) {
|
|
|
|
const user = await req.db.get(SQL`SELECT username, email FROM users WHERE username=${req.params.templateName.substring(1)}`);
|
2020-10-16 14:32:51 -07:00
|
|
|
if (!user) {
|
|
|
|
await fallback();
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.set('content-type', mime).send(canvas.toBuffer(mime));
|
2020-10-16 14:32:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const avatar = await loadImage(gravatar(user, imageSize));
|
2020-07-22 13:19:23 -07:00
|
|
|
|
2020-10-16 14:32:51 -07:00
|
|
|
drawCircle(context, avatar, width / leftRatio - imageSize / 2, height / 2 - imageSize / 2, imageSize);
|
2020-07-22 13:19:23 -07:00
|
|
|
|
2020-10-16 14:32:51 -07:00
|
|
|
context.font = `regular 48pt Quicksand`
|
|
|
|
context.fillText('@' + user.username, width / leftRatio + imageSize, height / 2)
|
|
|
|
|
|
|
|
const logo = await loadImage('static/favicon.svg');
|
|
|
|
|
|
|
|
context.font = 'regular 24pt Quicksand'
|
|
|
|
context.fillStyle = '#C71585';
|
|
|
|
const logoSize = 24 * 1.25;
|
|
|
|
context.drawImage(logo, width / leftRatio + imageSize, height / 2 + logoSize - 4, logoSize, logoSize / 1.25)
|
|
|
|
context.fillText(translations.title, width / leftRatio + imageSize + 36, height / 2 + 48);
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.set('content-type', mime).send(canvas.toBuffer(mime));
|
2020-10-16 14:32:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
const template = buildTemplate(
|
2020-10-31 13:33:59 -07:00
|
|
|
parseTemplates(loadTsv(__dirname + '/../../data/templates/templates.tsv')),
|
|
|
|
req.params.templateName,
|
2020-10-16 14:32:51 -07:00
|
|
|
);
|
|
|
|
|
|
|
|
const logo = await loadImage('node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg');
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
if (!template && req.params.templateName !== 'dowolne') { // TODO
|
2020-10-16 14:32:51 -07:00
|
|
|
await fallback();
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.set('content-type', mime).send(canvas.toBuffer(mime));
|
2020-07-22 13:19:23 -07:00
|
|
|
}
|
|
|
|
|
2020-10-16 14:32:51 -07:00
|
|
|
context.drawImage(logo, width / leftRatio - imageSize / 2, height / 2 - imageSize / 1.25 / 2, imageSize, imageSize / 1.25)
|
|
|
|
context.font = 'regular 48pt Quicksand'
|
|
|
|
context.fillText(translations.template.intro + ':', width / leftRatio + imageSize / 1.5, height / 2 - 36)
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
const templateNameOptions = req.params.templateName === 'dowolne' ? ['dowolne'] : template.nameOptions();
|
2020-10-16 14:32:51 -07:00
|
|
|
context.font = `bold ${templateNameOptions.length <= 2 ? '70' : '36'}pt Quicksand`
|
|
|
|
context.fillText(templateNameOptions.join('\n'), width / leftRatio + imageSize / 1.5, height / 2 + (templateNameOptions.length <= 2 ? 72 : 24))
|
|
|
|
|
2020-10-31 13:33:59 -07:00
|
|
|
return res.set('content-type', mime).send(canvas.toBuffer(mime));
|
|
|
|
});
|
|
|
|
|
|
|
|
export default router;
|