calculate size of base64 image from buffer, still not perfect but better than string.length
v1.18.x
Barış Soner Uşaklı 6 years ago
parent 352bea9475
commit 71ccd67665

@ -163,7 +163,10 @@ define('pictureCropper', ['translator', 'cropper', 'benchpress'], function (tran
var reader = new FileReader();
var imageUrl;
var imageType = file.type;
var fileSize = data.hasOwnProperty('fileSize') && data.fileSize !== undefined ? parseInt(data.fileSize, 10) : false;
if (file.size > fileSize * 1024) {
return app.alertError('[[error:file-too-big, ' + fileSize + ']]');
}
reader.addEventListener('load', function () {
imageUrl = reader.result;

@ -125,3 +125,7 @@ image.writeImageDataToTempFile = function (imageData, callback) {
callback(err, filepath);
});
};
image.sizeFromBase64 = function (imageData) {
return Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64').length;
};

@ -37,7 +37,7 @@ module.exports = function (User) {
async.waterfall([
function (next) {
var size = data.file ? data.file.size : data.imageData.length;
var size = data.file ? data.file.size : image.sizeFromBase64(data.imageData);
meta.config.maximumCoverImageSize = meta.config.maximumCoverImageSize || 2048;
if (size > parseInt(meta.config.maximumCoverImageSize, 10) * 1024) {
return next(new Error('[[error:file-too-big, ' + meta.config.maximumCoverImageSize + ']]'));
@ -89,10 +89,10 @@ module.exports = function (User) {
return callback(new Error('[[error:invalid-data]]'));
}
var size = data.file ? data.file.size : data.imageData.length;
var size = data.file ? data.file.size : image.sizeFromBase64(data.imageData);
var uploadSize = parseInt(meta.config.maximumProfileImageSize, 10) || 256;
if (size > uploadSize * 1024) {
return callback(new Error('[[error:file-too-big, ' + meta.config.maximumProfileImageSize + ']]'));
return callback(new Error('[[error:file-too-big, ' + uploadSize + ']]'));
}
var type = data.file ? data.file.type : image.mimeFromBase64(data.imageData);

Loading…
Cancel
Save