Skip to content

Commit

Permalink
Merge pull request #7168 from RocketChat/bug-avatar
Browse files Browse the repository at this point in the history
[FIX] Fix black background on transparent avatars
  • Loading branch information
engelgabriel authored Aug 23, 2017
2 parents abbc1ba + fd4e76d commit 865d05a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 33 deletions.
25 changes: 9 additions & 16 deletions packages/rocketchat-file-upload/server/config/GridFS.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,17 @@ const getByteRange = function(header) {
};


// code from: https://github.com/jalik/jalik-ufs/blob/master/ufs-server.js#L91
// code from: https://github.com/jalik/jalik-ufs/blob/master/ufs-server.js#L310
const readFromGridFS = function(storeName, fileId, file, headers, req, res) {
const store = UploadFS.getStore(storeName);
const rs = store.getReadStream(fileId, file);
const ws = new stream.PassThrough();

rs.on('error', function(err) {
[rs, ws].forEach(stream => stream.on('error', function(err) {
store.onReadError.call(store, err, fileId, file);
res.end();
});
ws.on('error', function(err) {
store.onReadError.call(store, err, fileId, file);
res.end();
});
}));

ws.on('close', function() {
// Close output stream at the end
ws.emit('end');
Expand All @@ -89,7 +86,6 @@ const readFromGridFS = function(storeName, fileId, file, headers, req, res) {

// Transform stream
store.transformRead(rs, ws, fileId, file, req, headers);

const range = getByteRange(req.headers.range);
let out_of_range = false;
if (range) {
Expand Down Expand Up @@ -193,15 +189,12 @@ new FileUploadClass({

get(file, req, res) {
const reqModifiedHeader = req.headers['if-modified-since'];
if (reqModifiedHeader) {
if (reqModifiedHeader === (file.uploadedAt && file.uploadedAt.toUTCString())) {
res.setHeader('Last-Modified', reqModifiedHeader);
res.writeHead(304);
res.end();
return;
}
if (reqModifiedHeader && reqModifiedHeader === (file.uploadedAt && file.uploadedAt.toUTCString())) {
res.setHeader('Last-Modified', reqModifiedHeader);
res.writeHead(304);
res.end();
return;
}

file = FileUpload.addExtensionTo(file);
const headers = {
'Cache-Control': 'public, max-age=0',
Expand Down
29 changes: 12 additions & 17 deletions packages/rocketchat-file-upload/server/lib/FileUpload.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,32 +53,29 @@ Object.assign(FileUpload, {
}
const height = RocketChat.settings.get('Accounts_AvatarSize');
const width = height;
return RocketChatFile.gm(readStream).background('#ffffff').resize(width, `${ height }^`).gravity('Center').crop(width, height).extent(width, height).stream('jpeg').pipe(writeStream);
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) {
return;
}

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

const fut = new Future();
const tempFilePath = UploadFS.getTempFilePath(file._id);

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

RocketChatFile.gm(tmpFile).background('#ffffff').resize(width, `${ height }^`).gravity('Center').crop(width, height).extent(width, height).setFormat('jpeg').write(tmpFile, Meteor.bindEnvironment((err) => {
(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(tmpFile).size;
const size = fs.lstatSync(tempFilePath).size;
this.getCollection().direct.update({_id: file._id}, {$set: {size}});
fut.return();
future.return();
}));

return fut.wait();
return future.wait();
},

uploadsTransformWrite(readStream, writeStream, fileId, file) {
Expand Down Expand Up @@ -183,18 +180,16 @@ Object.assign(FileUpload, {
if (this.handlers[handlerName] == null) {
console.error(`Upload handler "${ handlerName }" does not exists`);
}

return this.handlers[handlerName];
},

get(file, req, res, next) {
if (file.store && this.handlers && this.handlers[file.store] && this.handlers[file.store].get) {
this.handlers[file.store].get(file, req, res, next);
} else {
res.writeHead(404);
res.end();
return;
const store = this.getStoreByName(file.store);
if (store && store.get) {
return store.get(file, req, res, next);
}
res.writeHead(404);
res.end();
}
});

Expand Down

0 comments on commit 865d05a

Please sign in to comment.