allow regular uploading (modal) of cover photo

This commit is contained in:
psychobunny
2015-10-28 15:22:54 -04:00
parent 69e43cd35a
commit 61b1f5fe32
4 changed files with 42 additions and 4 deletions

View File

@@ -2,8 +2,9 @@
/* globals define, app*/
define('coverPhoto', [
'uploader',
'vendor/jquery/draggable-background/backgroundDraggable'
], function() {
], function(uploader) {
var coverPhoto = {
coverEl: null,
@@ -17,6 +18,12 @@ define('coverPhoto', [
coverPhoto.saveFn = saveFn;
coverEl.find('.change').on('click', function() {
uploader.open(RELATIVE_PATH + '/api/groups/uploadpicture', { groupName: 'administrators' }, 0, function(imageUrlOnServer) {
console.log(imageUrlOnServer);
coverPhoto.coverEl.css('background-image', 'url(' + imageUrlOnServer + '?' + new Date().getTime() + ')');
});
return;
coverEl.toggleClass('active', 1);
coverEl.backgroundDraggable({
axis: 'y',

View File

@@ -132,4 +132,19 @@ groupsController.members = function(req, res, next) {
});
};
groupsController.uploadCover = function(req, res, next) {
var params = JSON.parse(req.body.params);
groups.updateCover({
file: req.files.files[0].path,
groupName: params.groupName
}, function(err, image) {
if (err) {
return next(err);
}
res.json([{url: image.url.startsWith('http') ? image.url : nconf.get('relative_path') + image.url}]);
})
};
module.exports = groupsController;

View File

@@ -123,6 +123,10 @@ module.exports = function(Groups) {
async.series([
function(next) {
if (data.file) {
return next();
}
// Calculate md5sum of image
// This is required because user data can be private
md5sum = crypto.createHash('md5');
@@ -131,6 +135,10 @@ module.exports = function(Groups) {
next();
},
function(next) {
if (data.file) {
return next();
}
// Save image
tempPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), md5sum);
var buffer = new Buffer(data.imageData.slice(data.imageData.indexOf('base64') + 7), 'base64');
@@ -142,7 +150,7 @@ module.exports = function(Groups) {
function(next) {
uploadsController.uploadGroupCover({
name: 'groupCover',
path: tempPath
path: data.file ? data.file : tempPath
}, function(err, uploadData) {
if (err) {
return next(err);
@@ -156,14 +164,20 @@ module.exports = function(Groups) {
Groups.setGroupField(data.groupName, 'cover:url', url, next);
},
function(next) {
fs.unlink(tempPath, next); // Delete temporary file
fs.unlink(data.file ? data.file : tempPath, next); // Delete temporary file
}
], function(err) {
if (err) {
return callback(err);
}
Groups.updateCoverPosition(data.groupName, data.position, callback);
if (data.position) {
Groups.updateCoverPosition(data.groupName, data.position, function(err) {
callback(err, {url: url});
});
} else {
callback(err, {url: url});
}
});
};

View File

@@ -28,5 +28,7 @@ module.exports = function(app, middleware, controllers) {
router.post('/post/upload', middlewares, uploadsController.uploadPost);
router.post('/topic/thumb/upload', middlewares, uploadsController.uploadThumb);
router.post('/user/:userslug/uploadpicture', middlewares.concat([middleware.authenticate, middleware.checkGlobalPrivacySettings, middleware.checkAccountPermissions]), controllers.accounts.edit.uploadPicture);
router.post('/groups/uploadpicture', middlewares.concat([middleware.authenticate]), controllers.groups.uploadCover);
};