-
Notifications
You must be signed in to change notification settings - Fork 0
/
media.js
55 lines (45 loc) · 1.12 KB
/
media.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
const sharp = require('sharp');
const util = require('util');
const path = require('path');
const stream = require('stream');
const fs = require('fs');
const fsPromises = require('fs').promises;
const pipeline = util.promisify(stream.pipeline);
let media = {}
media.resizeImage = async function(options, ctx) {
if(!['image/jpeg', 'image/png'].includes(options.mimetype)) {
// remove file from uploads dir
try {
fs.unlinkSync(options.filepath)
//file removed
} catch(err) {
console.error(err)
}
throw('bad image type: ' + options.mimetype)
}
const resizer =
sharp()
.rotate()
.resize(options.width, options.height)
.png();
resizer.options.limitInputPixels = 0
console.log(options)
await pipeline (
fs.createReadStream(options.filepath),
resizer,
fs.createWriteStream(path.join('public', 'images', options.filename))
)
try {
// remove file from uploads dir
try {
fs.unlinkSync(options.filepath)
//file removed
} catch(err) {
console.error(err)
}
} catch(e) {
console.log('media import failed', e)
throw('Media import failed')
}
}
module.exports = media