-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
306 lines (264 loc) · 8.06 KB
/
main.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
const express = require('express')
const bodyParser = require('body-parser');
const nodemailer = require("nodemailer");
const capitalize_first_letters = require('./helper_functions');
require('dotenv').config()
const id = process.env.ID;
const secret = process.env.SECRET;
const rtoken = process.env.RTOKEN;
const { google } = require("googleapis");
const OAuth2 = google.auth.OAuth2;
const oauth2Client = new OAuth2(
id,
secret, // Client Secret
"https://developers.google.com/oauthplayground" // Redirect URL
);
oauth2Client.setCredentials({
refresh_token: rtoken
});
const accessToken = oauth2Client.getAccessToken()
const app = express()
app.use(bodyParser.urlencoded({ extended: true }));
const port = 5001
const { createCanvas, loadImage, rsvgVersion } = require('canvas')
var transporter;
eventName = 'Cloud Computing'
var busy = 0;
function delay(i, image, names, emails) {
return new Promise((resolve, reject) => {
setTimeout(async function () {
try { //load image to canvas and write to canvas
var canvas = createCanvas(794, 1123)
var ctx = canvas.getContext('2d')
ctx.font = '20px Sans'
var text = ctx.measureText('123')
ctx.strokeStyle = 'rgba(0,0,0,0.5)'
ctx.beginPath()
ctx.lineTo(50, 102)
ctx.lineTo(50 + text.width, 102)
ctx.stroke()
ctx.drawImage(image, 0, 0, 794, 1123)
ctx.fillText('365', 380, 430)
ctx.font = '40px Sans'
var newname = capitalize_first_letters(names[i])
console.log(newname + ' ' + i)
ctx.fillText(newname, 180, 550)
ctx.font = '20px Sans'
ctx.fillText(eventName, 80, 660)
var d = new Date();
ctx.fillText(d.toISOString().slice(0, 10), 80, 730)
//to write canvas to image
const fs = require('fs')
const out = fs.createWriteStream(__dirname + `/test${i}.png`)
const stream = canvas.createPNGStream()
stream.pipe(out)
out.on('finish', () => console.log('The file was created' + `/test${i}.png`))
resolve();
} catch (e) { console.log(e) }
}, 2000)
});
}
async function sendMail(i, names, emails) {
return new Promise(async (resolve, reject) => {
setTimeout(async function () {
try {
var img = require("fs").readFileSync(__dirname + `/test${i}.png`);
var attachment = [{
'filename': `test${i}.png`,
'content': new Buffer(img)
}]
// send mail with defined transport object
let info = await transporter.sendMail({
from: "[email protected]", // sender address
to: emails[i], // list of receivers
// to:"[email protected]",
subject: `Your event certificate is here ${names[i]} ! ✔`, // Subject line
attachments: attachment,
text: "Thank You for attending the webinar", // plain text body
html: "<b> We hope you had a great learning experience with us.<br> Thank You for attending the webinar.</b>", // html body
});
console.log("Message sent: %s", info.messageId);
console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info));
resolve();
} catch (e) { console.log(e) }
}, 2000)
});
}
async function removeFiles(i, names, emails) {
return new Promise(async (resolve, reject) => {
setTimeout(async function () {
try {
// var img = require("fs").readFileSync(__dirname + `/test${i}.png`);
const fs = require("fs")
const pathToFile = __dirname + `/test${i}.png`
fs.unlink(pathToFile, function (err) {
if (err) {
throw err
} else {
console.log("Successfully deleted the file" + `/test${i}.png`)
}
})
resolve();
} catch (e) { console.log(e) }
}, 2000)
});
}
async function write(names, emails) {
return new Promise(async (resolve, reject) => {
setTimeout(async function () {
try {
transporter = nodemailer.createTransport({
service: 'gmail',
// port: 587,
// secure: false, // true for 465, false for other ports
auth: {
// user: '',//process.env.EMAIL,
// pass: ''//process.env.password
type: "OAuth2",
user: "[email protected]",
clientId: id,
clientSecret: secret,
refreshToken: rtoken,
accessToken: accessToken
},
tls: {
rejectUnauthorized: false
}
});
loadImage('Slide1.jpg').then(async (image) => {
for (var i = 0; i < names.length; i++) {
await delay(i, image, names, emails)
await sendMail(i, names, emails);
await removeFiles(i, names, emails)
if (i == names.length - 1) {
// busy=0
//handling the queue
if (!queue.isEmpty()) {
console.log('clearing q');
let dd = drivers_driver(queue.dequeue(), 1);
if (dd == 1)
console.log('Success. Certificates will be generated and delivered shortly');
else if (dd == 0)
console.log('Wrong syntax')
else if (dd == -1)
console.log("Empty JSON")
}
else {
busy = 0
}
}
}
})
resolve();
} catch (e) { console.log(e) }
}, 0)
});
}
class Queue {
constructor() {
this.items = [];
}
// add element to the queue
enqueue(element) {
return this.items.push(element);
}
// remove element from the queue
dequeue() {
if (this.items.length > 0) {
return this.items.shift();
}
}
// view the last element
peek() {
return this.items[this.items.length - 1];
}
// check if the queue is empty
isEmpty() {
return this.items.length == 0;
}
// the size of the queue
size() {
return this.items.length;
}
// empty the queue
clear() {
this.items = [];
}
}
let queue = new Queue();
function isEmpty(obj) {
for (var key in obj) {
if (obj.hasOwnProperty(key))
return false;
}
return true;
}
function drivers_driver(data, src) {
try {
busy = 1
let names = []
let emails = []
let k = 0;
if (isEmpty(JSON.parse(data))) {
console.log('empty Json')
if (src == 0)
busy = 0
return -1
}
f = JSON.parse(data)
for (var key in f) {
if (f.hasOwnProperty(key)) {
if (key == "eventName") {
console.log(f[key], 'has event name')
eventName = f[key]
continue;
}
console.log(key + " -> " + f[key].username);
names[k] = f[key].username
emails[k] = f[key].email
k++
}
}
console.log(names.length)
for (var i = 0; i < names.length; i++)
console.log(names[i] + ' ' + emails[i])
write(names, emails)
return 1;
}
catch (e) {
if (src == 0)
busy = 0;
return 0;
}
}
app.get('/', (req, res) => {
res.send("Please send a Post Request instead. Thank You for Using the service.")
})
app.post('/get-cert', (req, res) => {
console.log('just inside app.post')
console.log(busy)
try {
if (busy == 0) {
let dd = drivers_driver(req.body.data, 0);
if (dd == 1)
res.send('Success. Certificates will be generated and delivered shortly');
else if (dd = 0)
res.send('Wrong syntax')
else if (dd = -1)
res.send("Empty JSON")
}
else {
queue.enqueue(req.body.data)
console.log('New entry in q')
res.send('Request has been placed.')
}
}
catch (e) {
console.log(e)
busy = 0
res.send('Error ')
}
});
app.listen(process.env.PORT || port, () => {
console.log(`Example app listening`)
})