changed userPhoto to files[]

v1.18.x
barisusakli 10 years ago
parent 23f450adbd
commit f74383bba4

@ -36,11 +36,7 @@ define('forum/account/profile', ['forum/account/header', 'forum/infinitescroll']
socket.removeListener('event:user_status_change', onUserStatusChange); socket.removeListener('event:user_status_change', onUserStatusChange);
socket.on('event:user_status_change', onUserStatusChange); socket.on('event:user_status_change', onUserStatusChange);
if (yourid !== theirid) { infinitescroll.init(loadMorePosts);
socket.emit('user.increaseViewCount', theirid);
}
infinitescroll.init(loadMoreTopics);
}; };
function processPage() { function processPage() {
@ -84,7 +80,7 @@ define('forum/account/profile', ['forum/account/header', 'forum/infinitescroll']
} }
function loadMoreTopics(direction) { function loadMorePosts(direction) {
if(direction < 0 || !$('.user-recent-posts').length) { if(direction < 0 || !$('.user-recent-posts').length) {
return; return;
} }

@ -20,7 +20,6 @@ define('uploader', ['csrf'], function(csrf) {
uploadForm[0].reset(); uploadForm[0].reset();
uploadForm.attr('action', route); uploadForm.attr('action', route);
uploadForm.find('#params').val(JSON.stringify(params)); uploadForm.find('#params').val(JSON.stringify(params));
// uploadForm.find('#csrfToken').val(csrf.get());
if (fileSize) { if (fileSize) {
uploadForm.find('#upload-file-size').html(fileSize); uploadForm.find('#upload-file-size').html(fileSize);

@ -144,6 +144,10 @@ accountsController.getAccount = function(req, res, next) {
return helpers.notFound(req, res); return helpers.notFound(req, res);
} }
if (callerUID !== parseInt(userData.uid, 10)) {
user.incrementUserFieldBy(userData.uid, 'profileviews', 1);
}
async.parallel({ async.parallel({
isFollowing: function(next) { isFollowing: function(next) {
user.isFollowing(callerUID, userData.theirid, next); user.isFollowing(callerUID, userData.theirid, next);
@ -386,25 +390,27 @@ accountsController.accountSettings = function(req, res, next) {
}; };
accountsController.uploadPicture = function (req, res, next) { accountsController.uploadPicture = function (req, res, next) {
var userPhoto = req.files.files[0];
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256; var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
if (req.files.userPhoto.size > uploadSize * 1024) {
fs.unlink(req.files.userPhoto.path); if (userPhoto.size > uploadSize * 1024) {
fs.unlink(userPhoto.path);
return res.json({ return res.json({
error: 'Images must be smaller than ' + uploadSize + ' kb!' error: 'Images must be smaller than ' + uploadSize + ' kb!'
}); });
} }
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(userPhoto.type) === -1) {
fs.unlink(req.files.userPhoto.path); fs.unlink(userPhoto.path);
return res.json({ return res.json({
error: 'Allowed image types are png, jpg and gif!' error: 'Allowed image types are png, jpg and gif!'
}); });
} }
var extension = path.extname(req.files.userPhoto.name); var extension = path.extname(userPhoto.name);
if (!extension) { if (!extension) {
fs.unlink(req.files.userPhoto.path); fs.unlink(userPhoto.path);
return res.json({ return res.json({
error: 'Error uploading file! Error : Invalid extension!' error: 'Error uploading file! Error : Invalid extension!'
}); });
@ -415,11 +421,11 @@ accountsController.uploadPicture = function (req, res, next) {
async.waterfall([ async.waterfall([
function(next) { function(next) {
image.resizeImage(req.files.userPhoto.path, extension, imageDimension, imageDimension, next); image.resizeImage(userPhoto.path, extension, imageDimension, imageDimension, next);
}, },
function(next) { function(next) {
if (parseInt(meta.config['profile:convertProfileImageToPNG'], 10) === 1) { if (parseInt(meta.config['profile:convertProfileImageToPNG'], 10) === 1) {
image.convertImageToPng(req.files.userPhoto.path, extension, next); image.convertImageToPng(userPhoto.path, extension, next);
} else { } else {
next(); next();
} }
@ -447,7 +453,7 @@ accountsController.uploadPicture = function (req, res, next) {
], function(err, result) { ], function(err, result) {
function done(err, image) { function done(err, image) {
fs.unlink(req.files.userPhoto.path); fs.unlink(userPhoto.path);
if(err) { if(err) {
return res.json({error: err.message}); return res.json({error: err.message});
} }
@ -460,12 +466,12 @@ accountsController.uploadPicture = function (req, res, next) {
} }
if (err) { if (err) {
fs.unlink(req.files.userPhoto.path); fs.unlink(userPhoto.path);
return res.json({error:err.message}); return res.json({error:err.message});
} }
if (plugins.hasListeners('filter:uploadImage')) { if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', {image: req.files.userPhoto, uid: updateUid}, done); return plugins.fireHook('filter:uploadImage', {image: userPhoto, uid: updateUid}, done);
} }
var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10) === 1; var convertToPNG = parseInt(meta.config['profile:convertProfileImageToPNG'], 10) === 1;
@ -473,7 +479,7 @@ accountsController.uploadPicture = function (req, res, next) {
user.getUserField(updateUid, 'uploadedpicture', function (err, oldpicture) { user.getUserField(updateUid, 'uploadedpicture', function (err, oldpicture) {
if (!oldpicture) { if (!oldpicture) {
file.saveFileToLocal(filename, 'profile', req.files.userPhoto.path, done); file.saveFileToLocal(filename, 'profile', userPhoto.path, done);
return; return;
} }
@ -484,7 +490,7 @@ accountsController.uploadPicture = function (req, res, next) {
winston.err(err); winston.err(err);
} }
file.saveFileToLocal(filename, 'profile', req.files.userPhoto.path, done); file.saveFileToLocal(filename, 'profile', userPhoto.path, done);
}); });
}); });
}); });

@ -8,37 +8,8 @@ var fs = require('fs'),
var uploadsController = {}; 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) { 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'], var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif', 'image/svg+xml'],
params = null; params = null;
@ -48,22 +19,23 @@ uploadsController.uploadCategoryPicture = function(req, res, next) {
var err = { var err = {
error: 'Error uploading file! Error :' + e.message 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)); return res.send(req.xhr ? err : JSON.stringify(err));
} }
if (validateUpload(res, req, allowedTypes)) { if (validateUpload(req, res, uploadedFile, allowedTypes)) {
var filename = 'category-' + params.cid + path.extname(req.files.userPhoto.name); var filename = 'category-' + params.cid + path.extname(uploadedFile.name);
uploadsController.uploadImage(filename, 'category', req, res); uploadImage(filename, 'category', uploadedFile, req, res);
} }
}; };
uploadsController.uploadFavicon = function(req, res, next) { uploadsController.uploadFavicon = function(req, res, next) {
var uploadedFile = req.files.files[0];
var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon']; var allowedTypes = ['image/x-icon', 'image/vnd.microsoft.icon'];
if (validateUpload(res, req, allowedTypes)) { if (validateUpload(res, req, uploadedFile, allowedTypes)) {
file.saveFileToLocal('favicon.ico', 'files', req.files.userPhoto.path, function(err, image) { file.saveFileToLocal('favicon.ico', 'files', uploadedFile.path, function(err, image) {
fs.unlink(req.files.userPhoto.path); fs.unlink(uploadedFile.path);
var response = err ? {error: err.message} : {path: image.url}; 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) { function upload(name, req, res, next) {
var uploadedFile = req.files.files[0];
var allowedTypes = ['image/png', 'image/jpeg', 'image/pjpeg', 'image/jpg', 'image/gif']; 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)) { return true;
var filename = name + path.extname(req.files.userPhoto.name); }
uploadsController.uploadImage(filename, 'files', req, res);
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);
} }
} }

@ -106,6 +106,13 @@ middleware.addSlug = function(req, res, next) {
next(); next();
}; };
middleware.validateFiles = function(req, res, next) {
if (!Array.isArray(req.files.files) || !req.files.files.length) {
return next(new Error(['[[error:invalid-files]]']));
}
next();
};
middleware.prepareAPI = function(req, res, next) { middleware.prepareAPI = function(req, res, next) {
res.locals.isAPI = true; res.locals.isAPI = true;
next(); next();

@ -10,7 +10,7 @@ function apiRoutes(app, middleware, controllers) {
var multipart = require('connect-multiparty'); var multipart = require('connect-multiparty');
var multipartMiddleware = multipart(); var multipartMiddleware = multipart();
var middlewares = [multipartMiddleware, middleware.applyCSRF, middleware.authenticate]; var middlewares = [multipartMiddleware, middleware.validateFiles, middleware.applyCSRF, middleware.authenticate];
app.post('/category/uploadpicture', middlewares, controllers.admin.uploads.uploadCategoryPicture); app.post('/category/uploadpicture', middlewares, controllers.admin.uploads.uploadCategoryPicture);
app.post('/uploadfavicon', middlewares, controllers.admin.uploads.uploadFavicon); app.post('/uploadfavicon', middlewares, controllers.admin.uploads.uploadFavicon);

@ -28,10 +28,10 @@ module.exports = function(app, middleware, controllers) {
var multipart = require('connect-multiparty'); var multipart = require('connect-multiparty');
var multipartMiddleware = multipart(); var multipartMiddleware = multipart();
var middlewares = [multipartMiddleware, middleware.validateFiles, middleware.applyCSRF];
router.post('/post/upload', multipartMiddleware, middleware.applyCSRF, uploadsController.uploadPost); router.post('/post/upload', middlewares, uploadsController.uploadPost);
router.post('/topic/thumb/upload', multipartMiddleware, middleware.applyCSRF, uploadsController.uploadThumb); router.post('/topic/thumb/upload', middlewares, uploadsController.uploadThumb);
router.post('/user/:userslug/uploadpicture', multipartMiddleware, middleware.applyCSRF, middleware.authenticate, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions, controllers.accounts.uploadPicture); router.post('/user/:userslug/uploadpicture', middlewares.concat([middleware.authenticate, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions]), controllers.accounts.uploadPicture);
}; };
function getModerators(req, res, next) { function getModerators(req, res, next) {

@ -13,7 +13,7 @@
<form id="uploadForm" action="" method="post" enctype="multipart/form-data"> <form id="uploadForm" action="" method="post" enctype="multipart/form-data">
<div class="form-group"> <div class="form-group">
<label for="userPhoto">Upload a picture</label> <label for="userPhoto">Upload a picture</label>
<input type="file" id="userPhotoInput" name="userPhoto"> <input type="file" id="userPhotoInput" name="files[]">
<p class="help-block"></p> <p class="help-block"></p>
</div> </div>
<input type="hidden" id="params" name="params"> <input type="hidden" id="params" name="params">

Loading…
Cancel
Save