-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
182 lines (155 loc) · 5.49 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
const { createCanvas, Image, loadImage, registerFont } = require('canvas');
const express = require('express');
const app = express();
const path = require('path');
const fetchMovementInfo = require('./methods/fetchMovementInfo');
const port = process.env.PORT || 4000;
const config = require('./config/apiConfig');
require('dotenv').config();
const publicMovement = {
driver: {
en: 'Driver',
es: 'Conductor'
},
in: {
en: 'Check In',
es: 'Entrada'
},
out: {
en: 'Check Out',
es: 'Salida'
},
purpose: {
en: 'Purpose',
es: 'Proposito'
},
trailer: {
en: 'Trailer',
es: 'Remolque'
},
truck: {
en: 'Truck',
es: 'Tractor'
}
}
// Register fonts
registerFont(path.join(__dirname, '/assets/fonts/inter/Inter-Black.ttf'), { family: 'Inter', weight: 800 });
registerFont(path.join(__dirname, '/assets/fonts/inter/Inter-Regular.ttf'), { family: 'Inter', weight: 400 });
app.get('/:id', async (req, res) => {
// Data
const { darkMode, lang } = req.query;
const id = req.params.id;
const movementInfo = await fetchMovementInfo(id);
const direction = movementInfo?.['direction']?.toLowerCase();
const driverName = movementInfo?.['driver_name'];
const guest = movementInfo?.['truck_type'] === 'VEHICLE';
const purpose = movementInfo?.['purpose'];
const trailerStatus = movementInfo?.['status']
const trailerNumber = movementInfo?.['trailer_number'];
const truckNumber = movementInfo?.['truck_number'];
// Image params
const height = 600;
const width = 1200;
const canvas = createCanvas(width, height);
const context = canvas.getContext('2d');
const lineWidth = 6;
const logoRatio = .75;
const logoSize = {
height: 75 * logoRatio,
width: 275 * logoRatio
}
const labelFontSize = '26pt';
const textFontSize = '30pt';
const title = publicMovement[direction]?.[lang || 'en'];
const baseX = 100;
let buffer;
let fillColor = darkMode ? '#1D1D1D' : '#FFF';
let firstLabel;
let firstText;
let gridImage;
let gridPattern;
let lastLabel;
let lastText;
let lines = guest ? 1 : 2;
let logoImage;
let logoY;
let movementImage;
let movementImageRatio;
let photoId;
let startY = 140;
let textColor = darkMode ? '#FFF' : '#353535';
if (lines === 2) {
startY = 70;
}
if (trailerStatus === 'LOADED') {
photoId = movementInfo?.['left_trailer_image_shared_id'];
} else if (trailerStatus === 'EMPTY') {
photoId = movementInfo?.['inside_trailer_picture_shared_id'];
} else {
photoId = movementInfo?.['truck_image_shared_id'];
}
context.fillStyle = fillColor;
context.fillRect(0, 0, width, height);
if (darkMode) {
context.globalAlpha = 0.25;
}
gridImage = await loadImage(`${__dirname}/assets/img/dot-grid.png`);
gridPattern = context.createPattern(gridImage, 'repeat');
context.fillStyle = gridPattern;
context.fillRect(0, 0, canvas.width, canvas.height);
if (darkMode) {
context.globalAlpha = 1;
}
context.font = '800 54pt Inter';
context.textAlign = 'left';
context.textBaseline = 'top'
context.fillStyle = textColor;
context.fillText(title, baseX, startY);
if (guest) {
firstLabel = publicMovement.driver?.[lang || 'en'];
firstText = driverName;
lastLabel = publicMovement.purpose?.[lang || 'en'] || '';
lastText = purpose;
} else {
firstLabel = publicMovement.truck?.[lang || 'en'];
firstText = truckNumber;
lastLabel = publicMovement.trailer?.[lang || 'en'] || '';
lastText = trailerNumber;
}
if (firstText) {
context.font = `400 ${labelFontSize} Inter`;
context.fillText(firstLabel, baseX, startY + 120);
context.font = `800 ${textFontSize} Inter`;
context.fillText(firstText, baseX, startY + 170);
}
if (lastText) {
context.font = `400 ${labelFontSize} Inter`;
context.fillText(lastLabel, baseX, startY + 250);
context.font = `800 ${textFontSize} Inter`;
context.fillText(lastText, baseX, startY + 300);
}
logoImage = await loadImage(`${__dirname}/assets/img/logo${darkMode ? '-dark' : ''}.png`);
logoY = lines === 1 ? startY + 260 : startY + 400;
context.drawImage(logoImage, baseX, logoY, logoSize.width, logoSize.height);
if (photoId) {
movementImage = await loadImage(`${config.apiEndpoint}/api/public/shared/attachment/${photoId}?size=MEDIUM`);
movementImageRatio = movementImage.width > movementImage.height ? movementImage.width / movementImage.height : movementImage.height / movementImage.width;
if (movementImage.width > movementImage.height) {
context.drawImage(movementImage, (canvas.width / 2), 0, canvas.height * movementImageRatio, canvas.height);
} else {
context.drawImage(movementImage, (canvas.width / 2), ((canvas.height - movementImage.height) / 2), canvas.width / 2, canvas.height * 2);
}
}
context.beginPath();
context.fillStyle = '#00A9DE';
context.fillRect((canvas.width / 2) - (lineWidth / 2), 0, lineWidth, canvas.height);
buffer = canvas.toBuffer('image/png');
res.writeHead(200, {
'Content-Type': 'image/png',
'Content-Length': buffer.length
});
res.end(buffer);
})
app.listen(port, () => {
console.log(`Example app listening at http://localhost:${port}`)
})