-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
85 lines (71 loc) · 2.72 KB
/
worker.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
import Bull from 'bull';
import { promisify } from 'util';
import imageThumbnail from 'image-thumbnail';
import dotenv from 'dotenv';
import dbClient from './utils/db';
import sendMail from './utils/mailer';
dotenv.config();
const fs = require('fs');
const { ObjectId } = require('mongodb');
const RedisOpts = {
port: process.env.RD_PORT || '6379',
host: process.env.RD_HOST || '127.0.0.1',
};
const fileQueue = new Bull('file-queue', { redis: RedisOpts });
const userQueue = new Bull('userQueue', { redis: RedisOpts });
// Generate 3 thumbnails with width = 100, 250 and 500 for each file
// found in file-queue jobs
fileQueue.process(async (job, done) => {
const { fileId, userId } = job.data;
if (!fileId) done(new Error('Missing fileId'));
if (!userId) done(new Error('Missing userId'));
const filesCollection = dbClient.db.collection('files');
const file = await filesCollection.findOne({
_id: ObjectId(fileId),
userId: ObjectId(userId),
});
if (!file) done(new Error('File not found'));
if (!fs.existsSync(file.localPath)) done(new Error('File not found'));
const writeFileAsync = promisify(fs.writeFile);
try {
let thumbnail = await imageThumbnail(file.localPath, { width: 100 });
await writeFileAsync(`${file.localPath}_100`, thumbnail);
thumbnail = await imageThumbnail(file.localPath, { width: 250 });
await writeFileAsync(`${file.localPath}_250`, thumbnail);
thumbnail = await imageThumbnail(file.localPath, { width: 500 });
await writeFileAsync(`${file.localPath}_500`, thumbnail);
done()
} catch (err) {
console.log(done(new Error(err)));
}
});
// Send "Welcome email" to new user
userQueue.process(async (job, done) => {
const { userId } = job.data;
if (!userId) done(new Error('Missing userId'));
const userCollection = dbClient.db.collection('users');
const user = await userCollection.findOne({ _id: ObjectId(userId) });
if (!user) done(new Error('User not found'));
console.log(`Welcome ${user.email}!`);
const username = user.email.split('@')[0];
const mailOptions = {
from: `ALX Files Manager <${process.env.EMAIL_ADD}>`,
to: user.email,
subject: 'Welcome to ALX-Files_Manager by Chigozirim',
html: [
'<div>',
`<h3>Hello ${username} 👋🏼,</h3>`,
'Welcome to <a href="https://github.com/iChigozirim/alx-files_manager">',
'ALX-Files_Manager</a>, ',
'a simple file management API built with Node.js by ',
'<a href="https://github.com/iChigozirim">Chigozirim Igweamaka</a>. ',
'We hope it meets your needs 🙂.',
'</div>',
].join(''),
};
try {
sendMail(mailOptions)
.then((result) => done(result))
.catch((error) => done(error));
} catch (error) { console.log(error); }
});