-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
159 lines (125 loc) · 4.81 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
const express = require('express');
const app = express();
const port = 80;
let ejs = require('ejs');
const { createCanvas, loadImage, registerFont } = require('canvas');
const fs = require('fs');
const mysql = require('mysql');
require('dotenv').config()
const toUnnamed = require('named-placeholders')();
const originalQuery = require('mysql/lib/Connection').prototype.query;
require('mysql/lib/Connection').prototype.query = function (...args) {
if (Array.isArray(args[0]) || !args[1]) {
return originalQuery.apply(this, args);
}
([
args[0],
args[1]
] = toUnnamed(args[0], args[1]));
return originalQuery.apply(this, args);
};
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: 'apps',
password: process.env.DB_PASS,
database: 'og-image'
})
connection.connect();
registerFont('./public/fonts/Atkinson-Regular.ttf', { family: 'atkinson' })
registerFont('./public/fonts/Menlo-Regular.ttf', { family: 'menlo' })
console.log('running')
app.set('view engine', 'ejs')
app.use(express.static('public'))
let count = 0;
app.get('/og/:title/:subtitle/:url/:protocol/:font/:image/:color/data.png', (req, res) => {
// res.send(req.params);
res.contentType('image/png');
// res.send(tile);
console.log(`${count}: {title: '${req.params.title}', subtitle: '${req.params.subtitle}', url: '${req.params.url}', image: '${req.params.image}', font: '${req.params.font}'}`)
if(req.params.title == ' ') req.params.title = '';
if(req.params.subtitle == ' ') req.params.subtitle = '';
if(req.params.url == ' ') req.params.url = '';
if(req.params.protocol == ' ') { req.params.protocol = '' } else { req.params.protocol += '://' };
if(req.params.image == ' ') req.params.title = 'candybar';
if(req.params.color == ' ') req.params.color = 'black';
if(req.params.font == ' ') req.params.font = 'menlo';
connection.query(`INSERT INTO uses (title, subtitle, protocol, url, font, color, background) VALUES (:title, :subtitle, :protocol, :url, :font, :color, :background)`, {
title: req.params.title,
subtitle: req.params.subtitle,
protocol: req.params.protocol,
url: req.params.url,
font: req.params.font,
color: req.params.color,
background: req.params.background
}, (err, result, fields) => {
if(err) throw err;
})
const img_map = {
candybar:'./public/backgrounds/candy-bar.jpg',
cheerfulorange: './public/backgrounds/cheerful-orange.jpg',
renownedred: './public/backgrounds/renowned-red.jpg',
unmatchedeclipse: './public/backgrounds/unmatched-eclipse.jpg',
virtualshapes: './public/backgrounds/virtual-shapes.jpg',
flawlessaffinity: './public/backgrounds/flawless-affinity.jpg',
prettyheroic: './public/backgrounds/pretty-heroic.jpg',
irritablehero: './public/backgrounds/irritable-hero.jpg',
circleacquired: './public/backgrounds/circle-acquired.jpg',
descendsun: './public/backgrounds/descend-sun.jpg',
shatteredfeelings: './public/backgrounds/shattered-feelings.jpg'
}
const getImage = (name) => {
return img_map[name]
}
let promises = [];
const width = 1200;
const height = 627;
const canvas = createCanvas(width, height);
const context = canvas.getContext('2d');
context.fillStyle = 'black';
context.fillRect(0, 0, width, height);
promises.push(
loadImage(getImage(req.params.image))
.then(image => {
context.drawImage(image, 0, 0, width, height);
})
.catch(e => console.log(e))
)
Promise.all(promises).then(values => {
context.strokeStyle = req.params.color.replace('{{h}}', '#');
context.lineWidth = 5;
context.beginPath();
context.rect(10, 10, width - 20, height - 20)
context.stroke();
context.lineWidth = 5;
context.beginPath();
context.moveTo(50, height - 150);
context.lineTo(250, height - 150);
context.stroke();
// context.font = 'bold 70pt Menlo';
context.font = `bold 70pt ${req.params.font}`;
context.textAlign = 'left';
context.fillStyle = req.params.color.replace('{{h}}', '#');
let text = req.params.title;
context.fillText(text.split('{{n}}').join('\n'), 50, 170);
// context.font = 'bold 40pt Menlo';
context.font = `bold 40pt ${req.params.font}`;
text = req.params.subtitle;
context.fillText(text.split('{{n}}').join('\n'), 50, 270);
// context.font = 'bold 40pt Menlo';
context.font = `bold 40pt ${req.params.font}`;
text = `${req.params.protocol}${req.params.url.split('{{s}}').join('/')}`;
context.fillText(text, 50, height - 60);
const buffer = canvas.toBuffer('image/png');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': buffer.length
});
res.end(buffer);
})
})
app.get('/', (req, res) => {
res.render('index');
})
app.listen(port, () => {
console.log(`App listening on port ${port}`)
})