Skip to content

Commit

Permalink
fixed #4186
Browse files Browse the repository at this point in the history
  • Loading branch information
julianlam committed Feb 15, 2016
1 parent 30736c0 commit 10024d7
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/user/picture.js
Expand Up @@ -3,6 +3,7 @@
var async = require('async'),
path = require('path'),
fs = require('fs'),
os = require('os'),
nconf = require('nconf'),
crypto = require('crypto'),
winston = require('winston'),
Expand Down Expand Up @@ -160,20 +161,23 @@ module.exports = function(User) {
md5sum.update(data.imageData);
md5sum = md5sum.digest('hex');

tempPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), md5sum);
data.file = {
path: path.join(os.tmpdir(), md5sum)
};

var buffer = new Buffer(data.imageData.slice(data.imageData.indexOf('base64') + 7), 'base64');

fs.writeFile(tempPath, buffer, {
fs.writeFile(data.file.path, buffer, {
encoding: 'base64'
}, next);
},
function(next) {
file.isFileTypeAllowed(tempPath, next);
file.isFileTypeAllowed(data.file.path, next);
},
function(tempPath, next) {
var image = {
name: 'profileCover',
path: data.file ? data.file.path : tempPath,
path: tempPath,
uid: data.uid
};

Expand All @@ -198,7 +202,7 @@ module.exports = function(User) {
User.setUserField(data.uid, 'cover:url', uploadData.url, next);
},
function(next) {
require('fs').unlink(data.file ? data.file.path : tempPath, function(err) {
fs.unlink(data.file.path, function(err) {
if (err) {
winston.error(err);
}
Expand All @@ -207,7 +211,7 @@ module.exports = function(User) {
}
], function(err) {
if (err) {
return fs.unlink(tempPath, function(unlinkErr) {
return fs.unlink(data.file.path, function(unlinkErr) {
callback(err); // send back the original error
});
}
Expand Down

0 comments on commit 10024d7

Please sign in to comment.