cleaned image upload a bit, no longer using the filename sent by the user

v1.18.x
Baris Soner Usakli 12 years ago
parent a5a44b2069
commit f2dfba33ad

@ -96,6 +96,7 @@ var user = require('./../user.js'),
}); });
return; return;
} }
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
var type = req.files.userPhoto.type; var type = req.files.userPhoto.type;
@ -120,28 +121,28 @@ var user = require('./../user.js'),
console.log(err); console.log(err);
} }
uploadUserPicture(req.user.uid, req.files.userPhoto.name, req.files.userPhoto.path, res); uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
}); });
}); });
}); });
function uploadUserPicture(uid, filename, tempPath, res) { function uploadUserPicture(uid, extension, tempPath, res) {
if(!filename){ if(!extension) {
res.send({ res.send({
error: 'Error uploading file! Error : Invalid file name!' error: 'Error uploading file! Error : Invalid extension!'
}); });
return; return;
} }
var filename = uid + '-profileimg' + extension;
var uploadPath = path.join(global.configuration['ROOT_DIRECTORY'], config.upload_path, filename);
filename = uid + '-' + filename; console.log('trying to upload to : '+ uploadPath);
var uploadPath = config.upload_path + filename;
console.log('trying to upload to : '+ global.configuration['ROOT_DIRECTORY'] + uploadPath);
var is = fs.createReadStream(tempPath); var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(global.configuration['ROOT_DIRECTORY'] + uploadPath); var os = fs.createWriteStream(uploadPath);
is.on('end', function() { is.on('end', function() {
fs.unlinkSync(tempPath); fs.unlinkSync(tempPath);
@ -158,13 +159,12 @@ var user = require('./../user.js'),
var im = require('node-imagemagick'); var im = require('node-imagemagick');
im.resize({ im.resize({
srcPath: global.configuration['ROOT_DIRECTORY'] + uploadPath, srcPath: uploadPath,
dstPath: global.configuration['ROOT_DIRECTORY'] + uploadPath, dstPath: uploadPath,
width: 128 width: 128
}, function(err, stdout, stderr){ }, function(err, stdout, stderr) {
if (err) { if (err) {
console.log(err); console.log(err);
throw err;
} }
}); });

Loading…
Cancel
Save