Galerie und tage

This commit is contained in:
2021-11-23 17:56:26 +01:00
parent ff35366279
commit 5f873bee89
4693 changed files with 149659 additions and 301447 deletions

View File

@@ -1,41 +1,66 @@
'use strict';
const execBuffer = require('exec-buffer');
const gifsicle = require('gifsicle');
const isGif = require('is-gif');
module.exports = opts => buf => {
opts = Object.assign({}, opts);
var spawn = require('child_process').spawn;
var gifsicle = require('gifsicle');
var isGif = require('is-gif');
var through = require('through2');
if (!Buffer.isBuffer(buf)) {
return Promise.reject(new TypeError('Expected a buffer'));
}
module.exports = function (opts) {
opts = opts || {};
if (!isGif(buf)) {
return Promise.resolve(buf);
}
return through.ctor({objectMode: true}, function (file, enc, cb) {
if (file.isNull()) {
cb(null, file);
return;
}
const args = ['--no-warnings', '--no-app-extensions'];
if (file.isStream()) {
cb(new Error('Streaming is not supported'));
return;
}
if (opts.interlaced) {
args.push('--interlace');
}
if (!isGif(file.contents)) {
cb(null, file);
return;
}
if (opts.optimizationLevel) {
args.push(`--optimize=${opts.optimizationLevel}`);
}
var args = ['-w'];
var ret = [];
var len = 0;
if (opts.colors) {
args.push(`--colors=${opts.colors}`);
}
if (opts.interlaced) {
args.push('--interlace');
}
args.push('--output', execBuffer.output, execBuffer.input);
var cp = spawn(gifsicle, args);
return execBuffer({
input: buf,
bin: gifsicle,
args
}).catch(error => {
error.message = error.stderr || error.message;
throw error;
cp.stderr.setEncoding('utf8');
cp.stderr.on('data', function (data) {
var err = new Error(data);
err.fileName = file.path;
cb(err);
return;
});
cp.stdout.on('data', function (data) {
ret.push(data);
len += data.length;
});
cp.on('error', function (err) {
err.fileName = file.path;
cb(err);
return;
});
cp.on('close', function () {
if (len < file.contents.length) {
file.contents = Buffer.concat(ret, len);
}
cb(null, file);
});
cp.stdin.end(file.contents);
});
};