Add extensions for cropped images

v1.18.x
pichalite 8 years ago
parent a379b03e71
commit 8dab8864f6

@ -18,7 +18,7 @@ define('pictureCropper', ['translator', 'cropper'], function (translator, croppe
uploadModal = $(uploadModal); uploadModal = $(uploadModal);
uploadModal.modal('show'); uploadModal.modal('show');
uploadModal.on('hidden.bs.modal', function () { uploadModal.on('hidden.bs.modal', function () {
uploadModal.remove(); uploadModal.remove();
}); });
@ -31,9 +31,11 @@ define('pictureCropper', ['translator', 'cropper'], function (translator, croppe
}); });
}; };
module.handleImageCrop = function (data, callback) { module.handleImageCrop = function (data, callback) {
$('#crop-picture-modal').remove(); $('#crop-picture-modal').remove();
templates.parse('modals/crop_picture', {url: data.url}, function (cropperHtml) { templates.parse('modals/crop_picture', {
url: data.url
}, function (cropperHtml) {
translator.translate(cropperHtml, function (translated) { translator.translate(cropperHtml, function (translated) {
var cropperModal = $(translated); var cropperModal = $(translated);
cropperModal.modal('show'); cropperModal.modal('show');
@ -58,28 +60,28 @@ define('pictureCropper', ['translator', 'cropper'], function (translator, croppe
cropperModal.find('.reset').on('click', function () { cropperModal.find('.reset').on('click', function () {
cropperTool.reset(); cropperTool.reset();
}); });
cropperModal.find('.crop-btn').on('click', function () { cropperModal.find('.crop-btn').on('click', function () {
$(this).addClass('disabled'); $(this).addClass('disabled');
var imageData = data.imageType ? cropperTool.getCroppedCanvas().toDataURL(data.imageType) : cropperTool.getCroppedCanvas().toDataURL(); var imageData = data.imageType ? cropperTool.getCroppedCanvas().toDataURL(data.imageType) : cropperTool.getCroppedCanvas().toDataURL();
cropperModal.find('#upload-progress-bar').css('width', '100%'); cropperModal.find('#upload-progress-bar').css('width', '100%');
cropperModal.find('#upload-progress-box').show().removeClass('hide'); cropperModal.find('#upload-progress-box').show().removeClass('hide');
var socketData = {}; var socketData = {};
socketData[data.paramName] = data.paramValue; socketData[data.paramName] = data.paramValue;
socketData['imageData'] = imageData; socketData['imageData'] = imageData;
socket.emit(data.socketMethod, socketData, function (err, imageData) { socket.emit(data.socketMethod, socketData, function (err, imageData) {
if (err) { if (err) {
cropperModal.find('#upload-progress-box').hide(); cropperModal.find('#upload-progress-box').hide();
cropperModal.find('.upload-btn').removeClass('disabled'); cropperModal.find('.upload-btn').removeClass('disabled');
cropperModal.find('.crop-btn').removeClass('disabled'); cropperModal.find('.crop-btn').removeClass('disabled');
return app.alertError(err.message); return app.alertError(err.message);
} }
callback(imageData.url); callback(imageData.url);
cropperModal.modal('hide'); cropperModal.modal('hide');
}); });
}); });
@ -115,8 +117,8 @@ define('pictureCropper', ['translator', 'cropper'], function (translator, croppe
return showAlert('error', '[[uploads:select-file-to-upload]]'); return showAlert('error', '[[uploads:select-file-to-upload]]');
} }
var file = fileInput[0].files[0]; var file = fileInput[0].files[0];
var reader = new FileReader(); var reader = new FileReader();
var imageUrl; var imageUrl;
var imageType = file.type; var imageType = file.type;

@ -6,6 +6,7 @@ var path = require('path');
var winston = require('winston'); var winston = require('winston');
var jimp = require('jimp'); var jimp = require('jimp');
var mkdirp = require('mkdirp'); var mkdirp = require('mkdirp');
var mime = require('mime');
var utils = require('../public/src/utils'); var utils = require('../public/src/utils');
@ -13,8 +14,8 @@ var file = {};
file.saveFileToLocal = function (filename, folder, tempPath, callback) { file.saveFileToLocal = function (filename, folder, tempPath, callback) {
/* /*
* remarkable doesn't allow spaces in hyperlinks, once that's fixed, remove this. * remarkable doesn't allow spaces in hyperlinks, once that's fixed, remove this.
*/ */
filename = filename.split('.'); filename = filename.split('.');
filename.forEach(function (name, idx) { filename.forEach(function (name, idx) {
filename[idx] = utils.slugify(name); filename[idx] = utils.slugify(name);
@ -100,7 +101,8 @@ file.existsSync = function (path) {
var exists = false; var exists = false;
try { try {
exists = fs.statSync(path); exists = fs.statSync(path);
} catch(err) { }
catch (err) {
exists = false; exists = false;
} }
@ -110,7 +112,8 @@ file.existsSync = function (path) {
file.link = function link(filePath, destPath, cb) { file.link = function link(filePath, destPath, cb) {
if (process.platform === 'win32') { if (process.platform === 'win32') {
fs.link(filePath, destPath, cb); fs.link(filePath, destPath, cb);
} else { }
else {
fs.symlink(filePath, destPath, 'file', cb); fs.symlink(filePath, destPath, 'file', cb);
} }
}; };
@ -120,4 +123,12 @@ file.linkDirs = function linkDirs(sourceDir, destDir, callback) {
fs.symlink(sourceDir, destDir, type, callback); fs.symlink(sourceDir, destDir, type, callback);
}; };
file.typeToExtension = function (type) {
var extension;
if (type) {
extension = '.' + mime.extension(type);
}
return extension;
};
module.exports = file; module.exports = file;

@ -8,7 +8,6 @@ var nconf = require('nconf');
var crypto = require('crypto'); var crypto = require('crypto');
var winston = require('winston'); var winston = require('winston');
var request = require('request'); var request = require('request');
var mime = require('mime');
var plugins = require('../plugins'); var plugins = require('../plugins');
var file = require('../file'); var file = require('../file');
@ -43,7 +42,10 @@ module.exports = function (User) {
async.waterfall([ async.waterfall([
function (next) { function (next) {
if (plugins.hasListeners('filter:uploadImage')) { if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', {image: picture, uid: updateUid}, next); return plugins.fireHook('filter:uploadImage', {
image: picture,
uid: updateUid
}, next);
} }
var filename = updateUid + '-profileimg' + (keepAllVersions ? '-' + Date.now() : '') + (convertToPNG ? '.png' : extension); var filename = updateUid + '-profileimg' + (keepAllVersions ? '-' + Date.now() : '') + (convertToPNG ? '.png' : extension);
@ -79,7 +81,10 @@ module.exports = function (User) {
}, },
function (_image, next) { function (_image, next) {
uploadedImage = _image; uploadedImage = _image;
User.setUserFields(updateUid, {uploadedpicture: uploadedImage.url, picture: uploadedImage.url}, next); User.setUserFields(updateUid, {
uploadedpicture: uploadedImage.url,
picture: uploadedImage.url
}, next);
}, },
function (next) { function (next) {
next(null, uploadedImage); next(null, uploadedImage);
@ -99,7 +104,7 @@ module.exports = function (User) {
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256; var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
var size = res.headers['content-length']; var size = res.headers['content-length'];
var type = res.headers['content-type']; var type = res.headers['content-type'];
var extension = mime.extension(type); var extension = file.typeToExtension(type);
if (['png', 'jpeg', 'jpg', 'gif'].indexOf(extension) === -1) { if (['png', 'jpeg', 'jpg', 'gif'].indexOf(extension) === -1) {
return callback(new Error('[[error:invalid-image-extension]]')); return callback(new Error('[[error:invalid-image-extension]]'));
@ -109,12 +114,21 @@ module.exports = function (User) {
return callback(new Error('[[error:file-too-big, ' + uploadSize + ']]')); return callback(new Error('[[error:file-too-big, ' + uploadSize + ']]'));
} }
var picture = {url: url, name: ''}; var picture = {
plugins.fireHook('filter:uploadImage', {image: picture, uid: uid}, function (err, image) { url: url,
name: ''
};
plugins.fireHook('filter:uploadImage', {
image: picture,
uid: uid
}, function (err, image) {
if (err) { if (err) {
return callback(err); return callback(err);
} }
User.setUserFields(uid, {uploadedpicture: image.url, picture: image.url}); User.setUserFields(uid, {
uploadedpicture: image.url,
picture: image.url
});
callback(null, image); callback(null, image);
}); });
}); });
@ -170,10 +184,14 @@ module.exports = function (User) {
}; };
if (plugins.hasListeners('filter:uploadImage')) { if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', {image: image, uid: data.uid}, next); return plugins.fireHook('filter:uploadImage', {
image: image,
uid: data.uid
}, next);
} }
var filename = data.uid + '-profilecover' + (keepAllVersions ? '-' + Date.now() : ''); var extension = file.typeToExtension(data.imageData.slice(5, data.imageData.indexOf('base64') - 1));
var filename = data.uid + '-profilecover' + (keepAllVersions ? '-' + Date.now() : '') + (extension || '');
async.waterfall([ async.waterfall([
function (next) { function (next) {
file.isFileTypeAllowed(data.file.path, next); file.isFileTypeAllowed(data.file.path, next);
@ -208,20 +226,25 @@ module.exports = function (User) {
winston.error(unlinkErr); winston.error(unlinkErr);
} }
callback(err); // send back the original error callback(err); // send back the original error
}); });
} }
if (data.position) { if (data.position) {
User.updateCoverPosition(data.uid, data.position, function (err) { User.updateCoverPosition(data.uid, data.position, function (err) {
callback(err, {url: url}); callback(err, {
url: url
});
});
}
else {
callback(err, {
url: url
}); });
} else {
callback(err, {url: url});
} }
}); });
}; };
User.uploadCroppedPicture = function (data, callback) { User.uploadCroppedPicture = function (data, callback) {
var keepAllVersions = parseInt(meta.config['profile:keepAllUserImages'], 10) === 1; var keepAllVersions = parseInt(meta.config['profile:keepAllUserImages'], 10) === 1;
var url, md5sum; var url, md5sum;
@ -229,7 +252,7 @@ module.exports = function (User) {
if (!data.imageData) { if (!data.imageData) {
return callback(new Error('[[error:invalid-data]]')); return callback(new Error('[[error:invalid-data]]'));
} }
async.waterfall([ async.waterfall([
function (next) { function (next) {
var size = data.file ? data.file.size : data.imageData.length; var size = data.file ? data.file.size : data.imageData.length;
@ -260,10 +283,14 @@ module.exports = function (User) {
}; };
if (plugins.hasListeners('filter:uploadImage')) { if (plugins.hasListeners('filter:uploadImage')) {
return plugins.fireHook('filter:uploadImage', {image: image, uid: data.uid}, next); return plugins.fireHook('filter:uploadImage', {
image: image,
uid: data.uid
}, next);
} }
var filename = data.uid + '-profileavatar' + (keepAllVersions ? '-' + Date.now() : ''); var extension = file.typeToExtension(data.imageData.slice(5, data.imageData.indexOf('base64') - 1));
var filename = data.uid + '-profileavatar' + (keepAllVersions ? '-' + Date.now() : '') + (extension || '');
async.waterfall([ async.waterfall([
function (next) { function (next) {
file.isFileTypeAllowed(data.file.path, next); file.isFileTypeAllowed(data.file.path, next);
@ -281,7 +308,10 @@ module.exports = function (User) {
}, },
function (uploadData, next) { function (uploadData, next) {
url = uploadData.url; url = uploadData.url;
User.setUserFields(data.uid, {uploadedpicture: url, picture: url}, next); User.setUserFields(data.uid, {
uploadedpicture: url,
picture: url
}, next);
}, },
function (next) { function (next) {
fs.unlink(data.file.path, function (err) { fs.unlink(data.file.path, function (err) {
@ -293,10 +323,12 @@ module.exports = function (User) {
} }
], function (err) { ], function (err) {
if (err) { if (err) {
callback(err); // send back the original error callback(err); // send back the original error
} }
callback(err, {url: url}); callback(err, {
url: url
});
}); });
}; };

Loading…
Cancel
Save