added hooks for image uploads into user and admin routes

v1.18.x
Baris Soner Usakli 11 years ago
parent e163282995
commit 26849ba2b7

@ -115,7 +115,7 @@ define(['taskbar'], function(taskbar) {
var postContainer = $(composerTemplate[0]); var postContainer = $(composerTemplate[0]);
if(config.allowFileUploads || config.hasPostUploadPlugin) { if(config.allowFileUploads || config.hasImageUploadPlugin) {
initializeFileReader(post_uuid); initializeFileReader(post_uuid);
} }
@ -369,7 +369,7 @@ define(['taskbar'], function(taskbar) {
postContainer.css('height', $(window).height() - $('#header-menu').height()); postContainer.css('height', $(window).height() - $('#header-menu').height());
} }
if(config.hasPostUploadPlugin) { if(config.hasImageUploadPlugin) {
if(env === 'md' || env === 'lg') { if(env === 'md' || env === 'lg') {
postContainer.find('.upload-instructions').removeClass('hide'); postContainer.find('.upload-instructions').removeClass('hide');
} }

@ -396,8 +396,8 @@ var db = require('./database'),
Posts.uploadPostImage = function(image, callback) { Posts.uploadPostImage = function(image, callback) {
if(plugins.hasListeners('filter:post.uploadImage')) { if(plugins.hasListeners('filter:uploadImage')) {
plugins.fireHook('filter:post.uploadImage', image, callback); plugins.fireHook('filter:uploadImage', image, callback);
} else { } else {
if (meta.config.allowFileUploads) { if (meta.config.allowFileUploads) {
@ -410,8 +410,8 @@ var db = require('./database'),
Posts.uploadPostFile = function(file, callback) { Posts.uploadPostFile = function(file, callback) {
if(plugins.hasListeners('filter:post.uploadFile')) { if(plugins.hasListeners('filter:uploadFile')) {
plugins.fireHook('filter:post.uploadFile', file, callback); plugins.fireHook('filter:uploadFile', file, callback);
} else { } else {
if(!meta.config.allowFileUploads) { if(!meta.config.allowFileUploads) {

@ -11,6 +11,7 @@ var nconf = require('nconf'),
categories = require('./../categories'), categories = require('./../categories'),
meta = require('../meta'), meta = require('../meta'),
plugins = require('../plugins'), plugins = require('../plugins'),
image = require('./../image'),
Languages = require('../languages'), Languages = require('../languages'),
events = require('./../events'), events = require('./../events'),
utils = require('./../../public/src/utils'), utils = require('./../../public/src/utils'),
@ -139,7 +140,17 @@ var nconf = require('nconf'),
return; return;
} }
uploadImage('favicon.ico', req, res); saveFileToLocal('favicon.ico', req, res, function(err, image) {
if(err) {
return res.send({
error: err.message
});
}
res.json({
path: image.url
});
});
}); });
app.post('/uploadlogo', function(req, res) { app.post('/uploadlogo', function(req, res) {
@ -156,7 +167,7 @@ var nconf = require('nconf'),
return; return;
} }
var filename = 'site-logo' + path.extname(req.files.userPhoto.name); var filename = 'site-logo' + path.extname(req.files.userPhoto.name);
uploadImage(filename, req, res); uploadImage(filename, req, res);
}); });
@ -171,6 +182,32 @@ var nconf = require('nconf'),
}); });
function uploadImage(filename, req, res) { function uploadImage(filename, req, res) {
function done(err, image) {
if(err) {
return res.send({
error: err.message
});
}
res.json({
path: image.url
});
}
if(plugins.hasListeners('filter:uploadImage')) {
image.convertImageToBase64(req.files.userPhoto.path, function(err, image64) {
if(err) {
return done(err);
}
plugins.fireHook('filter:uploadImage', {data:image64, name:filename}, done);
});
} else {
saveFileToLocal(filename, req, res, done);
}
}
function saveFileToLocal(filename, req, res, callback) {
var tempPath = req.files.userPhoto.path; var tempPath = req.files.userPhoto.path;
var extension = path.extname(req.files.userPhoto.name); var extension = path.extname(req.files.userPhoto.name);
@ -191,9 +228,7 @@ var nconf = require('nconf'),
is.on('end', function () { is.on('end', function () {
fs.unlinkSync(tempPath); fs.unlinkSync(tempPath);
res.json({ callback(null, {url: nconf.get('upload_url') + filename});
path: nconf.get('upload_url') + filename
});
}); });
os.on('error', function (err) { os.on('error', function (err) {
@ -204,7 +239,6 @@ var nconf = require('nconf'),
is.pipe(os); is.pipe(os);
} }
var custom_routes = { var custom_routes = {
'routes': [], 'routes': [],
'api': [] 'api': []

@ -37,7 +37,7 @@ var path = require('path'),
config.minimumTitleLength = meta.config.minimumTitleLength; config.minimumTitleLength = meta.config.minimumTitleLength;
config.maximumTitleLength = meta.config.maximumTitleLength; config.maximumTitleLength = meta.config.maximumTitleLength;
config.minimumPostLength = meta.config.minimumPostLength; config.minimumPostLength = meta.config.minimumPostLength;
config.hasPostUploadPlugin = Plugins.hasListeners('filter:post.uploadImage'); config.hasImageUploadPlugin = Plugins.hasListeners('filter:uploadImage');
config.minimumUsernameLength = meta.config.minimumUsernameLength; config.minimumUsernameLength = meta.config.minimumUsernameLength;
config.maximumUsernameLength = meta.config.maximumUsernameLength; config.maximumUsernameLength = meta.config.maximumUsernameLength;
config.minimumPasswordLength = meta.config.minimumPasswordLength; config.minimumPasswordLength = meta.config.minimumPasswordLength;

@ -3,6 +3,7 @@ var fs = require('fs'),
winston = require('winston'), winston = require('winston'),
nconf = require('nconf'), nconf = require('nconf'),
async= require('async'), async= require('async'),
imagemagick = require('node-imagemagick'),
user = require('./../user'), user = require('./../user'),
posts = require('./../posts'), posts = require('./../posts'),
@ -10,6 +11,8 @@ var fs = require('fs'),
utils = require('./../../public/src/utils'), utils = require('./../../public/src/utils'),
templates = require('./../../public/src/templates'), templates = require('./../../public/src/templates'),
meta = require('./../meta'), meta = require('./../meta'),
plugins = require('./../plugins'),
image = require('./../image'),
db = require('./../database'); db = require('./../database');
(function (User) { (function (User) {
@ -114,118 +117,101 @@ var fs = require('fs'),
}); });
app.post('/uploadpicture', function (req, res) { app.post('/uploadpicture', function (req, res) {
if (!req.user) if (!req.user) {
return res.redirect('/403'); return res.json(403, {
error: 'Not allowed!'
});
}
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256; var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
if (req.files.userPhoto.size > uploadSize * 1024) { if (req.files.userPhoto.size > uploadSize * 1024) {
res.send({ return res.send({
error: 'Images must be smaller than ' + uploadSize + ' kb!' error: 'Images must be smaller than ' + uploadSize + ' kb!'
}); });
return;
} }
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif']; var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) { if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
res.send({ return res.send({
error: 'Allowed image types are png, jpg and gif!' error: 'Allowed image types are png, jpg and gif!'
}); });
return;
} }
user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) { var extension = path.extname(req.files.userPhoto.name);
if (!oldpicture) { if (!extension) {
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); return res.send({
return; error: 'Error uploading file! Error : Invalid extension!'
} });
}
var absolutePath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), path.basename(oldpicture)); var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10);
var filename = req.user.uid + '-profileimg' + (convertToPNG ? '.png' : extension);
fs.unlink(absolutePath, function (err) { async.waterfall([
if (err) { function(next) {
winston.err(err); image.resizeImage(req.files.userPhoto.path, extension, 128, 128, next);
},
function(next) {
image.convertImageToPng(req.files.userPhoto.path, extension, next);
}
], function(err, result) {
function done(err, image) {
if(err) {
return res.send({error: err.message});
} }
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res); user.setUserField(req.user.uid, 'uploadedpicture', image.url);
}); user.setUserField(req.user.uid, 'picture', image.url);
res.json({
path: image.url
});
}
if(err) {
return res.send({error:err.message});
}
if(plugins.hasListeners('filter:uploadImage')) {
image.convertImageToBase64(req.files.userPhoto.path, function(err, image64) {
plugins.fireHook('filter:uploadImage', {data:image64, name:filename}, done);
});
} else {
user.getUserField(req.user.uid, 'uploadedpicture', function (err, oldpicture) {
if (!oldpicture) {
saveFileToLocal(filename, req.files.userPhoto.path, done);
return;
}
var absolutePath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), path.basename(oldpicture));
fs.unlink(absolutePath, function (err) {
if (err) {
winston.err(err);
}
saveFileToLocal(filename, req.files.userPhoto.path, done);
});
});
}
}); });
}); });
}); });
function uploadUserPicture(uid, extension, tempPath, res) {
if (!extension) {
res.send({
error: 'Error uploading file! Error : Invalid extension!'
});
return;
}
var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10); function saveFileToLocal(filename, tempPath, callback) {
var filename = uid + '-profileimg' + (convertToPNG ? '.png' : extension);
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename); var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
winston.info('Attempting upload to: ' + uploadPath); winston.info('Saving file '+ filename +' to : ' + uploadPath);
var is = fs.createReadStream(tempPath); var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath); var os = fs.createWriteStream(uploadPath);
var im = require('node-imagemagick');
is.on('end', function () { is.on('end', function () {
fs.unlinkSync(tempPath); fs.unlinkSync(tempPath);
function done(err) { callback(null, {url: nconf.get('upload_url') + filename});
if (err) {
winston.err(err);
res.send({
error: 'Invalid image file!'
});
return;
}
var imageUrl = nconf.get('upload_url') + filename;
user.setUserField(uid, 'uploadedpicture', imageUrl);
user.setUserField(uid, 'picture', imageUrl);
if (convertToPNG && extension !== '.png') {
im.convert([uploadPath, 'png:-'], function(err, stdout) {
if (err) {
winston.err(err);
res.send({
error: 'Unable to convert image to PNG.'
});
return;
}
fs.writeFileSync(uploadPath, stdout, 'binary');
res.json({
path: imageUrl
});
});
} else {
res.json({
path: imageUrl
});
}
}
if(extension === '.gif') {
im.convert([uploadPath, '-coalesce', '-repage', '0x0', '-crop', '128x128+0+0', '+repage', 'uploadPath'], function(err, stdout) {
done(err);
});
} else {
im.crop({
srcPath: uploadPath,
dstPath: uploadPath,
width: 128,
height: 128
}, function (err, stdout, stderr) {
done(err);
});
}
}); });
os.on('error', function (err) { os.on('error', function (err) {

Loading…
Cancel
Save