-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.js
45 lines (34 loc) · 1.29 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
import DBClient from './utils/db';
const fs = require('fs');
const Bull = require('bull');
const { ObjectId } = require('mongodb');
const imageThumbnail = require('image-thumbnail');
const fileQueue = new Bull('fileQueue');
const userQueue = new Bull('userQueue');
const createImageThumbnail = async (path, options) => {
try {
const thumbnail = await imageThumbnail(path, options);
const imagePath = `${path}_${options.width}`;
await fs.writeFileSync(imagePath, thumbnail);
} catch (error) {
console.log(error);
}
};
fileQueue.process(async (job) => {
const { fileId } = job.data;
if (!fileId) throw Error('Missing fileId');
const { userId } = job.data;
if (!userId) throw Error('Missing userId');
const file = await DBClient.files.findOne({ _id: ObjectId(fileId), userId: ObjectId(userId) });
if (!file) throw Error('File not found');
createImageThumbnail(file.localPath, { width: 500 });
createImageThumbnail(file.localPath, { width: 250 });
createImageThumbnail(file.localPath, { width: 100 });
});
userQueue.process(async (job) => {
const { userId } = job.data;
if (!userId) throw Error('Missing userId');
const user = await DBClient.users.findOne({ _id: ObjectId(userId) });
if (!user) throw Error('User not found');
console.log(`Welcome ${user.email}`);
});