2020-09-17 17:36:39 +02:00
|
|
|
const fs = require("fs")
|
2020-10-14 14:21:43 +02:00
|
|
|
const sharp = require("sharp")
|
2020-09-17 17:36:39 +02:00
|
|
|
const fsPromises = fs.promises
|
2020-09-17 13:45:28 +02:00
|
|
|
|
|
|
|
const FORMATS = {
|
|
|
|
IMAGES: ["png", "jpg", "jpeg", "gif", "svg", "tiff", "raw"],
|
|
|
|
}
|
|
|
|
|
2020-09-18 12:01:39 +02:00
|
|
|
async function processImage(file) {
|
2020-10-14 14:21:43 +02:00
|
|
|
const imgMeta = await sharp(file.path)
|
|
|
|
.resize(300)
|
|
|
|
.toFile(file.outputPath)
|
|
|
|
return {
|
|
|
|
...file,
|
|
|
|
...imgMeta,
|
|
|
|
}
|
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-09-18 12:01:39 +02:00
|
|
|
return await processImage(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
|