-
Notifications
You must be signed in to change notification settings - Fork 0
/
saveWorkImage.js
executable file
·32 lines (27 loc) · 978 Bytes
/
saveWorkImage.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
import sharp from 'sharp';
const WORK_PATH = 'public/works';
export default function saveWorkImage(req) {
return new Promise((resolve) => {
const { chromosome, hash, image } = req.body;
const inputFullFilename = `${WORK_PATH}/full/${hash}.jpg`;
const mediumFullFilename = `${WORK_PATH}/medium/${hash}.jpg`;
const thumbFullFilename = `${WORK_PATH}/thumb/${hash}.jpg`;
const base64Data = image.replace(/^data:image\/jpeg;base64,/, '');
require('fs').writeFile(inputFullFilename, base64Data, 'base64', function(err) {
sharp(inputFullFilename)
.jpeg({ quality: 90 })
.resize(900, 900)
.toFile(mediumFullFilename, function (err) {
sharp(inputFullFilename)
.jpeg({ quality: 90 })
.resize(450, 450)
.toFile(thumbFullFilename, function (err) {
resolve({
hash,
chromosome
});
});
});
});
});
}