Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] Fix black background on transparent avatars #7168

Merged
merged 7 commits into from
Aug 23, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 RocketChatFile.gm(readStream).background('#ffffff').alpha('remove').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) => {
RocketChatFile.gm(tempFilePath).background('#FFFFFF').alpha('remove').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) {
return store.get(file, req, res, next);
}
res.writeHead(404);
res.end();
}
});

Expand Down