Change image compression plugin to use janus-image-worker.
This commit is contained in:
Родитель
fb2cef872a
Коммит
44448ce615
|
@ -135,6 +135,8 @@ cache:
|
|||
# Image compression settings.
|
||||
imgcompression:
|
||||
enabled: true
|
||||
# Use libjpeg-turbo instead of mozjpeg.
|
||||
turbo: true
|
||||
|
||||
# Adblock settings.
|
||||
adblock:
|
||||
|
|
|
@ -1,11 +1,8 @@
|
|||
'use strict';
|
||||
|
||||
var fs = require('fs');
|
||||
var temp = require ('temp');
|
||||
var execFile = require('child_process').execFile;
|
||||
var jpegtran = require('mozjpeg-bin').jpegtran;
|
||||
var pngquant = require('pngquant-bin').path;
|
||||
var imgtype = require('imagetype');
|
||||
var spawn = require('child_process').spawn;
|
||||
var config = require('config');
|
||||
var imageWorker = require('janus-image-worker');
|
||||
|
||||
var log = require('../log');
|
||||
|
||||
|
@ -13,77 +10,37 @@ var NAME = exports.name = 'imgcompression';
|
|||
|
||||
var metrics = require('../metrics').session(NAME);
|
||||
|
||||
// Send processed image from given path to the given destination.
|
||||
function sendImage(source, dest, path) {
|
||||
fs.stat(path, function(err, stats) {
|
||||
var finalize = function() {
|
||||
fs.unlink(path);
|
||||
};
|
||||
|
||||
if (source.headers['content-length']) {
|
||||
var origLength = parseInt(source.headers['content-length']);
|
||||
var newLength = stats.size.toString();
|
||||
|
||||
source.headers['content-length'] = newLength;
|
||||
|
||||
metrics.count('in', origLength);
|
||||
metrics.count('out', newLength);
|
||||
}
|
||||
|
||||
var imageFile = fs.createReadStream(path);
|
||||
|
||||
dest.writeHead(source.statusCode, source.headers);
|
||||
imageFile.pipe(dest);
|
||||
|
||||
dest.on('end', finalize);
|
||||
dest.on('close', finalize);
|
||||
dest.on('error', finalize);
|
||||
});
|
||||
}
|
||||
|
||||
exports.handleResponse = function(request, source, dest) {
|
||||
// If this is an image we first write it into a file, then optimize it
|
||||
// and send it.
|
||||
if (source.headers['content-type'] === 'image/jpeg' ||
|
||||
source.headers['content-type'] === 'image/png') {
|
||||
|
||||
var path = temp.path();
|
||||
var optPath = path + '.opt';
|
||||
var imageFile = fs.createWriteStream(path);
|
||||
metrics.count('hit');
|
||||
|
||||
source.pipe(imageFile);
|
||||
var args = ['--small'];
|
||||
|
||||
imageFile.on('finish', function() {
|
||||
// We need to check the actual type of the downloaded image.
|
||||
// Some websites (e.g. mozilla.org) are sending JPEGs as PNGs.
|
||||
imgtype(path, function(type) {
|
||||
if (type === 'png') {
|
||||
execFile(pngquant,
|
||||
['--skip-if-larger', '-o', optPath, path],
|
||||
function(err) {
|
||||
if (err) {
|
||||
sendImage(source, dest, path);
|
||||
} else {
|
||||
sendImage(source, dest, optPath);
|
||||
fs.unlink(path);
|
||||
}
|
||||
});
|
||||
} else if (type === 'jpeg') {
|
||||
execFile(jpegtran,
|
||||
['-outfile', optPath, path],
|
||||
function(err, stdout, stderr) {
|
||||
if (err) {
|
||||
log.error('mozjpeg error: ', stderr);
|
||||
sendImage(source, dest, path);
|
||||
} else {
|
||||
sendImage(source, dest, optPath);
|
||||
fs.unlink(path);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
sendImage(source, dest, path);
|
||||
}
|
||||
});
|
||||
if (config.imgcompression.turbo) {
|
||||
args[0] = '--fast';
|
||||
}
|
||||
|
||||
delete source.headers['content-length'];
|
||||
dest.writeHead(source.statusCode, source.headers);
|
||||
|
||||
var child = spawn(imageWorker.bin,
|
||||
args,
|
||||
{
|
||||
stdio: 'pipe',
|
||||
env: { LD_LIBRARY_PATH: imageWorker.libraryPath }
|
||||
});
|
||||
|
||||
source.pipe(child.stdin);
|
||||
child.stdout.pipe(dest);
|
||||
|
||||
child.stderr.on('data', function(data) {
|
||||
log.info(data.toString());
|
||||
});
|
||||
|
||||
child.stdin.on('error', function(data) {
|
||||
log.error(data.toString());
|
||||
});
|
||||
} else {
|
||||
source.forward(dest);
|
||||
|
|
|
@ -31,15 +31,14 @@
|
|||
"hiredis": "^0.1.17",
|
||||
"iconv": "^2.1.4",
|
||||
"imagetype": "^0.3.0",
|
||||
"janus-image-worker": "git+https://github.com/mozilla/janus-image-worker.git",
|
||||
"js-yaml": "^3.0.2",
|
||||
"lzma-native": "^0.1.2",
|
||||
"marionette-client": "^1.1.8",
|
||||
"memory-cache": "0.0.5",
|
||||
"memwatch": "^0.2.2",
|
||||
"mozjpeg-bin": "^0.2.0",
|
||||
"node-statsd": "0.0.7",
|
||||
"png": "~3.0.3",
|
||||
"pngquant-bin": "^0.3.1",
|
||||
"redis": "^0.10.3",
|
||||
"spdy": "~1.27.0",
|
||||
"sprintf-js": "0.0.7",
|
||||
|
|
Загрузка…
Ссылка в новой задаче