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

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

@ -11,6 +11,7 @@ var nconf = require('nconf'),
categories = require('./../categories'),
meta = require('../meta'),
plugins = require('../plugins'),
image = require('./../image'),
Languages = require('../languages'),
events = require('./../events'),
utils = require('./../../public/src/utils'),
@ -139,7 +140,17 @@ var nconf = require('nconf'),
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) {
@ -171,6 +182,32 @@ var nconf = require('nconf'),
});
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 extension = path.extname(req.files.userPhoto.name);
@ -191,9 +228,7 @@ var nconf = require('nconf'),
is.on('end', function () {
fs.unlinkSync(tempPath);
res.json({
path: nconf.get('upload_url') + filename
});
callback(null, {url: nconf.get('upload_url') + filename});
});
os.on('error', function (err) {
@ -204,7 +239,6 @@ var nconf = require('nconf'),
is.pipe(os);
}
var custom_routes = {
'routes': [],
'api': []

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

@ -3,6 +3,7 @@ var fs = require('fs'),
winston = require('winston'),
nconf = require('nconf'),
async= require('async'),
imagemagick = require('node-imagemagick'),
user = require('./../user'),
posts = require('./../posts'),
@ -10,6 +11,8 @@ var fs = require('fs'),
utils = require('./../../public/src/utils'),
templates = require('./../../public/src/templates'),
meta = require('./../meta'),
plugins = require('./../plugins'),
image = require('./../image'),
db = require('./../database');
(function (User) {
@ -114,30 +117,69 @@ var fs = require('fs'),
});
app.post('/uploadpicture', function (req, res) {
if (!req.user)
return res.redirect('/403');
if (!req.user) {
return res.json(403, {
error: 'Not allowed!'
});
}
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
if (req.files.userPhoto.size > uploadSize * 1024) {
res.send({
return res.send({
error: 'Images must be smaller than ' + uploadSize + ' kb!'
});
return;
}
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
res.send({
return res.send({
error: 'Allowed image types are png, jpg and gif!'
});
return;
}
var extension = path.extname(req.files.userPhoto.name);
if (!extension) {
return res.send({
error: 'Error uploading file! Error : Invalid extension!'
});
}
var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10);
var filename = req.user.uid + '-profileimg' + (convertToPNG ? '.png' : extension);
async.waterfall([
function(next) {
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});
}
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) {
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
saveFileToLocal(filename, req.files.userPhoto.path, done);
return;
}
@ -148,84 +190,28 @@ var fs = require('fs'),
winston.err(err);
}
uploadUserPicture(req.user.uid, path.extname(req.files.userPhoto.name), req.files.userPhoto.path, res);
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);
var filename = uid + '-profileimg' + (convertToPNG ? '.png' : extension);
function saveFileToLocal(filename, tempPath, callback) {
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 os = fs.createWriteStream(uploadPath);
var im = require('node-imagemagick');
is.on('end', function () {
fs.unlinkSync(tempPath);
function done(err) {
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);
});
}
callback(null, {url: nconf.get('upload_url') + filename});
});
os.on('error', function (err) {

Loading…
Cancel
Save