moved duplicate upload validation code into its own fn

v1.18.x
psychobunny 11 years ago
parent 0bbff06cfa
commit 641ec3ef82

@ -229,6 +229,19 @@ adminController.groups.get = function(req, res, next) {
}); });
}; };
function validateUpload(type, allowedTypes) {
if (allowedTypes.indexOf(type) === -1) {
var err = {
error: 'Invalid image type. Allowed types are: ' + allowedTypes.join(',')
};
res.send(req.xhr ? err : JSON.stringify(err));
return false;
}
return true;
}
adminController.uploads.uploadImage = function(filename, req, res) { adminController.uploads.uploadImage = function(filename, req, res) {
function done(err, image) { function done(err, image) {
var er, rs; var er, rs;
@ -252,65 +265,48 @@ adminController.uploads.uploadImage = function(filename, req, res) {
adminController.uploads.uploadCategoryPicture = function(req, res, next) { adminController.uploads.uploadCategoryPicture = function(req, res, next) {
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'], var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'],
params = null, er; params = null;
try { try {
params = JSON.parse(req.body.params); params = JSON.parse(req.body.params);
} catch (e) { } catch (e) {
er = { var err = {
error: 'Error uploading file! Error :' + e.message error: 'Error uploading file! Error :' + e.message
}; };
return res.send(req.xhr ? er : JSON.stringify(er)); return res.send(req.xhr ? err : JSON.stringify(err));
} }
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) { if (validateUpload(req.files.userPhoto.type, allowedTypes)) {
er = { var filename = 'category-' + params.cid + path.extname(req.files.userPhoto.name);
error: 'Allowed image types are png, jpg and gif!' adminController.uploads.uploadImage(filename, req, res);
};
res.send(req.xhr ? er : JSON.stringify(er));
return;
} }
var filename = 'category-' + params.cid + path.extname(req.files.userPhoto.name);
adminController.uploads.uploadImage(filename, req, res);
}; };
adminController.uploads.uploadFavicon = function(req, res, next) { adminController.uploads.uploadFavicon = function(req, res, next) {
var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'], var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
er;
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
er = {error: 'You can only upload icon file type!'};
res.send(req.xhr ? er : JSON.stringify(er));
return;
}
file.saveFileToLocal('favicon.ico', req.files.userPhoto.path, function(err, image) { if (validateUpload(req.files.userPhoto.type, allowedTypes)) {
fs.unlink(req.files.userPhoto.path); file.saveFileToLocal('favicon.ico', req.files.userPhoto.path, function(err, image) {
fs.unlink(req.files.userPhoto.path);
if(err) { if(err) {
er = {error: err.message}; return res.send(req.xhr ? err : JSON.stringify(err));
return res.send(req.xhr ? er : JSON.stringify(er)); }
}
var rs = {path: image.url}; var rs = {path: image.url};
res.send(req.xhr ? rs : JSON.stringify(rs)); res.send(req.xhr ? rs : JSON.stringify(rs));
}); });
}
}; };
adminController.uploads.uploadLogo = function(req, res, next) { adminController.uploads.uploadLogo = function(req, res, next) {
var allowedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'], var allowedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'],
er; er;
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) { if (validateUpload(req.files.userPhoto.type, allowedTypes)) {
er = {error: 'Allowed image types are png, jpg and gif!'}; var filename = 'site-logo' + path.extname(req.files.userPhoto.name);
res.send(req.xhr ? er : JSON.stringify(er)); adminController.uploads.uploadImage(filename, req, res);
return;
} }
var filename = 'site-logo' + path.extname(req.files.userPhoto.name);
adminController.uploads.uploadImage(filename, req, res);
}; };

Loading…
Cancel
Save