mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-10-26 08:36:12 +01:00
@@ -40,6 +40,7 @@ define('forum/groups/details', [
|
||||
paramName: 'groupName',
|
||||
paramValue: groupName,
|
||||
}, function (imageUrlOnServer) {
|
||||
imageUrlOnServer = (!imageUrlOnServer.startsWith('http') ? config.relative_path : '') + imageUrlOnServer + '?' + Date.now();
|
||||
components.get('groups/cover').css('background-image', 'url(' + imageUrlOnServer + ')');
|
||||
});
|
||||
},
|
||||
|
||||
@@ -168,31 +168,6 @@ uploadsController.uploadThumb = function (req, res, next) {
|
||||
}, next);
|
||||
};
|
||||
|
||||
uploadsController.uploadGroupCover = function (uid, uploadedFile, callback) {
|
||||
if (plugins.hasListeners('filter:uploadImage')) {
|
||||
return plugins.fireHook('filter:uploadImage', {
|
||||
image: uploadedFile,
|
||||
uid: uid,
|
||||
}, callback);
|
||||
}
|
||||
|
||||
if (plugins.hasListeners('filter:uploadFile')) {
|
||||
return plugins.fireHook('filter:uploadFile', {
|
||||
file: uploadedFile,
|
||||
uid: uid,
|
||||
}, callback);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
file.isFileTypeAllowed(uploadedFile.path, next);
|
||||
},
|
||||
function (next) {
|
||||
saveFileToLocal(uid, uploadedFile, next);
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
uploadsController.uploadFile = function (uid, uploadedFile, callback) {
|
||||
if (plugins.hasListeners('filter:uploadFile')) {
|
||||
return plugins.fireHook('filter:uploadFile', {
|
||||
|
||||
@@ -23,8 +23,10 @@ function getCover(type, id) {
|
||||
} else {
|
||||
id %= covers.length;
|
||||
}
|
||||
|
||||
return covers[id];
|
||||
if (!covers[id].startsWith('http')) {
|
||||
covers = nconf.get('relative_path') + covers[id];
|
||||
}
|
||||
return covers;
|
||||
}
|
||||
|
||||
return nconf.get('relative_path') + '/assets/images/cover-default.png';
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
|
||||
var async = require('async');
|
||||
var path = require('path');
|
||||
var mime = require('mime');
|
||||
|
||||
var db = require('../database');
|
||||
var image = require('../image');
|
||||
var file = require('../file');
|
||||
var uploadsController = require('../controllers/uploads');
|
||||
|
||||
module.exports = function (Groups) {
|
||||
Groups.updateCoverPosition = function (groupName, position, callback) {
|
||||
@@ -25,7 +23,7 @@ module.exports = function (Groups) {
|
||||
|
||||
var tempPath = data.file ? data.file : '';
|
||||
var url;
|
||||
var type = data.file ? mime.getType(data.file) : 'image/png';
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
if (tempPath) {
|
||||
@@ -36,10 +34,10 @@ module.exports = function (Groups) {
|
||||
function (_tempPath, next) {
|
||||
tempPath = _tempPath;
|
||||
|
||||
uploadsController.uploadGroupCover(uid, {
|
||||
name: 'groupCover' + path.extname(tempPath),
|
||||
const filename = 'groupCover-' + data.groupName + path.extname(tempPath);
|
||||
image.uploadImage(filename, 'files', {
|
||||
path: tempPath,
|
||||
type: type,
|
||||
uid: uid,
|
||||
}, next);
|
||||
},
|
||||
function (uploadData, next) {
|
||||
@@ -53,10 +51,9 @@ module.exports = function (Groups) {
|
||||
}, next);
|
||||
},
|
||||
function (next) {
|
||||
uploadsController.uploadGroupCover(uid, {
|
||||
name: 'groupCoverThumb' + path.extname(tempPath),
|
||||
image.uploadImage('groupCoverThumb-' + data.groupName + path.extname(tempPath), 'files', {
|
||||
path: tempPath,
|
||||
type: type,
|
||||
uid: uid,
|
||||
}, next);
|
||||
},
|
||||
function (uploadData, next) {
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
var async = require('async');
|
||||
var validator = require('validator');
|
||||
var nconf = require('nconf');
|
||||
|
||||
var db = require('../database');
|
||||
var plugins = require('../plugins');
|
||||
@@ -91,8 +92,20 @@ function modifyGroup(group, fields) {
|
||||
group.createtimeISO = utils.toISOString(group.createtime);
|
||||
group.private = ([null, undefined].includes(group.private)) ? 1 : group.private;
|
||||
|
||||
group['cover:url'] = group['cover:url'] || require('../coverPhoto').getDefaultGroupCover(group.name);
|
||||
group['cover:thumb:url'] = group['cover:thumb:url'] || group['cover:url'];
|
||||
|
||||
if (group['cover:url']) {
|
||||
group['cover:url'] = group['cover:url'].startsWith('http') ? group['cover:url'] : (nconf.get('relative_path') + group['cover:url']);
|
||||
} else {
|
||||
group['cover:url'] = require('../coverPhoto').getDefaultGroupCover(group.name);
|
||||
}
|
||||
|
||||
if (group['cover:thumb:url']) {
|
||||
group['cover:thumb:url'] = group['cover:thumb:url'].startsWith('http') ? group['cover:thumb:url'] : (nconf.get('relative_path') + group['cover:thumb:url']);
|
||||
} else {
|
||||
group['cover:thumb:url'] = require('../coverPhoto').getDefaultGroupCover(group.name);
|
||||
}
|
||||
|
||||
group['cover:position'] = validator.escape(String(group['cover:position'] || '50% 50%'));
|
||||
}
|
||||
}
|
||||
|
||||
25
src/image.js
25
src/image.js
@@ -139,3 +139,28 @@ image.writeImageDataToTempFile = function (imageData, callback) {
|
||||
image.sizeFromBase64 = function (imageData) {
|
||||
return Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64').length;
|
||||
};
|
||||
|
||||
image.uploadImage = function (filename, folder, image, callback) {
|
||||
if (plugins.hasListeners('filter:uploadImage')) {
|
||||
return plugins.fireHook('filter:uploadImage', {
|
||||
image: image,
|
||||
uid: image.uid,
|
||||
}, callback);
|
||||
}
|
||||
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
file.isFileTypeAllowed(image.path, next);
|
||||
},
|
||||
function (next) {
|
||||
file.saveFileToLocal(filename, folder, image.path, next);
|
||||
},
|
||||
function (upload, next) {
|
||||
next(null, {
|
||||
url: upload.url,
|
||||
path: upload.path,
|
||||
name: image.name,
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
};
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
var async = require('async');
|
||||
var winston = require('winston');
|
||||
|
||||
var plugins = require('../plugins');
|
||||
var file = require('../file');
|
||||
var image = require('../image');
|
||||
var meta = require('../meta');
|
||||
@@ -58,7 +57,7 @@ module.exports = function (User) {
|
||||
|
||||
var extension = file.typeToExtension(type);
|
||||
var filename = generateProfileImageFilename(data.uid, 'profilecover', extension);
|
||||
uploadProfileOrCover(filename, picture, next);
|
||||
image.uploadImage(filename, 'profile', picture, next);
|
||||
},
|
||||
function (uploadData, next) {
|
||||
url = uploadData.url;
|
||||
@@ -130,7 +129,7 @@ module.exports = function (User) {
|
||||
},
|
||||
function (next) {
|
||||
var filename = generateProfileImageFilename(data.uid, 'profileavatar', extension);
|
||||
uploadProfileOrCover(filename, picture, next);
|
||||
image.uploadImage(filename, 'profile', picture, next);
|
||||
},
|
||||
function (_uploadedImage, next) {
|
||||
uploadedImage = _uploadedImage;
|
||||
@@ -162,41 +161,12 @@ module.exports = function (User) {
|
||||
], callback);
|
||||
}
|
||||
|
||||
function uploadProfileOrCover(filename, image, callback) {
|
||||
if (plugins.hasListeners('filter:uploadImage')) {
|
||||
return plugins.fireHook('filter:uploadImage', {
|
||||
image: image,
|
||||
uid: image.uid,
|
||||
}, callback);
|
||||
}
|
||||
|
||||
saveFileToLocal(filename, image, callback);
|
||||
}
|
||||
|
||||
function generateProfileImageFilename(uid, type, extension) {
|
||||
var keepAllVersions = meta.config['profile:keepAllUserImages'] === 1;
|
||||
var convertToPNG = meta.config['profile:convertProfileImageToPNG'] === 1;
|
||||
return uid + '-' + type + (keepAllVersions ? '-' + Date.now() : '') + (convertToPNG ? '.png' : extension);
|
||||
}
|
||||
|
||||
function saveFileToLocal(filename, image, callback) {
|
||||
async.waterfall([
|
||||
function (next) {
|
||||
file.isFileTypeAllowed(image.path, next);
|
||||
},
|
||||
function (next) {
|
||||
file.saveFileToLocal(filename, 'profile', image.path, next);
|
||||
},
|
||||
function (upload, next) {
|
||||
next(null, {
|
||||
url: upload.url,
|
||||
path: upload.path,
|
||||
name: image.name,
|
||||
});
|
||||
},
|
||||
], callback);
|
||||
}
|
||||
|
||||
User.removeCoverPicture = function (data, callback) {
|
||||
db.deleteObjectFields('user:' + data.uid, ['cover:url', 'cover:position'], callback);
|
||||
};
|
||||
|
||||
@@ -54,7 +54,7 @@
|
||||
<p class="help-block">
|
||||
[[admin/settings/group:default-cover-help]]
|
||||
</p>
|
||||
<input type="text" class="form-control input-lg" id="groups:defaultCovers" data-field="groups:defaultCovers" data-field-type="tagsinput" value="{config.relative_path}/assets/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" /><br />
|
||||
<input type="text" class="form-control input-lg" id="groups:defaultCovers" data-field="groups:defaultCovers" data-field-type="tagsinput" value="/assets/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" /><br />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -178,7 +178,7 @@
|
||||
<p class="help-block">
|
||||
[[admin/settings/uploads:default-covers-help]]
|
||||
</p>
|
||||
<input type="text" class="form-control input-lg" id="profile:defaultCovers" data-field="profile:defaultCovers" data-field-type="tagsinput" value="{config.relative_path}/assets/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" />
|
||||
<input type="text" class="form-control input-lg" id="profile:defaultCovers" data-field="profile:defaultCovers" data-field-type="tagsinput" value="/assets/images/cover-default.png" placeholder="https://example.com/group1.png, https://example.com/group2.png" />
|
||||
</form>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert');
|
||||
var async = require('async');
|
||||
var nconf = require('nconf');
|
||||
|
||||
var db = require('./mocks/databasemock');
|
||||
var coverPhoto = require('../src/coverPhoto');
|
||||
@@ -9,16 +9,16 @@ var meta = require('../src/meta');
|
||||
|
||||
describe('coverPhoto', function () {
|
||||
it('should get default group cover', function (done) {
|
||||
meta.config['groups:defaultCovers'] = 'image1.png,image2.png';
|
||||
meta.config['groups:defaultCovers'] = '/assets/image1.png, /assets/image2.png';
|
||||
var result = coverPhoto.getDefaultGroupCover('registered-users');
|
||||
assert.equal(result, 'image2.png');
|
||||
assert.equal(result, nconf.get('relative_path') + '/assets/image2.png');
|
||||
done();
|
||||
});
|
||||
|
||||
it('should get default default profile cover', function (done) {
|
||||
meta.config['profile:defaultCovers'] = ' image1.png ,image2.png ';
|
||||
meta.config['profile:defaultCovers'] = ' /assets/image1.png, /assets/image2.png ';
|
||||
var result = coverPhoto.getDefaultProfileCover(1);
|
||||
assert.equal(result, 'image2.png');
|
||||
assert.equal(result, nconf.get('relative_path') + '/assets/image2.png');
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -1297,7 +1297,11 @@ describe('Groups', function () {
|
||||
assert.ifError(err);
|
||||
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||||
assert.ifError(err);
|
||||
assert.equal(data.url, groupData['cover:url']);
|
||||
assert.equal(nconf.get('relative_path') + data.url, groupData['cover:url']);
|
||||
if (nconf.get('relative_path')) {
|
||||
assert(!data.url.startsWith(nconf.get('relative_path')));
|
||||
assert(groupData['cover:url'].startsWith(nconf.get('relative_path')), groupData['cover:url']);
|
||||
}
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -1313,7 +1317,7 @@ describe('Groups', function () {
|
||||
assert.ifError(err);
|
||||
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||||
assert.ifError(err);
|
||||
assert.equal(data.url, groupData['cover:url']);
|
||||
assert.equal(nconf.get('relative_path') + data.url, groupData['cover:url']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
@@ -1361,7 +1365,7 @@ describe('Groups', function () {
|
||||
assert.equal(res.statusCode, 200);
|
||||
Groups.getGroupFields('Test', ['cover:url'], function (err, groupData) {
|
||||
assert.ifError(err);
|
||||
assert.equal(body[0].url, groupData['cover:url']);
|
||||
assert.equal(nconf.get('relative_path') + body[0].url, groupData['cover:url']);
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user