-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
43 lines (33 loc) · 1.17 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
const Queue = require('bull');
const dbClient = require('./utils/db');
const thumbnail = require('image-thumbnail');
const fileQueue = new Queue('fileQueue');
fileQueue.process(async job => {
const { userId, fileId } = job.data;
if (!userId) {
throw new Error('Missing userId');
}
if (!fileId) {
throw new Error('Missing fileId');
}
const file = await dbClient.getFileById(fileId);
if (!file || file.userId !== userId) {
throw new Error('File not found');
}
if (file.type === 'image') {
try {
const options = { width: 500 };
const thumbnail500 = await thumbnail(file.localPath, options);
await fs.promises.writeFile(`${file.localPath}_500`, thumbnail500);
options.width = 250;
const thumbnail250 = await thumbnail(file.localPath, options);
await fs.promises.writeFile(`${file.localPath}_250`, thumbnail250);
options.width = 100;
const thumbnail100 = await thumbnail(file.localPath, options);
await fs.promises.writeFile(`${file.localPath}_100`, thumbnail100);
} catch (error) {
console.error('Error generating thumbnails:', error);
}
}
});
module.exports = { fileQueue };