Skip to content

Node.js based forum software built for the modern web

closes #3991

barisusakli1070fff0e3b7
diff --git a/src/groups.js b/src/groups.js
index 2da15aefa5..30fefa466f 100644
--- a/src/groups.js
+++ b/src/groups.js
@@ -18,6 +18,7 @@ var utils = require('../public/src/utils');
require('./groups/membership')(Groups);
require('./groups/ownership')(Groups);
require('./groups/search')(Groups);
+ require('./groups/cover')(Groups);
var ephemeralGroups = ['guests'],
@@ -412,6 +413,7 @@ var utils = require('../public/src/utils');
group.disableJoinRequests = parseInt(group.disableJoinRequests) === 1;
group['cover:url'] = group['cover:url'] || require('./coverPhoto').getDefaultGroupCover(group.name);
+ group['cover:thumb:url'] = group['cover:thumb:url'] || group['cover:url'];
group['cover:position'] = group['cover:position'] || '50% 50%';
}
});
diff --git a/src/groups/cover.js b/src/groups/cover.js
new file mode 100644
index 0000000000..f26350e09d
--- /dev/null
+++ b/src/groups/cover.js
@@ -0,0 +1,119 @@
+'use strict';
+
+var async = require('async');
+var nconf = require('nconf');
+var path = require('path');
+var fs = require('fs');
+var crypto = require('crypto');
+var Jimp = require('jimp');
+
+
+var db = require('../database');
+var uploadsController = require('../controllers/uploads');
+
+module.exports = function(Groups) {
+
+ Groups.updateCoverPosition = function(groupName, position, callback) {
+ if (!groupName) {
+ return callback(new Error('[[error:invalid-data]]'));
+ }
+ Groups.setGroupField(groupName, 'cover:position', position, callback);
+ };
+
+ Groups.updateCover = function(uid, data, callback) {
+
+ // Position only? That's fine
+ if (!data.imageData && data.position) {
+ return Groups.updateCoverPosition(data.groupName, data.position, callback);
+ }
+
+ var tempPath = data.file ? data.file : '';
+ var url;
+
+ async.waterfall([
+ function (next) {
+ if (tempPath) {
+ return next(null, tempPath);
+ }
+ writeImageDataToFile(data.imageData, next);
+ },
+ function (_tempPath, next) {
+ tempPath = _tempPath;
+ uploadsController.uploadGroupCover(uid, {
+ name: 'groupCover',
+ path: tempPath
+ }, next);
+ },
+ function (uploadData, next) {
+ url = uploadData.url;
+ Groups.setGroupField(data.groupName, 'cover:url', url, next);
+ },
+ function (next) {
+ resizeCover(tempPath, next);
+ },
+ function (next) {
+ uploadsController.uploadGroupCover(uid, {
+ name: 'groupCoverThumb',
+ path: tempPath
+ }, next);
+ },
+ function (uploadData, next) {
+ Groups.setGroupField(data.groupName, 'cover:thumb:url', uploadData.url, next);
+ },
+ function (next){
+ fs.unlink(tempPath, next); // Delete temporary file
+ }
+ ], function (err) {
+ if (err) {
+ return callback(err);
+ }
+
+ if (data.position) {
+ Groups.updateCoverPosition(data.groupName, data.position, function(err) {
+ callback(err, {url: url});
+ });
+ } else {
+ callback(err, {url: url});
+ }
+ });
+ };
+
+ function resizeCover(path, callback) {
+ async.waterfall([
+ function (next) {
+ new Jimp(path, next);
+ },
+ function (image, next) {
+ image.resize(358, Jimp.AUTO, next);
+ },
+ function (image, next) {
+ image.write(path, next);
+ }
+ ], function (err) {
+ callback(err);
+ });
+ }
+
+ function writeImageDataToFile(imageData, callback) {
+ // Calculate md5sum of image
+ // This is required because user data can be private
+ var md5sum = crypto.createHash('md5');
+ md5sum.update(imageData);
+ md5sum = md5sum.digest('hex');
+
+ // Save image
+ var tempPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), md5sum);
+ var buffer = new Buffer(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
+
+ fs.writeFile(tempPath, buffer, {
+ encoding: 'base64'
+ }, function(err) {
+ callback(err, tempPath);
+ });
+ }
+
+ Groups.removeCover = function(data, callback) {
+ db.deleteObjectField('group:' + data.groupName, 'cover:url', callback);
+ };
+
+};
\ No newline at end of file
diff --git a/src/groups/update.js b/src/groups/update.js
index 724f9f2109..f48ff96d21 100644
--- a/src/groups/update.js
+++ b/src/groups/update.js
@@ -1,17 +1,12 @@
'use strict';
-var async = require('async'),
- winston = require('winston'),
- crypto = require('crypto'),
- path = require('path'),
- nconf = require('nconf'),
- fs = require('fs'),
+var async = require('async');
+var winston = require('winston');
- plugins = require('../plugins'),
- utils = require('../../public/src/utils'),
- db = require('../database'),
+var plugins = require('../plugins');
+var utils = require('../../public/src/utils');
+var db = require('../database');
- uploadsController = require('../controllers/uploads');
module.exports = function(Groups) {
@@ -114,82 +109,6 @@ module.exports = function(Groups) {
});
}
- Groups.updateCoverPosition = function(groupName, position, callback) {
- Groups.setGroupField(groupName, 'cover:position', position, callback);
- };
-
- Groups.updateCover = function(uid, data, callback) {
- var tempPath, md5sum, url;
-
- // Position only? That's fine
- if (!data.imageData && data.position) {
- return Groups.updateCoverPosition(data.groupName, data.position, callback);
- }
-
- 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');
- md5sum.update(data.imageData);
- md5sum = md5sum.digest('hex');
- 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');
-
- fs.writeFile(tempPath, buffer, {
- encoding: 'base64'
- }, next);
- },
- function(next) {
- uploadsController.uploadGroupCover(uid, {
- name: 'groupCover',
- path: data.file ? data.file : tempPath
- }, function(err, uploadData) {
- if (err) {
- return next(err);
- }
-
- url = uploadData.url;
- next();
- });
- },
- function(next) {
- Groups.setGroupField(data.groupName, 'cover:url', url, next);
- },
- function(next) {
- fs.unlink(data.file ? data.file : tempPath, next); // Delete temporary file
- }
- ], function(err) {
- if (err) {
- return callback(err);
- }
-
- if (data.position) {
- Groups.updateCoverPosition(data.groupName, data.position, function(err) {
- callback(err, {url: url});
- });
- } else {
- callback(err, {url: url});
- }
- });
- };
-
- Groups.removeCover = function(data, callback) {
- db.deleteObjectField('group:' + data.groupName, 'cover:url', callback);
- };
-
function updatePrivacy(groupName, newValue, callback) {
if (!newValue) {
return callback();