mirror of
https://github.com/plankanban/planka.git
synced 2025-07-19 13:19:44 +02:00
fix: Change mechanics of file uploading
This commit is contained in:
parent
c940805f14
commit
cece2254d7
10 changed files with 375 additions and 317 deletions
79
server/api/helpers/attachments/process-uploaded-file.js
Normal file
79
server/api/helpers/attachments/process-uploaded-file.js
Normal file
|
@ -0,0 +1,79 @@
|
|||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
const util = require('util');
|
||||
const rimraf = require('rimraf');
|
||||
const filenamify = require('filenamify');
|
||||
const { v4: uuid } = require('uuid');
|
||||
const sharp = require('sharp');
|
||||
|
||||
const rename = util.promisify(fs.rename);
|
||||
|
||||
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);
|
||||
await rename(inputs.file.fd, filePath);
|
||||
|
||||
const image = sharp(filePath);
|
||||
let metadata;
|
||||
|
||||
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);
|
||||
|
||||
try {
|
||||
await image
|
||||
.resize(
|
||||
metadata.height > metadata.width
|
||||
? {
|
||||
width: 256,
|
||||
height: 320,
|
||||
}
|
||||
: {
|
||||
width: 256,
|
||||
},
|
||||
)
|
||||
.jpeg({
|
||||
quality: 100,
|
||||
chromaSubsampling: '4:4:4',
|
||||
})
|
||||
.toFile(path.join(thumbnailsPath, 'cover-256.jpg'));
|
||||
|
||||
fileData.image = _.pick(metadata, ['width', 'height']);
|
||||
} catch (error1) {
|
||||
try {
|
||||
rimraf.sync(thumbnailsPath);
|
||||
} catch (error2) {
|
||||
console.warn(error2.stack); // eslint-disable-line no-console
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fileData;
|
||||
},
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue