2014-03-05 23:00:27 -05:00
|
|
|
'use strict';
|
2014-02-09 00:34:05 -05:00
|
|
|
|
2017-02-20 21:27:56 +03:00
|
|
|
var os = require('os');
|
2016-08-30 13:10:48 +03:00
|
|
|
var fs = require('fs');
|
2017-02-20 21:27:56 +03:00
|
|
|
var path = require('path');
|
|
|
|
|
var crypto = require('crypto');
|
2018-09-20 17:05:52 -04:00
|
|
|
var async = require('async');
|
|
|
|
|
|
|
|
|
|
var sharp = require('sharp');
|
|
|
|
|
if (os.platform() === 'win32') {
|
|
|
|
|
// https://github.com/lovell/sharp/issues/1259
|
|
|
|
|
sharp.cache(false);
|
|
|
|
|
}
|
2017-02-20 21:27:56 +03:00
|
|
|
|
|
|
|
|
var file = require('./file');
|
2016-08-30 13:10:48 +03:00
|
|
|
var plugins = require('./plugins');
|
2014-02-09 00:34:05 -05:00
|
|
|
|
2016-08-30 13:10:48 +03:00
|
|
|
var image = module.exports;
|
2014-02-09 00:34:05 -05:00
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
image.resizeImage = function (data, callback) {
|
2015-06-04 12:32:39 -04:00
|
|
|
if (plugins.hasListeners('filter:image.resize')) {
|
|
|
|
|
plugins.fireHook('filter:image.resize', {
|
2015-09-24 12:04:24 -04:00
|
|
|
path: data.path,
|
2016-04-20 13:58:25 -04:00
|
|
|
target: data.target,
|
2015-09-24 12:04:24 -04:00
|
|
|
width: data.width,
|
2017-02-17 19:31:21 -07:00
|
|
|
height: data.height,
|
2018-03-19 16:24:15 -04:00
|
|
|
quality: data.quality,
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err) {
|
2015-06-04 12:32:39 -04:00
|
|
|
callback(err);
|
2015-05-30 20:04:42 +02:00
|
|
|
});
|
2015-06-04 12:32:39 -04:00
|
|
|
} else {
|
2018-09-20 17:05:52 -04:00
|
|
|
async.waterfall([
|
|
|
|
|
function (next) {
|
|
|
|
|
fs.readFile(data.path, next);
|
|
|
|
|
},
|
|
|
|
|
function (buffer, next) {
|
|
|
|
|
var sharpImage = sharp(buffer, {
|
|
|
|
|
failOnError: true,
|
|
|
|
|
});
|
|
|
|
|
sharpImage.rotate(); // auto-orients based on exif data
|
|
|
|
|
sharpImage.resize(data.hasOwnProperty('width') ? data.width : null, data.hasOwnProperty('height') ? data.height : null);
|
|
|
|
|
|
|
|
|
|
if (data.quality) {
|
|
|
|
|
sharpImage.jpeg({ quality: data.quality });
|
2016-04-20 13:58:25 -04:00
|
|
|
}
|
2018-09-20 17:05:52 -04:00
|
|
|
|
|
|
|
|
sharpImage.toFile(data.target || data.path, next);
|
|
|
|
|
},
|
|
|
|
|
], function (err) {
|
|
|
|
|
callback(err);
|
2015-09-21 15:36:08 -04:00
|
|
|
});
|
2015-06-04 12:32:39 -04:00
|
|
|
}
|
2014-02-09 00:34:05 -05:00
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
image.normalise = function (path, extension, callback) {
|
2015-06-04 12:32:39 -04:00
|
|
|
if (plugins.hasListeners('filter:image.normalise')) {
|
|
|
|
|
plugins.fireHook('filter:image.normalise', {
|
|
|
|
|
path: path,
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err) {
|
2017-02-20 21:27:56 +03:00
|
|
|
callback(err, path + '.png');
|
2015-06-04 12:32:39 -04:00
|
|
|
});
|
|
|
|
|
} else {
|
2018-09-20 17:05:52 -04:00
|
|
|
sharp(path, { failOnError: true }).png().toFile(path + '.png', function (err) {
|
|
|
|
|
callback(err, path + '.png');
|
|
|
|
|
});
|
2014-02-09 00:34:05 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
image.size = function (path, callback) {
|
2016-05-09 22:25:51 +03:00
|
|
|
if (plugins.hasListeners('filter:image.size')) {
|
|
|
|
|
plugins.fireHook('filter:image.size', {
|
|
|
|
|
path: path,
|
2016-10-13 11:43:39 +02:00
|
|
|
}, function (err, image) {
|
2018-09-20 17:05:52 -04:00
|
|
|
callback(err, image ? { width: image.width, height: image.height } : undefined);
|
2016-05-09 22:25:51 +03:00
|
|
|
});
|
|
|
|
|
} else {
|
2018-09-20 17:05:52 -04:00
|
|
|
sharp(path, { failOnError: true }).metadata(function (err, metadata) {
|
|
|
|
|
callback(err, metadata ? { width: metadata.width, height: metadata.height } : undefined);
|
2016-05-09 22:25:51 +03:00
|
|
|
});
|
|
|
|
|
}
|
2016-08-30 13:10:48 +03:00
|
|
|
};
|
2016-05-01 12:44:43 +03:00
|
|
|
|
2018-09-20 17:05:52 -04:00
|
|
|
image.checkDimensions = function (path, callback) {
|
|
|
|
|
const meta = require('./meta');
|
|
|
|
|
image.size(path, function (err, result) {
|
|
|
|
|
if (err) {
|
|
|
|
|
return callback(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const maxWidth = parseInt(meta.config.rejectImageWidth, 10) || 5000;
|
|
|
|
|
const maxHeight = parseInt(meta.config.rejectImageHeight, 10) || 5000;
|
|
|
|
|
if (result.width > maxWidth || result.height > maxHeight) {
|
|
|
|
|
return callback(new Error('[[error:invalid-image-dimensions]]'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
callback();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2016-10-13 11:43:39 +02:00
|
|
|
image.convertImageToBase64 = function (path, callback) {
|
2017-11-16 15:43:52 -07:00
|
|
|
fs.readFile(path, 'base64', callback);
|
2014-03-05 23:00:27 -05:00
|
|
|
};
|
2017-02-20 21:27:56 +03:00
|
|
|
|
|
|
|
|
image.mimeFromBase64 = function (imageData) {
|
|
|
|
|
return imageData.slice(5, imageData.indexOf('base64') - 1);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
image.extensionFromBase64 = function (imageData) {
|
|
|
|
|
return file.typeToExtension(image.mimeFromBase64(imageData));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
image.writeImageDataToTempFile = function (imageData, callback) {
|
|
|
|
|
var filename = crypto.createHash('md5').update(imageData).digest('hex');
|
|
|
|
|
|
|
|
|
|
var type = image.mimeFromBase64(imageData);
|
|
|
|
|
var extension = file.typeToExtension(type);
|
|
|
|
|
|
|
|
|
|
var filepath = path.join(os.tmpdir(), filename + extension);
|
|
|
|
|
|
2017-10-01 16:19:10 -06:00
|
|
|
var buffer = Buffer.from(imageData.slice(imageData.indexOf('base64') + 7), 'base64');
|
2017-02-20 21:27:56 +03:00
|
|
|
|
|
|
|
|
fs.writeFile(filepath, buffer, {
|
2017-02-23 18:31:49 -07:00
|
|
|
encoding: 'base64',
|
2017-02-20 21:27:56 +03:00
|
|
|
}, function (err) {
|
|
|
|
|
callback(err, filepath);
|
|
|
|
|
});
|
2017-02-23 18:31:49 -07:00
|
|
|
};
|