|
|
|
@ -14,7 +14,7 @@ var plugins = require('../../plugins');
|
|
|
|
|
|
|
|
|
|
var allowedImageTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif', 'image/svg+xml'];
|
|
|
|
|
|
|
|
|
|
var uploadsController = {};
|
|
|
|
|
var uploadsController = module.exports;
|
|
|
|
|
|
|
|
|
|
uploadsController.uploadCategoryPicture = function (req, res, next) {
|
|
|
|
|
var uploadedFile = req.files.files[0];
|
|
|
|
@ -23,11 +23,7 @@ uploadsController.uploadCategoryPicture = function (req, res, next) {
|
|
|
|
|
try {
|
|
|
|
|
params = JSON.parse(req.body.params);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
fs.unlink(uploadedFile.path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
deleteTempFile(uploadedFile.path);
|
|
|
|
|
return next(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -43,11 +39,7 @@ uploadsController.uploadFavicon = function (req, res, next) {
|
|
|
|
|
|
|
|
|
|
if (validateUpload(req, res, next, uploadedFile, allowedTypes)) {
|
|
|
|
|
file.saveFileToLocal('favicon.ico', 'system', uploadedFile.path, function (err, image) {
|
|
|
|
|
fs.unlink(uploadedFile.path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
deleteTempFile(uploadedFile.path);
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
@ -80,11 +72,7 @@ uploadsController.uploadTouchIcon = function (req, res, next) {
|
|
|
|
|
}),
|
|
|
|
|
], next);
|
|
|
|
|
}, function (err) {
|
|
|
|
|
fs.unlink(uploadedFile.path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
deleteTempFile(uploadedFile.path);
|
|
|
|
|
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
@ -108,19 +96,20 @@ uploadsController.uploadSound = function (req, res, next) {
|
|
|
|
|
return next(Error('[[error:invalid-data]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
file.saveFileToLocal(uploadedFile.name, 'sounds', uploadedFile.path, function (err) {
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
file.saveFileToLocal(uploadedFile.name, 'sounds', uploadedFile.path, next);
|
|
|
|
|
},
|
|
|
|
|
function (uploadedSound, next) {
|
|
|
|
|
meta.sounds.build(next);
|
|
|
|
|
},
|
|
|
|
|
], function (err) {
|
|
|
|
|
deleteTempFile(uploadedFile.path);
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
meta.sounds.build(function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json([{}]);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
uploadsController.uploadDefaultAvatar = function (req, res, next) {
|
|
|
|
@ -142,12 +131,7 @@ function upload(name, req, res, next) {
|
|
|
|
|
|
|
|
|
|
function validateUpload(req, res, next, uploadedFile, allowedTypes) {
|
|
|
|
|
if (allowedTypes.indexOf(uploadedFile.type) === -1) {
|
|
|
|
|
fs.unlink(uploadedFile.path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
deleteTempFile(uploadedFile.path);
|
|
|
|
|
res.json({ error: '[[error:invalid-image-type, ' + allowedTypes.join(', ') + ']]' });
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
@ -156,24 +140,27 @@ function validateUpload(req, res, next, uploadedFile, allowedTypes) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function uploadImage(filename, folder, uploadedFile, req, res, next) {
|
|
|
|
|
function done(err, image) {
|
|
|
|
|
fs.unlink(uploadedFile.path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err);
|
|
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
if (plugins.hasListeners('filter:uploadImage')) {
|
|
|
|
|
plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.user.uid }, next);
|
|
|
|
|
} else {
|
|
|
|
|
file.saveFileToLocal(filename, folder, uploadedFile.path, next);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
], function (err, image) {
|
|
|
|
|
deleteTempFile(uploadedFile.path);
|
|
|
|
|
if (err) {
|
|
|
|
|
return next(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
res.json([{ name: uploadedFile.name, url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url }]);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (plugins.hasListeners('filter:uploadImage')) {
|
|
|
|
|
plugins.fireHook('filter:uploadImage', { image: uploadedFile, uid: req.user.uid }, done);
|
|
|
|
|
} else {
|
|
|
|
|
file.saveFileToLocal(filename, folder, uploadedFile.path, done);
|
|
|
|
|
function deleteTempFile(path) {
|
|
|
|
|
fs.unlink(path, function (err) {
|
|
|
|
|
if (err) {
|
|
|
|
|
winston.error(err);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = uploadsController;
|
|
|
|
|