2022-08-26 02:45:27 +02:00
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
const rimraf = require('rimraf');
|
2022-08-26 16:02:47 +02:00
|
|
|
const moveFile = require('move-file');
|
2022-08-26 02:45:27 +02:00
|
|
|
const filenamify = require('filenamify');
|
|
|
|
const { v4: uuid } = require('uuid');
|
|
|
|
const sharp = require('sharp');
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
inputs: {
|
|
|
|
file: {
|
|
|
|
type: 'json',
|
|
|
|
required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
async fn(inputs) {
|
|
|
|
const dirname = uuid();
|
|
|
|
|
|
|
|
// FIXME: https://github.com/sindresorhus/filenamify/issues/13
|
|
|
|
const filename = filenamify(inputs.file.filename);
|
|
|
|
|
|
|
|
const rootPath = path.join(sails.config.custom.attachmentsPath, dirname);
|
|
|
|
const filePath = path.join(rootPath, filename);
|
|
|
|
|
|
|
|
fs.mkdirSync(rootPath);
|
2022-08-26 16:02:47 +02:00
|
|
|
await moveFile(inputs.file.fd, filePath);
|
2022-08-26 02:45:27 +02:00
|
|
|
|
2023-01-17 22:14:39 +01:00
|
|
|
let image = sharp(filePath, {
|
2022-12-24 00:47:59 +01:00
|
|
|
animated: true,
|
|
|
|
});
|
2022-08-26 02:45:27 +02:00
|
|
|
|
2022-12-24 00:47:59 +01:00
|
|
|
let metadata;
|
2022-08-26 02:45:27 +02:00
|
|
|
try {
|
|
|
|
metadata = await image.metadata();
|
|
|
|
} catch (error) {} // eslint-disable-line no-empty
|
|
|
|
|
|
|
|
const fileData = {
|
|
|
|
dirname,
|
|
|
|
filename,
|
|
|
|
image: null,
|
|
|
|
name: inputs.file.filename,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (metadata && !['svg', 'pdf'].includes(metadata.format)) {
|
|
|
|
const thumbnailsPath = path.join(rootPath, 'thumbnails');
|
|
|
|
fs.mkdirSync(thumbnailsPath);
|
|
|
|
|
2023-01-17 22:14:39 +01:00
|
|
|
let { width, pageHeight: height = metadata.height } = metadata;
|
|
|
|
if (metadata.orientation && metadata.orientation > 4) {
|
|
|
|
[image, width, height] = [image.rotate(), height, width];
|
|
|
|
}
|
|
|
|
|
2022-12-26 22:43:21 +01:00
|
|
|
const isPortrait = height > width;
|
2022-12-24 02:33:03 +01:00
|
|
|
const thumbnailsExtension = metadata.format === 'jpeg' ? 'jpg' : metadata.format;
|
2022-12-24 00:47:59 +01:00
|
|
|
|
2022-08-26 02:45:27 +02:00
|
|
|
try {
|
|
|
|
await image
|
2022-12-26 22:43:21 +01:00
|
|
|
.resize(
|
|
|
|
256,
|
|
|
|
isPortrait ? 320 : undefined,
|
|
|
|
width < 256 || (isPortrait && height < 320)
|
|
|
|
? {
|
|
|
|
kernel: sharp.kernel.nearest,
|
|
|
|
}
|
|
|
|
: undefined,
|
|
|
|
)
|
2022-12-24 02:33:03 +01:00
|
|
|
.toFile(path.join(thumbnailsPath, `cover-256.${thumbnailsExtension}`));
|
2022-08-26 02:45:27 +02:00
|
|
|
|
2022-12-24 00:47:59 +01:00
|
|
|
fileData.image = {
|
2022-12-24 02:33:03 +01:00
|
|
|
width,
|
|
|
|
height,
|
|
|
|
thumbnailsExtension,
|
2022-12-24 00:47:59 +01:00
|
|
|
};
|
2022-08-26 02:45:27 +02:00
|
|
|
} catch (error1) {
|
|
|
|
try {
|
|
|
|
rimraf.sync(thumbnailsPath);
|
|
|
|
} catch (error2) {
|
|
|
|
console.warn(error2.stack); // eslint-disable-line no-console
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fileData;
|
|
|
|
},
|
|
|
|
};
|