1
0
Fork 0
mirror of synced 2024-09-28 07:11:40 +12:00
budibase/packages/server/src/utilities/fileProcessor.js

31 lines
605 B
JavaScript
Raw Normal View History

2020-09-18 03:36:39 +12:00
const fs = require("fs")
const sharp = require("sharp")
const fsPromises = fs.promises
2020-09-17 23:45:28 +12:00
const FORMATS = {
IMAGES: ["png", "jpg", "jpeg", "gif", "svg", "tiff", "raw"],
}
2020-09-18 22:01:39 +12:00
async function processImage(file) {
const imgMeta = await sharp(file.path)
2020-09-17 23:45:28 +12:00
.resize(300)
2020-09-18 22:01:39 +12:00
.toFile(file.outputPath)
return {
...file,
...imgMeta,
}
2020-09-17 23:45:28 +12:00
}
2020-09-18 22:01:39 +12:00
async function process(file) {
2020-09-17 23:45:28 +12:00
if (FORMATS.IMAGES.includes(file.extension.toLowerCase())) {
2020-09-18 22:01:39 +12:00
return await processImage(file)
2020-09-17 23:45:28 +12:00
}
// No processing required
2020-09-18 22:01:39 +12:00
await fsPromises.copyFile(file.path, file.outputPath)
return file
2020-09-17 23:45:28 +12:00
}
2020-09-18 03:36:39 +12:00
exports.process = process