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');
|
||||
|
||||
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);
|
||||
|
||||
@@ -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);
|
||||
var filename = 'category-' + params.cid + extension;
|
||||
|
||||
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
|
||||
});
|
||||
});
|
||||
|
||||
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,10 +186,15 @@ var nconf = require('nconf'),
|
||||
}
|
||||
|
||||
var filename = 'site-logo' + extension;
|
||||
|
||||
uploadImage(filename, tempPath, res);
|
||||
});
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
@@ -239,8 +212,7 @@ var nconf = require('nconf'),
|
||||
});
|
||||
|
||||
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) {
|
||||
|
||||
Reference in New Issue
Block a user