1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-19 21:29:43 +02:00
planka/server/api/controllers/users/upload-avatar.js

123 lines
2.5 KiB
JavaScript
Raw Normal View History

2019-08-31 04:07:25 +05:00
const fs = require('fs');
const path = require('path');
const util = require('util');
const stream = require('stream');
2020-02-25 02:10:27 +05:00
const { v4: uuid } = require('uuid');
2019-08-31 04:07:25 +05:00
const sharp = require('sharp');
const Errors = {
USER_NOT_FOUND: {
2020-04-03 00:35:25 +05:00
userNotFound: 'User not found',
},
2019-08-31 04:07:25 +05:00
};
const pipeline = util.promisify(stream.pipeline);
2019-08-31 04:07:25 +05:00
const createReceiver = () => {
const receiver = stream.Writable({ objectMode: true });
2019-08-31 04:07:25 +05:00
let firstFileHandled = false;
// eslint-disable-next-line no-underscore-dangle
receiver._write = async (file, receiverEncoding, done) => {
2019-08-31 04:07:25 +05:00
if (firstFileHandled) {
file.pipe(
new stream.Writable({
write(chunk, streamEncoding, callback) {
2019-08-31 04:07:25 +05:00
callback();
},
}),
2019-08-31 04:07:25 +05:00
);
return done();
}
firstFileHandled = true;
2020-04-08 21:12:58 +05:00
const resize = sharp().resize(100, 100).jpeg();
2019-08-31 04:07:25 +05:00
const transform = new stream.Transform({
transform(chunk, streamEncoding, callback) {
2019-08-31 04:07:25 +05:00
callback(null, chunk);
},
2019-08-31 04:07:25 +05:00
});
try {
await pipeline(file, resize, transform);
2019-08-31 04:07:25 +05:00
file.fd = `${uuid()}.jpg`; // eslint-disable-line no-param-reassign
2019-08-31 04:07:25 +05:00
await pipeline(
transform,
fs.createWriteStream(path.join(sails.config.custom.uploadsPath, file.fd)),
2019-08-31 04:07:25 +05:00
);
return done();
} catch (error) {
return done(error);
}
2019-08-31 04:07:25 +05:00
};
return receiver;
};
module.exports = {
inputs: {
id: {
type: 'string',
regex: /^[0-9]+$/,
required: true,
},
2019-08-31 04:07:25 +05:00
},
exits: {
2020-04-03 00:35:25 +05:00
userNotFound: {
responseType: 'notFound',
2019-08-31 04:07:25 +05:00
},
2020-04-03 00:35:25 +05:00
uploadError: {
responseType: 'unprocessableEntity',
},
2019-08-31 04:07:25 +05:00
},
async fn(inputs, exits) {
2019-08-31 04:07:25 +05:00
const { currentUser } = this.req;
let user;
if (currentUser.isAdmin) {
user = await sails.helpers.getUser(inputs.id);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
} else if (inputs.id !== currentUser.id) {
throw Errors.USER_NOT_FOUND; // Forbidden
} else {
user = currentUser;
}
this.req.file('file').upload(createReceiver(), async (error, files) => {
if (error) {
2020-04-03 00:35:25 +05:00
return exits.uploadError(error.message);
2019-08-31 04:07:25 +05:00
}
if (files.length === 0) {
2020-04-03 00:35:25 +05:00
return exits.uploadError('No file was uploaded');
2019-08-31 04:07:25 +05:00
}
user = await sails.helpers.updateUser(
user,
{
avatar: files[0].fd,
2019-08-31 04:07:25 +05:00
},
this.req,
2019-08-31 04:07:25 +05:00
);
if (!user) {
throw Errors.USER_NOT_FOUND;
}
return exits.success({
item: user.toJSON().avatar,
2019-08-31 04:07:25 +05:00
});
});
},
2019-08-31 04:07:25 +05:00
};