mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 05:09:43 +02:00
fix: Change mechanics of file uploading
This commit is contained in:
parent
af71a96624
commit
542c4a845c
10 changed files with 375 additions and 317 deletions
|
@ -0,0 +1,63 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const rimraf = require('rimraf');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const sharp = require('sharp');
|
||||
|
||||
module.exports = {
|
||||
inputs: {
|
||||
file: {
|
||||
type: 'json',
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
|
||||
exits: {
|
||||
fileIsNotImage: {},
|
||||
},
|
||||
|
||||
async fn(inputs) {
|
||||
const image = sharp(inputs.file.fd);
|
||||
|
||||
try {
|
||||
await image.metadata();
|
||||
} catch (error) {
|
||||
throw 'fileIsNotImage';
|
||||
}
|
||||
|
||||
const dirname = uuid();
|
||||
const rootPath = path.join(sails.config.custom.projectBackgroundImagesPath, dirname);
|
||||
|
||||
fs.mkdirSync(rootPath);
|
||||
|
||||
try {
|
||||
await image.jpeg().toFile(path.join(rootPath, 'original.jpg'));
|
||||
|
||||
await image
|
||||
.resize(336, 200)
|
||||
.jpeg({
|
||||
quality: 100,
|
||||
chromaSubsampling: '4:4:4',
|
||||
})
|
||||
.toFile(path.join(rootPath, 'cover-336.jpg'));
|
||||
} catch (error1) {
|
||||
try {
|
||||
rimraf.sync(rootPath);
|
||||
} catch (error2) {
|
||||
console.warn(error2.stack); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
throw 'fileIsNotImage';
|
||||
}
|
||||
|
||||
try {
|
||||
rimraf.sync(inputs.file.fd);
|
||||
} catch (error) {
|
||||
console.warn(error.stack); // eslint-disable-line no-console
|
||||
}
|
||||
|
||||
return {
|
||||
dirname,
|
||||
};
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue