2020-07-26 03:59:49 -07:00
|
|
|
import { buildTemplate, parseTemplates } from "../src/buildTemplate";
|
2020-07-22 13:19:23 -07:00
|
|
|
import { createCanvas, registerFont, loadImage } from 'canvas';
|
2020-07-26 03:59:49 -07:00
|
|
|
import Papa from 'papaparse';
|
|
|
|
import fs from 'fs';
|
2020-09-28 08:51:26 -07:00
|
|
|
import translations from '../server/translations';
|
2020-07-26 03:59:49 -07:00
|
|
|
|
|
|
|
const loadTsv = (filename) => {
|
|
|
|
return Papa.parse(fs.readFileSync(__dirname + '/../data/' + filename).toString('utf-8'), {
|
|
|
|
dynamicTyping: true,
|
|
|
|
header: true,
|
|
|
|
skipEmptyLines: true,
|
|
|
|
}).data;
|
|
|
|
};
|
2020-07-22 13:19:23 -07:00
|
|
|
|
|
|
|
export default async function (req, res, next) {
|
|
|
|
if (req.url.substr(req.url.length - 4) !== '.png') {
|
|
|
|
res.statusCode = 404;
|
|
|
|
res.write('Not found');
|
|
|
|
res.end();
|
|
|
|
return;
|
|
|
|
}
|
2020-07-26 03:59:49 -07:00
|
|
|
|
2020-09-13 13:15:44 -07:00
|
|
|
const templateName = decodeURIComponent(req.url.substr(1, req.url.length - 5));
|
|
|
|
|
2020-07-26 03:59:49 -07:00
|
|
|
const template = buildTemplate(
|
2020-10-12 23:41:42 -07:00
|
|
|
parseTemplates(loadTsv('templates/templates.tsv')),
|
2020-09-13 13:15:44 -07:00
|
|
|
templateName,
|
2020-07-26 03:59:49 -07:00
|
|
|
);
|
2020-07-22 13:19:23 -07:00
|
|
|
|
|
|
|
const width = 1200
|
|
|
|
const height = 600
|
|
|
|
const mime = 'image/png';
|
|
|
|
const imageSize = 200;
|
2020-09-13 13:15:44 -07:00
|
|
|
const leftRatio = template || templateName === 'dowolne' ? 4 : 5;
|
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'
|
|
|
|
|
|
|
|
const image = await loadImage('node_modules/@fortawesome/fontawesome-pro/svgs/light/tags.svg');
|
|
|
|
context.drawImage(image, width / leftRatio - imageSize / 2, height / 2 - imageSize / 1.25 / 2, imageSize, imageSize / 1.25)
|
|
|
|
|
2020-09-13 13:15:44 -07:00
|
|
|
if (template || templateName === 'dowolne') {
|
2020-07-22 13:19:23 -07:00
|
|
|
context.font = 'regular 48pt Quicksand'
|
2020-09-28 08:51:26 -07:00
|
|
|
context.fillText(translations.template.intro + ':', width / leftRatio + imageSize / 1.5, height / 2 - 36)
|
2020-07-22 13:19:23 -07:00
|
|
|
|
2020-09-13 13:15:44 -07:00
|
|
|
const templateNameOptions = templateName === 'dowolne' ? ['dowolne'] : template.nameOptions();
|
2020-07-26 04:14:30 -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-07-22 13:19:23 -07:00
|
|
|
} else {
|
|
|
|
context.font = 'regular 120pt Quicksand'
|
2020-09-28 10:22:36 -07:00
|
|
|
context.fillText(translations.title, width / leftRatio + imageSize / 1.5, height / 2 + 48)
|
2020-07-22 13:19:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
res.setHeader('content-type', mime);
|
|
|
|
res.write(canvas.toBuffer(mime));
|
|
|
|
res.end()
|
|
|
|
}
|