mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-02 12:05:57 +01:00
closes #706, refactor admin image uploads, fixed gif uploads
This commit is contained in:
4
app.js
4
app.js
@@ -69,8 +69,8 @@
|
|||||||
});
|
});
|
||||||
meta = require('./src/meta');
|
meta = require('./src/meta');
|
||||||
|
|
||||||
nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path') + '/');
|
nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path') + path.sep);
|
||||||
nconf.set('upload_url', nconf.get('url') + 'uploads/');
|
nconf.set('upload_url', path.join(path.sep, nconf.get('relative_path'), 'uploads', path.sep));
|
||||||
nconf.set('base_dir', __dirname);
|
nconf.set('base_dir', __dirname);
|
||||||
|
|
||||||
winston.info('Initializing NodeBB v' + pkg.version + ', on port ' + nconf.get('port') + ', using Redis store at ' + nconf.get('redis:host') + ':' + nconf.get('redis:port') + '.');
|
winston.info('Initializing NodeBB v' + pkg.version + ', on port ' + nconf.get('port') + ', using Redis store at ' + nconf.get('redis:host') + ':' + nconf.get('redis:port') + '.');
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
|
|||||||
$('#uploadPictureBtn').on('click', function() {
|
$('#uploadPictureBtn').on('click', function() {
|
||||||
|
|
||||||
$('#change-picture-modal').modal('hide');
|
$('#change-picture-modal').modal('hide');
|
||||||
uploader.open(RELATIVE_PATH + '/user/uploadpicture', function(imageUrlOnServer) {
|
uploader.open(RELATIVE_PATH + '/user/uploadpicture', {}, function(imageUrlOnServer) {
|
||||||
imageUrlOnServer = imageUrlOnServer + '?' + new Date().getTime();
|
imageUrlOnServer = imageUrlOnServer + '?' + new Date().getTime();
|
||||||
|
|
||||||
$('#user-current-picture').attr('src', imageUrlOnServer);
|
$('#user-current-picture').attr('src', imageUrlOnServer);
|
||||||
|
|||||||
@@ -183,8 +183,8 @@ define(['uploader'], function(uploader) {
|
|||||||
|
|
||||||
$('.upload-button').on('click', function() {
|
$('.upload-button').on('click', function() {
|
||||||
var inputEl = this;
|
var inputEl = this;
|
||||||
|
var cid = $(this).parents('li[data-cid]').attr('data-cid');
|
||||||
uploader.open(RELATIVE_PATH + '/admin/category/uploadpicture', function(imageUrlOnServer) {
|
uploader.open(RELATIVE_PATH + '/admin/category/uploadpicture', {cid:cid}, function(imageUrlOnServer) {
|
||||||
inputEl.value = imageUrlOnServer;
|
inputEl.value = imageUrlOnServer;
|
||||||
$(inputEl).parents('li[data-cid]').find('.preview-box').css('background', 'url(' + imageUrlOnServer + '?' + new Date().getTime() + ')');
|
$(inputEl).parents('li[data-cid]').find('.preview-box').css('background', 'url(' + imageUrlOnServer + '?' + new Date().getTime() + ')');
|
||||||
modified(inputEl);
|
modified(inputEl);
|
||||||
@@ -270,7 +270,7 @@ define(['uploader'], function(uploader) {
|
|||||||
liEl = document.createElement('li');
|
liEl = document.createElement('li');
|
||||||
var numResults = results.length,
|
var numResults = results.length,
|
||||||
resultObj;
|
resultObj;
|
||||||
|
|
||||||
for(var x=0;x<numResults;x++) {
|
for(var x=0;x<numResults;x++) {
|
||||||
resultObj = results[x];
|
resultObj = results[x];
|
||||||
liEl.setAttribute('data-gid', resultObj.gid);
|
liEl.setAttribute('data-gid', resultObj.gid);
|
||||||
@@ -281,13 +281,13 @@ define(['uploader'], function(uploader) {
|
|||||||
'</div>' +
|
'</div>' +
|
||||||
'</div>' +
|
'</div>' +
|
||||||
' '+resultObj.name;
|
' '+resultObj.name;
|
||||||
|
|
||||||
groupsFrag.appendChild(liEl.cloneNode(true));
|
groupsFrag.appendChild(liEl.cloneNode(true));
|
||||||
}
|
}
|
||||||
|
|
||||||
groupsResultsEl.html(groupsFrag);
|
groupsResultsEl.html(groupsFrag);
|
||||||
});
|
});
|
||||||
|
|
||||||
groupsResultsEl.off().on('click', '[data-gpriv]', function(e) {
|
groupsResultsEl.off().on('click', '[data-gpriv]', function(e) {
|
||||||
var btnEl = $(this),
|
var btnEl = $(this),
|
||||||
gid = btnEl.parents('li[data-gid]').attr('data-gid'),
|
gid = btnEl.parents('li[data-gid]').attr('data-gid'),
|
||||||
|
|||||||
@@ -75,7 +75,7 @@ define(['uploader'], function(uploader) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#uploadLogoBtn').on('click', function() {
|
$('#uploadLogoBtn').on('click', function() {
|
||||||
uploader.open(RELATIVE_PATH + '/admin/uploadlogo', function(image) {
|
uploader.open(RELATIVE_PATH + '/admin/uploadlogo', {}, function(image) {
|
||||||
$('#logoUrl').val(image);
|
$('#logoUrl').val(image);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -83,7 +83,7 @@ define(['uploader'], function(uploader) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
$('#uploadFaviconBtn').on('click', function() {
|
$('#uploadFaviconBtn').on('click', function() {
|
||||||
uploader.open(RELATIVE_PATH + '/admin/uploadfavicon', function(icon) {
|
uploader.open(RELATIVE_PATH + '/admin/uploadfavicon', {}, function(icon) {
|
||||||
$('#faviconUrl').val(icon);
|
$('#faviconUrl').val(icon);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -2,12 +2,13 @@ define(function() {
|
|||||||
|
|
||||||
var module = {};
|
var module = {};
|
||||||
|
|
||||||
module.open = function(route, callback) {
|
module.open = function(route, params, callback) {
|
||||||
$('#upload-picture-modal').modal('show').removeClass('hide');
|
$('#upload-picture-modal').modal('show').removeClass('hide');
|
||||||
module.hideAlerts();
|
module.hideAlerts();
|
||||||
|
|
||||||
$('#uploadForm')[0].reset();
|
$('#uploadForm')[0].reset();
|
||||||
$('#uploadForm').attr('action', route);
|
$('#uploadForm').attr('action', route);
|
||||||
|
$('#uploadForm').find('#params').val(JSON.stringify(params));
|
||||||
|
|
||||||
$('#pictureUploadSubmitBtn').off('click').on('click', function() {
|
$('#pictureUploadSubmitBtn').off('click').on('click', function() {
|
||||||
$('#uploadForm').submit();
|
$('#uploadForm').submit();
|
||||||
|
|||||||
@@ -17,6 +17,7 @@
|
|||||||
<p class="help-block">You may only upload PNG, JPG, or GIF files under 256kb.</p>
|
<p class="help-block">You may only upload PNG, JPG, or GIF files under 256kb.</p>
|
||||||
</div>
|
</div>
|
||||||
<input id="imageUploadCsrf" type="hidden" name="_csrf" value="" />
|
<input id="imageUploadCsrf" type="hidden" name="_csrf" value="" />
|
||||||
|
<input type="hidden" id="params" name="params">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div id="upload-progress-box" class="progress progress-striped">
|
<div id="upload-progress-box" class="progress progress-striped">
|
||||||
|
|||||||
@@ -36,6 +36,7 @@
|
|||||||
<p class="help-block">[[user:image_spec]]</p>
|
<p class="help-block">[[user:image_spec]]</p>
|
||||||
</div>
|
</div>
|
||||||
<input id="imageUploadCsrf" type="hidden" name="_csrf" value="" />
|
<input id="imageUploadCsrf" type="hidden" name="_csrf" value="" />
|
||||||
|
<input type="hidden" id="params" name="params">
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<div id="upload-progress-box" class="progress progress-striped">
|
<div id="upload-progress-box" class="progress progress-striped">
|
||||||
|
|||||||
@@ -104,6 +104,14 @@ var nconf = require('nconf'),
|
|||||||
return res.redirect('/403');
|
return res.redirect('/403');
|
||||||
|
|
||||||
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
var allowedTypes = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
|
||||||
|
var params = null;
|
||||||
|
try {
|
||||||
|
params = JSON.parse(req.body.params);
|
||||||
|
} catch (e) {
|
||||||
|
return res.send({
|
||||||
|
error: 'Error uploading file! Error :' + e.message
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
if (allowedTypes.indexOf(req.files.userPhoto.type) === -1) {
|
||||||
res.send({
|
res.send({
|
||||||
@@ -122,28 +130,9 @@ var nconf = require('nconf'),
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var filename = 'category' + utils.generateUUID() + extension;
|
var filename = 'category-' + params.cid + extension;
|
||||||
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
|
||||||
|
|
||||||
winston.info('Attempting upload to: ' + uploadPath);
|
uploadImage(filename, tempPath, res);
|
||||||
|
|
||||||
var is = fs.createReadStream(tempPath);
|
|
||||||
var os = fs.createWriteStream(uploadPath);
|
|
||||||
|
|
||||||
is.on('end', function () {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
console.log(nconf.get('upload_url') + filename);
|
|
||||||
res.json({
|
|
||||||
path: nconf.get('upload_url') + filename
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
os.on('error', function (err) {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
winston.err(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
is.pipe(os);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/uploadfavicon', function(req, res) {
|
app.post('/uploadfavicon', function(req, res) {
|
||||||
@@ -169,28 +158,7 @@ var nconf = require('nconf'),
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
var filename = 'favicon.ico';
|
uploadImage('favicon.ico', tempPath, res);
|
||||||
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
|
||||||
|
|
||||||
winston.info('Attempting upload to: ' + uploadPath);
|
|
||||||
|
|
||||||
var is = fs.createReadStream(tempPath);
|
|
||||||
var os = fs.createWriteStream(uploadPath);
|
|
||||||
|
|
||||||
is.on('end', function () {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
path: nconf.get('upload_url') + filename
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
os.on('error', function (err) {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
winston.err(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
is.pipe(os);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
app.post('/uploadlogo', function(req, res) {
|
app.post('/uploadlogo', function(req, res) {
|
||||||
@@ -218,30 +186,34 @@ var nconf = require('nconf'),
|
|||||||
}
|
}
|
||||||
|
|
||||||
var filename = 'site-logo' + extension;
|
var filename = 'site-logo' + extension;
|
||||||
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
|
||||||
|
|
||||||
winston.info('Attempting upload to: ' + uploadPath);
|
uploadImage(filename, tempPath, res);
|
||||||
|
|
||||||
var is = fs.createReadStream(tempPath);
|
|
||||||
var os = fs.createWriteStream(uploadPath);
|
|
||||||
|
|
||||||
is.on('end', function () {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
|
|
||||||
res.json({
|
|
||||||
path: nconf.get('upload_url') + filename
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
os.on('error', function (err) {
|
|
||||||
fs.unlinkSync(tempPath);
|
|
||||||
winston.err(err);
|
|
||||||
});
|
|
||||||
|
|
||||||
is.pipe(os);
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
function uploadImage(filename, tempPath, res) {
|
||||||
|
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
|
||||||
|
|
||||||
|
winston.info('Attempting upload to: ' + uploadPath);
|
||||||
|
var is = fs.createReadStream(tempPath);
|
||||||
|
var os = fs.createWriteStream(uploadPath);
|
||||||
|
|
||||||
|
is.on('end', function () {
|
||||||
|
fs.unlinkSync(tempPath);
|
||||||
|
|
||||||
|
res.json({
|
||||||
|
path: nconf.get('upload_url') + filename
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
os.on('error', function (err) {
|
||||||
|
fs.unlinkSync(tempPath);
|
||||||
|
winston.err(err);
|
||||||
|
});
|
||||||
|
|
||||||
|
is.pipe(os);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
var custom_routes = {
|
var custom_routes = {
|
||||||
'routes': [],
|
'routes': [],
|
||||||
|
|||||||
@@ -161,12 +161,7 @@ var fs = require('fs'),
|
|||||||
is.on('end', function () {
|
is.on('end', function () {
|
||||||
fs.unlinkSync(tempPath);
|
fs.unlinkSync(tempPath);
|
||||||
|
|
||||||
im.crop({
|
function done(err) {
|
||||||
srcPath: uploadPath,
|
|
||||||
dstPath: uploadPath,
|
|
||||||
width: 128,
|
|
||||||
height: 128
|
|
||||||
}, function (err, stdout, stderr) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.err(err);
|
winston.err(err);
|
||||||
res.send({
|
res.send({
|
||||||
@@ -180,7 +175,7 @@ var fs = require('fs'),
|
|||||||
user.setUserField(uid, 'uploadedpicture', imageUrl);
|
user.setUserField(uid, 'uploadedpicture', imageUrl);
|
||||||
user.setUserField(uid, 'picture', imageUrl);
|
user.setUserField(uid, 'picture', imageUrl);
|
||||||
|
|
||||||
if (convertToPNG) {
|
if (convertToPNG && extension !== '.png') {
|
||||||
im.convert([uploadPath, 'png:-'],
|
im.convert([uploadPath, 'png:-'],
|
||||||
function(err, stdout){
|
function(err, stdout){
|
||||||
if (err) {
|
if (err) {
|
||||||
@@ -195,11 +190,25 @@ var fs = require('fs'),
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
res.json({
|
res.json({
|
||||||
path: imageUrl
|
path: imageUrl
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|
||||||
|
if(extension === '.gif') {
|
||||||
|
im.convert([uploadPath, '-coalesce', '-repage', '0x0', '-crop', '128x128+0+0', '+repage', 'uploadPath'], function(err, stdout) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
} else {
|
||||||
|
im.crop({
|
||||||
|
srcPath: uploadPath,
|
||||||
|
dstPath: uploadPath,
|
||||||
|
width: 128,
|
||||||
|
height: 128
|
||||||
|
}, function (err, stdout, stderr) {
|
||||||
|
done(err);
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
os.on('error', function (err) {
|
os.on('error', function (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user