mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-07 06:25:50 +01:00
cleaned image upload a bit, no longer using the filename sent by the user
This commit is contained in:
@@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
filename = uid + '-' + filename;
|
var filename = uid + '-profileimg' + extension;
|
||||||
var uploadPath = config.upload_path + filename;
|
var uploadPath = path.join(global.configuration['ROOT_DIRECTORY'], config.upload_path, filename);
|
||||||
|
|
||||||
console.log('trying to upload to : '+ global.configuration['ROOT_DIRECTORY'] + uploadPath);
|
console.log('trying to upload to : '+ 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;
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user