Skip to content

Commit

Permalink
Use sharp to resize avatars on upload
Browse files Browse the repository at this point in the history
  • Loading branch information
rodrigok committed Feb 13, 2018
1 parent 131fda3 commit 56fd1cc
Showing 1 changed file with 28 additions and 19 deletions.
47 changes: 28 additions & 19 deletions packages/rocketchat-file-upload/server/lib/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import stream from 'stream';
import mime from 'mime-type/with-db';
import Future from 'fibers/future';
import { Cookies } from 'meteor/ostrio:cookies';
import sharp from 'sharp';

const cookie = new Cookies();

Expand Down Expand Up @@ -50,7 +51,6 @@ Object.assign(FileUpload, {
// filter: new UploadFS.Filter({
// onCheck: FileUpload.validateFileUpload
// }),
// transformWrite: FileUpload.avatarTransformWrite,
getPath(file) {
return `${ RocketChat.settings.get('uniqueID') }/avatars/${ file.userId }`;
},
Expand All @@ -59,34 +59,43 @@ Object.assign(FileUpload, {
};
},

avatarTransformWrite(readStream, writeStream/*, fileId, file*/) {
if (RocketChatFile.enabled === false || RocketChat.settings.get('Accounts_AvatarResize') !== true) {
return readStream.pipe(writeStream);
}
const height = RocketChat.settings.get('Accounts_AvatarSize');
const width = height;
return (file => RocketChat.Info.GraphicsMagick.enabled ? file: file.alpha('remove'))(RocketChatFile.gm(readStream).background('#FFFFFF')).resize(width, `${ height }^`).gravity('Center').crop(width, height).extent(width, height).stream('jpeg').pipe(writeStream);
},

avatarsOnValidate(file) {
if (RocketChatFile.enabled === false || RocketChat.settings.get('Accounts_AvatarResize') !== true) {
if (RocketChat.settings.get('Accounts_AvatarResize') !== true) {
return;
}

const tempFilePath = UploadFS.getTempFilePath(file._id);

const height = RocketChat.settings.get('Accounts_AvatarSize');
const width = height;
const future = new Future();

(file => RocketChat.Info.GraphicsMagick.enabled ? file: file.alpha('remove'))(RocketChatFile.gm(tempFilePath).background('#FFFFFF')).resize(width, `${ height }^`).gravity('Center').crop(width, height).extent(width, height).setFormat('jpeg').write(tempFilePath, Meteor.bindEnvironment(err => {
if (err != null) {
console.error(err);
}
const size = fs.lstatSync(tempFilePath).size;
this.getCollection().direct.update({_id: file._id}, {$set: {size}});
future.return();
const s = sharp(tempFilePath);
s.rotate();
// Get metadata to resize the image the first time to keep "inside" the dimensions
// then resize again to create the canvas around
s.metadata(Meteor.bindEnvironment((err, metadata) => {
s.toFormat(sharp.format.jpeg)
.resize(Math.min(height, metadata.width), Math.min(height, metadata.height))
.pipe(sharp()
.resize(height, height)
.background('#FFFFFF')
.embed()
)
// Use buffer to get the result in memory then replace the existing file
// There is no option to override a file using this library
.toBuffer()
.then(Meteor.bindEnvironment(outputBuffer => {
fs.writeFile(tempFilePath, outputBuffer, Meteor.bindEnvironment(err => {
if (err != null) {
console.error(err);
}
const size = fs.lstatSync(tempFilePath).size;
this.getCollection().direct.update({_id: file._id}, {$set: {size}});
future.return();
}));
}));
}));

return future.wait();
},

Expand Down

0 comments on commit 56fd1cc

Please sign in to comment.