You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
3.2 KiB
JavaScript

9 years ago
'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');
9 years ago
var mime = require('mime');
var winston = require('winston');
9 years ago
var db = require('../database');
9 years ago
var file = require('../file');
9 years ago
var uploadsController = require('../controllers/uploads');
module.exports = function (Groups) {
9 years ago
Groups.updateCoverPosition = function (groupName, position, callback) {
9 years ago
if (!groupName) {
return callback(new Error('[[error:invalid-data]]'));
}
Groups.setGroupField(groupName, 'cover:position', position, callback);
};
Groups.updateCover = function (uid, data, callback) {
9 years ago
// 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;
9 years ago
var type = data.file ? mime.lookup(data.file) : 'image/png';
9 years ago
async.waterfall([
function (next) {
if (tempPath) {
return next(null, tempPath);
}
writeImageDataToFile(data.imageData, next);
},
function (_tempPath, next) {
9 years ago
tempPath = _tempPath;
9 years ago
uploadsController.uploadGroupCover(uid, {
name: 'groupCover',
9 years ago
path: tempPath,
type: type
9 years ago
}, 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',
9 years ago
path: tempPath,
type: type
9 years ago
}, 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 fs.unlink(tempPath, function (unlinkErr) {
if (unlinkErr) {
winston.error(unlinkErr);
}
9 years ago
callback(err); // send back original error
});
9 years ago
}
if (data.position) {
Groups.updateCoverPosition(data.groupName, data.position, function (err) {
9 years ago
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
9 years ago
var tempPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), md5sum) + '.png';
9 years ago
var buffer = new Buffer(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
fs.writeFile(tempPath, buffer, {
encoding: 'base64'
}, function (err) {
9 years ago
callback(err, tempPath);
});
}
Groups.removeCover = function (data, callback) {
9 years ago
db.deleteObjectFields('group:' + data.groupName, ['cover:url', 'cover:thumb:url'], callback);
9 years ago
};
};