2020-09-17 17:36:39 +02:00
|
|
|
const fs = require("fs")
|
2020-10-21 14:00:23 +02:00
|
|
|
const jimp = require("jimp")
|
2020-09-17 17:36:39 +02:00
|
|
|
const fsPromises = fs.promises
|
2020-09-17 13:45:28 +02:00
|
|
|
|
|
|
|
const FORMATS = {
|
2020-10-21 14:00:23 +02:00
|
|
|
IMAGES: ["png", "jpg", "jpeg", "gif", "bmp", "tiff"],
|
2020-09-17 13:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-10-21 14:00:23 +02:00
|
|
|
function processImage(file) {
|
|
|
|
return jimp.read(file.path).then(img => {
|
|
|
|
return img.resize(300, jimp.AUTO).write(file.outputPath)
|
|
|
|
})
|
2020-09-17 13:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-18 12:01:39 +02:00
|
|
|
async function process(file) {
|
2020-09-17 13:45:28 +02:00
|
|
|
if (FORMATS.IMAGES.includes(file.extension.toLowerCase())) {
|
2020-10-21 14:00:23 +02:00
|
|
|
await processImage(file)
|
|
|
|
return file
|
2020-09-17 13:45:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// No processing required
|
2020-09-18 12:01:39 +02:00
|
|
|
await fsPromises.copyFile(file.path, file.outputPath)
|
|
|
|
return file
|
2020-09-17 13:45:28 +02:00
|
|
|
}
|
|
|
|
|
2020-09-17 17:36:39 +02:00
|
|
|
exports.process = process
|