-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
77 lines (63 loc) · 1.88 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
import { ObjectId } from 'mongodb';
import imageThumbnail from 'image-thumbnail';
import fs from 'fs';
import { fileQueue } from './controllers/FilesController';
import { userQueue } from './controllers/UsersController';
import dbClient from './utils/db';
async function createThumbnail(originalFilePath) {
try {
const sizes = [500, 250, 100];
for await (const size of sizes) {
const thumbnail = await imageThumbnail(originalFilePath, { width: size, responseType: 'buffer' });
fs.writeFileSync(`${originalFilePath}_${size}`, thumbnail);
}
} catch (error) {
console.error(error);
}
}
fileQueue.process(async (job) => {
const { userId } = job.data;
const { fileId } = job.data;
if (!fileId) {
Promise.reject(new Error('Missing fileId'));
}
if (!userId) {
return Promise.reject(new Error('Missing userId'));
}
const filesCollection = dbClient.db.collection('files');
let document;
try {
document = await filesCollection.findOne({ userId: ObjectId(userId), _id: ObjectId(fileId) });
createThumbnail(document.localPath);
} catch (error) {
return Promise.reject(new Error('File not found'));
}
return Promise.resolve();
});
fileQueue.on('completed', () => {
console.log('complete');
});
fileQueue.on('failed', (job, err) => {
console.log('failed', err);
});
userQueue.process(async (job) => {
const { userId } = job.data;
if (!userId) {
return Promise.reject(new Error('Missing userId'));
}
const usersCollection = dbClient.db.collection('users');
let user;
try {
user = await usersCollection.findOne({ _id: ObjectId(userId) });
console.log('Welcome', user.email);
} catch (error) {
return Promise.reject(new Error('User not found'));
}
return Promise.resolve();
});
userQueue.on('completed', () => {
console.log('complete');
});
userQueue.on('failed', (job, err) => {
console.log('failed', err);
});