|
|
|
@ -8,37 +8,8 @@ var fs = require('fs'),
|
|
|
|
|
|
|
|
|
|
var uploadsController = {};
|
|
|
|
|
|
|
|
|
|
function validateUpload(res, req, allowedTypes) {
|
|
|
|
|
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
|
|
|
|
var err = {
|
|
|
|
|
error: 'Invalid image type. Allowed types are: ' + allowedTypes.join(', ')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fs.unlink(req.files.userPhoto.path);
|
|
|
|
|
res.send(req.xhr ? err : JSON.stringify(err));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
uploadsController.uploadImage = function(filename, folder, req, res) {
|
|
|
|
|
function done(err, image) {
|
|
|
|
|
fs.unlink(req.files.userPhoto.path);
|
|
|
|
|
|
|
|
|
|
var response = err ? {error: err.message} : {path: image.url};
|
|
|
|
|
|
|
|
|
|
res.send(req.xhr ? response : JSON.stringify(response));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (plugins.hasListeners('filter:uploadImage')) {
|
|
|
|
|
plugins.fireHook('filter:uploadImage', {image: req.files.userPhoto, uid: req.user.uid}, done);
|
|
|
|
|
} else {
|
|
|
|
|
file.saveFileToLocal(filename, folder, req.files.userPhoto.path, done);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
uploadsController.uploadCategoryPicture = function(req, res, next) {
|
|
|
|
|
var uploadedFile = req.files.files[0];
|
|
|
|
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/svg+xml'],
|
|
|
|
|
params = null;
|
|
|
|
|
|
|
|
|
@ -48,22 +19,23 @@ uploadsController.uploadCategoryPicture = function(req, res, next) {
|
|
|
|
|
var err = {
|
|
|
|
|
error: 'Error uploading file! Error :' + e.message
|
|
|
|
|
};
|
|
|
|
|
fs.unlink(req.files.userPhoto.path);
|
|
|
|
|
fs.unlink(uploadedFile.path);
|
|
|
|
|
return res.send(req.xhr ? err : JSON.stringify(err));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, req, allowedTypes)) {
|
|
|
|
|
var filename = 'category-' + params.cid + path.extname(req.files.userPhoto.name);
|
|
|
|
|
uploadsController.uploadImage(filename, 'category', req, res);
|
|
|
|
|
if (validateUpload(req, res, uploadedFile, allowedTypes)) {
|
|
|
|
|
var filename = 'category-' + params.cid + path.extname(uploadedFile.name);
|
|
|
|
|
uploadImage(filename, 'category', uploadedFile, req, res);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
uploadsController.uploadFavicon = function(req, res, next) {
|
|
|
|
|
var uploadedFile = req.files.files[0];
|
|
|
|
|
var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, req, allowedTypes)) {
|
|
|
|
|
file.saveFileToLocal('favicon.ico', 'files', req.files.userPhoto.path, function(err, image) {
|
|
|
|
|
fs.unlink(req.files.userPhoto.path);
|
|
|
|
|
if (validateUpload(res, req, uploadedFile, allowedTypes)) {
|
|
|
|
|
file.saveFileToLocal('favicon.ico', 'files', uploadedFile.path, function(err, image) {
|
|
|
|
|
fs.unlink(uploadedFile.path);
|
|
|
|
|
|
|
|
|
|
var response = err ? {error: err.message} : {path: image.url};
|
|
|
|
|
|
|
|
|
@ -81,11 +53,41 @@ uploadsController.uploadGravatarDefault = function(req, res, next) {
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function upload(name, req, res, next) {
|
|
|
|
|
var uploadedFile = req.files.files[0];
|
|
|
|
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif'];
|
|
|
|
|
if (validateUpload(req, res, uploadedFile, allowedTypes)) {
|
|
|
|
|
var filename = name + path.extname(uploadedFile.name);
|
|
|
|
|
uploadImage(filename, 'files', uploadedFile, req, res);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validateUpload(req, res, uploadedFile, allowedTypes) {
|
|
|
|
|
if (allowedTypes.indexOf(uploadedFile.type) === -1) {
|
|
|
|
|
var err = {
|
|
|
|
|
error: 'Invalid image type. Allowed types are: ' + allowedTypes.join(', ')
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
fs.unlink(uploadedFile.path);
|
|
|
|
|
res.send(req.xhr ? err : JSON.stringify(err));
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (validateUpload(res, req, allowedTypes)) {
|
|
|
|
|
var filename = name + path.extname(req.files.userPhoto.name);
|
|
|
|
|
uploadsController.uploadImage(filename, 'files', req, res);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadImage(filename, folder, uploadedFile, req, res) {
|
|
|
|
|
function done(err, uploadedImage) {
|
|
|
|
|
fs.unlink(uploadedFile.path);
|
|
|
|
|
|
|
|
|
|
var response = err ? {error: err.message} : {path: uploadedImage.url};
|
|
|
|
|
|
|
|
|
|
res.send(req.xhr ? response : JSON.stringify(response));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (plugins.hasListeners('filter:uploadImage')) {
|
|
|
|
|
plugins.fireHook('filter:uploadImage', {image: uploadedFile, uid: req.user.uid}, done);
|
|
|
|
|
} else {
|
|
|
|
|
file.saveFileToLocal(filename, folder, uploadedFile.path, done);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|