-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
43 lines (33 loc) · 1007 Bytes
/
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
import Bull from 'bull';
import dbClient from './utils/db';
import imageThumbnail from 'image-thumbnail';
import fs from 'fs';
import path from 'path';
const fileQueue = new Bull('fileQueue');
fileQueue.process(async (job, done) => {
const { userId, fileId } = job.data;
if (!fileId) {
throw new Error('Missing fileId');
}
if (!userId) {
throw new Error('Missing userId');
}
const file = await dbClient.db.collection('files').findOne({ _id: ObjectId(fileId), userId: ObjectId(userId) });
if (!file) {
throw new Error('File not found');
}
try {
const sizes = [500, 250, 100];
const filePath = file.localPath;
for (const size of sizes) {
const options = { width: size };
const thumbnail = await imageThumbnail(filePath, options);
const thumbPath = `${filePath}_${size}`;
fs.writeFileSync(thumbPath, thumbnail);
}
done();
} catch (error) {
done(new Error('Failed to generate thumbnails'));
}
});
export { fileQueue };