Skip to content

Commit

Permalink
fix: Change mechanics of file uploading
Browse files Browse the repository at this point in the history
  • Loading branch information
meltyshev committed Aug 26, 2022
1 parent c940805 commit cece225
Show file tree
Hide file tree
Showing 10 changed files with 365 additions and 307 deletions.
61 changes: 37 additions & 24 deletions server/api/controllers/attachments/create.js
@@ -1,10 +1,16 @@
const util = require('util');
const { v4: uuid } = require('uuid');

const Errors = {
NOT_ENOUGH_RIGHTS: {
notEnoughRights: 'Not enough rights',
},
CARD_NOT_FOUND: {
cardNotFound: 'Card not found',
},
NO_FILE_WAS_UPLOADED: {
noFileWasUploaded: 'No file was uploaded',
},
};

module.exports = {
Expand All @@ -27,6 +33,9 @@ module.exports = {
cardNotFound: {
responseType: 'notFound',
},
noFileWasUploaded: {
responseType: 'unprocessableEntity',
},
uploadError: {
responseType: 'unprocessableEntity',
},
Expand All @@ -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)),
);

let files;
try {
files = await upload({
saveAs: uuid(),
maxBytes: null,
});
} catch (error) {
return exits.uploadError(error.message); // TODO: add error
}

if (files.length === 0) {
return exits.uploadError('No file was uploaded');
}
if (files.length === 0) {
throw Errors.NO_FILE_WAS_UPLOADED;
}

const file = files[0];
const file = _.last(files);
const fileData = await sails.helpers.attachments.processUploadedFile(file);

const attachment = await sails.helpers.attachments.createOne(
{
...file.extra,
filename: file.filename,
},
currentUser,
card,
inputs.requestId,
this.req,
);
const attachment = await sails.helpers.attachments.createOne(
fileData,
currentUser,
card,
inputs.requestId,
this.req,
);

return exits.success({
item: attachment.toJSON(),
});
});
return exits.success({
item: attachment,
});
},
};
6 changes: 3 additions & 3 deletions server/api/controllers/cards/index.js
Expand Up @@ -23,7 +23,7 @@ module.exports = {
},
},

async fn(inputs, exits) {
async fn(inputs) {
const { currentUser } = this.req;

const { board } = await sails.helpers.boards
Expand Down Expand Up @@ -61,14 +61,14 @@ module.exports = {
const tasks = await sails.helpers.cards.getTasks(cardIds);
const attachments = await sails.helpers.cards.getAttachments(cardIds);

return exits.success({
return {
items: cards,
included: {
cardMemberships,
cardLabels,
tasks,
attachments,
},
});
};
},
};
78 changes: 57 additions & 21 deletions server/api/controllers/projects/update-background-image.js
@@ -1,7 +1,17 @@
const util = require('util');
const rimraf = require('rimraf');
const { v4: uuid } = require('uuid');

const Errors = {
PROJECT_NOT_FOUND: {
projectNotFound: 'Project not found',
},
NO_FILE_WAS_UPLOADED: {
noFileWasUploaded: 'No file was uploaded',
},
FILE_IS_NOT_IMAGE: {
fileIsNotImage: 'File is not image',
},
};

module.exports = {
Expand All @@ -17,6 +27,12 @@ module.exports = {
projectNotFound: {
responseType: 'notFound',
},
noFileWasUploaded: {
responseType: 'unprocessableEntity',
},
fileIsNotImage: {
responseType: 'unprocessableEntity',
},
uploadError: {
responseType: 'unprocessableEntity',
},
Expand All @@ -37,32 +53,52 @@ module.exports = {
throw Errors.PROJECT_NOT_FOUND; // Forbidden
}

this.req
.file('file')
.upload(sails.helpers.utils.createProjectBackgroundImageReceiver(), 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');
}
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;
}

project = await sails.helpers.projects.updateOne(
project,
{
backgroundImageDirname: files[0].extra.dirname,
},
this.req,
);
const file = _.last(files);

if (!project) {
throw Errors.PROJECT_NOT_FOUND;
const fileData = await sails.helpers.projects
.processUploadedBackgroundImageFile(file)
.intercept('fileIsNotImage', () => {
try {
rimraf.sync(file.fd);
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}

return exits.success({
item: project.toJSON(),
});
return Errors.FILE_IS_NOT_IMAGE;
});

project = await sails.helpers.projects.updateOne(
project,
{
backgroundImageDirname: fileData.dirname,
},
this.req,
);

if (!project) {
throw Errors.PROJECT_NOT_FOUND;
}

return exits.success({
item: project,
});
},
};
80 changes: 58 additions & 22 deletions server/api/controllers/users/update-avatar.js
@@ -1,7 +1,17 @@
const util = require('util');
const rimraf = require('rimraf');
const { v4: uuid } = require('uuid');

const Errors = {
USER_NOT_FOUND: {
userNotFound: 'User not found',
},
NO_FILE_WAS_UPLOADED: {
noFileWasUploaded: 'No file was uploaded',
},
FILE_IS_NOT_IMAGE: {
fileIsNotImage: 'File is not image',
},
};

module.exports = {
Expand All @@ -17,6 +27,12 @@ module.exports = {
userNotFound: {
responseType: 'notFound',
},
noFileWasUploaded: {
responseType: 'unprocessableEntity',
},
fileIsNotImage: {
responseType: 'unprocessableEntity',
},
uploadError: {
responseType: 'unprocessableEntity',
},
Expand All @@ -38,33 +54,53 @@ module.exports = {
user = currentUser;
}

this.req
.file('file')
.upload(sails.helpers.utils.createUserAvatarReceiver(), 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');
}
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;
}

user = await sails.helpers.users.updateOne(
user,
{
avatarDirname: files[0].extra.dirname,
},
currentUser,
this.req,
);
const file = _.last(files);

if (!user) {
throw Errors.USER_NOT_FOUND;
const fileData = await sails.helpers.users
.processUploadedAvatarFile(file)
.intercept('fileIsNotImage', () => {
try {
rimraf.sync(file.fd);
} catch (error) {
console.warn(error.stack); // eslint-disable-line no-console
}

return exits.success({
item: user.toJSON(),
});
return Errors.FILE_IS_NOT_IMAGE;
});

user = await sails.helpers.users.updateOne(
user,
{
avatarDirname: fileData.dirname,
},
currentUser,
this.req,
);

if (!user) {
throw Errors.USER_NOT_FOUND;
}

return exits.success({
item: user,
});
},
};

0 comments on commit cece225

Please sign in to comment.