diff --git a/app.js b/app.js index e1201f1bd1..a5da55bcfe 100644 --- a/app.js +++ b/app.js @@ -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') + '.'); diff --git a/public/src/forum/accountedit.js b/public/src/forum/accountedit.js index b681522457..858ef06bad 100644 --- a/public/src/forum/accountedit.js +++ b/public/src/forum/accountedit.js @@ -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); diff --git a/public/src/forum/admin/categories.js b/public/src/forum/admin/categories.js index 73eafde5df..47cf72b80a 100644 --- a/public/src/forum/admin/categories.js +++ b/public/src/forum/admin/categories.js @@ -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' + '' + ' '+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'), diff --git a/public/src/forum/admin/settings.js b/public/src/forum/admin/settings.js index b384b3b378..acb74827f4 100644 --- a/public/src/forum/admin/settings.js +++ b/public/src/forum/admin/settings.js @@ -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); }); diff --git a/public/src/modules/uploader.js b/public/src/modules/uploader.js index 381e0d2cad..dd51395de2 100644 --- a/public/src/modules/uploader.js +++ b/public/src/modules/uploader.js @@ -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(); diff --git a/public/templates/admin/footer.tpl b/public/templates/admin/footer.tpl index c7fd7dde5c..48d6288c8e 100644 --- a/public/templates/admin/footer.tpl +++ b/public/templates/admin/footer.tpl @@ -17,6 +17,7 @@

You may only upload PNG, JPG, or GIF files under 256kb.

+
diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl index 24e12add51..ed17d6d878 100644 --- a/public/templates/footer.tpl +++ b/public/templates/footer.tpl @@ -36,6 +36,7 @@

[[user:image_spec]]

+
diff --git a/src/routes/admin.js b/src/routes/admin.js index 88d3226039..273c373c51 100644 --- a/src/routes/admin.js +++ b/src/routes/admin.js @@ -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 = { diff --git a/src/routes/user.js b/src/routes/user.js index c97bcf6126..543bbb5162 100644 --- a/src/routes/user.js +++ b/src/routes/user.js @@ -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) {