-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
74 lines (60 loc) · 1.86 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
import BullQueue from 'bull';
import imageThumbnail from 'image-thumbnail';
import { createReadStream, createWriteStream } from 'fs';
import { Readable } from 'stream';
import { ObjectId } from 'mongodb';
import dbClient from './utils/db';
import { THUMBNAIL_WIDTH, FILE_QUEUE, USER_QUEUE } from './utils/constants';
const fileQueue = new BullQueue(FILE_QUEUE);
const userQueue = new BullQueue(USER_QUEUE);
/**
* Generate thumbnail file of `width` from the image file at `filePath`.
*
* The new generated thumbnail file will be stored at `filePath` with `_{width}`
* suffix.
*
* e.g. `.../myImageFile` => `.../myImageFile_100`
*
* @param {string} filePath - the path of the image file
* @param {string} width - the width of the thumbnail
* @returns {Promise<void>}
*/
const generateThumbnails = async (filePath, width) => {
const fileReader = createReadStream(filePath);
const fileWriter = createWriteStream(`${filePath}_${width}`);
const thumbnail = await imageThumbnail(fileReader, {
width,
});
const reader = new Readable();
reader.push(thumbnail);
reader.push(null);
reader.pipe(fileWriter);
};
fileQueue.process(async (job) => {
const { fileId, userId } = job.data;
if (!fileId) {
throw new Error('Missing fileId');
}
if (!userId) {
throw new Error('Missing userId');
}
const file = await dbClient.files.findOne({
_id: new ObjectId(fileId),
userId: new ObjectId(userId),
});
if (!file) {
throw new Error('File not found');
}
THUMBNAIL_WIDTH.forEach((width) => generateThumbnails(file.localPath, width));
});
userQueue.process(async (job) => {
const { userId } = job.data;
if (!userId) {
throw new Error('Missing userId');
}
const user = await dbClient.users.findOne({ _id: new ObjectId(userId) });
if (!user) {
throw new Error('User not found');
}
console.log(`Welcome ${user.email}!`);
});