diff --git a/public/src/client/groups/details.js b/public/src/client/groups/details.js index 65534df0aa..10704698ac 100644 --- a/public/src/client/groups/details.js +++ b/public/src/client/groups/details.js @@ -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 + ')'); }); }, diff --git a/src/controllers/uploads.js b/src/controllers/uploads.js index 5ea85d7b6b..4e33bb4602 100644 --- a/src/controllers/uploads.js +++ b/src/controllers/uploads.js @@ -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', { diff --git a/src/coverPhoto.js b/src/coverPhoto.js index ec1b3f7be9..4c7afb21eb 100644 --- a/src/coverPhoto.js +++ b/src/coverPhoto.js @@ -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'; diff --git a/src/groups/cover.js b/src/groups/cover.js index ae2f86c90f..fc336d0e0d 100644 --- a/src/groups/cover.js +++ b/src/groups/cover.js @@ -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) { diff --git a/src/groups/data.js b/src/groups/data.js index 041a87fe9f..21fcc7911e 100644 --- a/src/groups/data.js +++ b/src/groups/data.js @@ -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%')); } } diff --git a/src/image.js b/src/image.js index 7789caa0b6..bc495bf9ef 100644 --- a/src/image.js +++ b/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); +}; diff --git a/src/user/picture.js b/src/user/picture.js index caae45e3e9..a7740ed28e 100644 --- a/src/user/picture.js +++ b/src/user/picture.js @@ -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); }; diff --git a/src/views/admin/settings/group.tpl b/src/views/admin/settings/group.tpl index 6f267baf57..5319ef5309 100644 --- a/src/views/admin/settings/group.tpl +++ b/src/views/admin/settings/group.tpl @@ -54,7 +54,7 @@
[[admin/settings/group:default-cover-help]]
-[[admin/settings/uploads:default-covers-help]]
- + diff --git a/test/coverPhoto.js b/test/coverPhoto.js index cb80e72938..1f3cfc27e9 100644 --- a/test/coverPhoto.js +++ b/test/coverPhoto.js @@ -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(); }); }); diff --git a/test/groups.js b/test/groups.js index 078f871d7f..c247652650 100644 --- a/test/groups.js +++ b/test/groups.js @@ -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(); }); });