1
0
Fork 0
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:
Maksim Eltyshev 2022-08-26 02:45:27 +02:00
parent af71a96624
commit 542c4a845c
10 changed files with 375 additions and 317 deletions

View file

@ -1,3 +1,6 @@
const util = require('util');
const { v4: uuid } = require('uuid');
const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
@ -5,6 +8,9 @@ const Errors = {
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
NO_FILE_WAS_UPLOADED: {
noFileWasUploaded: 'No file was uploaded',
},
};
module.exports = {
@ -27,6 +33,9 @@ module.exports = {
cardNotFound: {
responseType: 'notFound',
},
noFileWasUploaded: {
responseType: 'unprocessableEntity',
},
uploadError: {
responseType: 'unprocessableEntity',
},
@ -52,33 +61,37 @@ module.exports = {
throw Errors.NOT_ENOUGH_RIGHTS;
}
this.req
.file('file')
.upload(sails.helpers.utils.createAttachmentReceiver(), async (error, files) => {
if (error) {
return exits.uploadError(error.message);
}
const upload = util.promisify((options, callback) =>
this.req.file('file').upload(options, (error, files) => callback(error, files)),
);
if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
const file = files[0];
const attachment = await sails.helpers.attachments.createOne(
{
...file.extra,
filename: file.filename,
},
currentUser,
card,
inputs.requestId,
this.req,
);
return exits.success({
item: attachment.toJSON(),
});
let files;
try {
files = await upload({
saveAs: uuid(),
maxBytes: null,
});
} catch (error) {
return exits.uploadError(error.message); // TODO: add error
}
if (files.length === 0) {
throw Errors.NO_FILE_WAS_UPLOADED;
}
const file = _.last(files);
const fileData = await sails.helpers.attachments.processUploadedFile(file);
const attachment = await sails.helpers.attachments.createOne(
fileData,
currentUser,
card,
inputs.requestId,
this.req,
);
return exits.success({
item: attachment,
});
},
};