closes #706, refactor admin image uploads, fixed gif uploads

v1.18.x
Baris Soner Usakli 11 years ago
parent fbb4998999
commit 6c30437c47

@ -69,8 +69,8 @@
});
meta = require('./src/meta');
nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path') + '/');
nconf.set('upload_url', nconf.get('url') + 'uploads/');
nconf.set('url', nconf.get('base_url') + (nconf.get('use_port') ? ':' + nconf.get('port') : '') + nconf.get('relative_path') + path.sep);
nconf.set('upload_url', path.join(path.sep, nconf.get('relative_path'), 'uploads', path.sep));
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') + '.');

@ -95,7 +95,7 @@ define(['forum/accountheader', 'uploader'], function(header, uploader) {
$('#uploadPictureBtn').on('click', function() {
$('#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();
$('#user-current-picture').attr('src', imageUrlOnServer);

@ -183,8 +183,8 @@ define(['uploader'], function(uploader) {
$('.upload-button').on('click', function() {
var inputEl = this;
uploader.open(RELATIVE_PATH + '/admin/category/uploadpicture', function(imageUrlOnServer) {
var cid = $(this).parents('li[data-cid]').attr('data-cid');
uploader.open(RELATIVE_PATH + '/admin/category/uploadpicture', {cid:cid}, function(imageUrlOnServer) {
inputEl.value = imageUrlOnServer;
$(inputEl).parents('li[data-cid]').find('.preview-box').css('background', 'url(' + imageUrlOnServer + '?' + new Date().getTime() + ')');
modified(inputEl);
@ -270,7 +270,7 @@ define(['uploader'], function(uploader) {
liEl = document.createElement('li');
var numResults = results.length,
resultObj;
for(var x=0;x<numResults;x++) {
resultObj = results[x];
liEl.setAttribute('data-gid', resultObj.gid);
@ -281,13 +281,13 @@ define(['uploader'], function(uploader) {
'</div>' +
'</div>' +
' '+resultObj.name;
groupsFrag.appendChild(liEl.cloneNode(true));
}
groupsResultsEl.html(groupsFrag);
});
groupsResultsEl.off().on('click', '[data-gpriv]', function(e) {
var btnEl = $(this),
gid = btnEl.parents('li[data-gid]').attr('data-gid'),

@ -75,7 +75,7 @@ define(['uploader'], function(uploader) {
});
$('#uploadLogoBtn').on('click', function() {
uploader.open(RELATIVE_PATH + '/admin/uploadlogo', function(image) {
uploader.open(RELATIVE_PATH + '/admin/uploadlogo', {}, function(image) {
$('#logoUrl').val(image);
});
@ -83,7 +83,7 @@ define(['uploader'], function(uploader) {
});
$('#uploadFaviconBtn').on('click', function() {
uploader.open(RELATIVE_PATH + '/admin/uploadfavicon', function(icon) {
uploader.open(RELATIVE_PATH + '/admin/uploadfavicon', {}, function(icon) {
$('#faviconUrl').val(icon);
});

@ -2,12 +2,13 @@ define(function() {
var module = {};
module.open = function(route, callback) {
module.open = function(route, params, callback) {
$('#upload-picture-modal').modal('show').removeClass('hide');
module.hideAlerts();
$('#uploadForm')[0].reset();
$('#uploadForm').attr('action', route);
$('#uploadForm').find('#params').val(JSON.stringify(params));
$('#pictureUploadSubmitBtn').off('click').on('click', function() {
$('#uploadForm').submit();

@ -17,6 +17,7 @@
<p class="help-block">You may only upload PNG, JPG, or GIF files under 256kb.</p>
</div>
<input id="imageUploadCsrf" type="hidden" name="_csrf" value="" />
<input type="hidden" id="params" name="params">
</form>
<div id="upload-progress-box" class="progress progress-striped">

@ -36,6 +36,7 @@
<p class="help-block">[[user:image_spec]]</p>
</div>
<input id="imageUploadCsrf" type="hidden" name="_csrf" value="" />
<input type="hidden" id="params" name="params">
</form>
<div id="upload-progress-box" class="progress progress-striped">

@ -104,6 +104,14 @@ var nconf = require('nconf'),
return res.redirect('/403');
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) {
res.send({
@ -122,28 +130,9 @@ var nconf = require('nconf'),
return;
}
var filename = 'category' + utils.generateUUID() + extension;
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);
console.log(nconf.get('upload_url') + filename);
res.json({
path: nconf.get('upload_url') + filename
});
});
var filename = 'category-' + params.cid + extension;
os.on('error', function (err) {
fs.unlinkSync(tempPath);
winston.err(err);
});
is.pipe(os);
uploadImage(filename, tempPath, res);
});
app.post('/uploadfavicon', function(req, res) {
@ -169,28 +158,7 @@ var nconf = require('nconf'),
return;
}
var filename = 'favicon.ico';
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);
uploadImage('favicon.ico', tempPath, res);
});
app.post('/uploadlogo', function(req, res) {
@ -218,29 +186,33 @@ var nconf = require('nconf'),
}
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);
function uploadImage(filename, tempPath, res) {
var uploadPath = path.join(nconf.get('base_dir'), nconf.get('upload_path'), filename);
is.on('end', function () {
fs.unlinkSync(tempPath);
winston.info('Attempting upload to: ' + uploadPath);
var is = fs.createReadStream(tempPath);
var os = fs.createWriteStream(uploadPath);
res.json({
path: nconf.get('upload_url') + filename
});
});
is.on('end', function () {
fs.unlinkSync(tempPath);
os.on('error', function (err) {
fs.unlinkSync(tempPath);
winston.err(err);
res.json({
path: nconf.get('upload_url') + filename
});
});
is.pipe(os);
os.on('error', function (err) {
fs.unlinkSync(tempPath);
winston.err(err);
});
});
is.pipe(os);
}
var custom_routes = {

@ -161,12 +161,7 @@ var fs = require('fs'),
is.on('end', function () {
fs.unlinkSync(tempPath);
im.crop({
srcPath: uploadPath,
dstPath: uploadPath,
width: 128,
height: 128
}, function (err, stdout, stderr) {
function done(err) {
if (err) {
winston.err(err);
res.send({
@ -180,7 +175,7 @@ var fs = require('fs'),
user.setUserField(uid, 'uploadedpicture', imageUrl);
user.setUserField(uid, 'picture', imageUrl);
if (convertToPNG) {
if (convertToPNG && extension !== '.png') {
im.convert([uploadPath, 'png:-'],
function(err, stdout){
if (err) {
@ -195,11 +190,25 @@ var fs = require('fs'),
});
}
res.json({
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) {

Loading…
Cancel
Save