Skip to content

Commit b3f382b

Browse files
committed
Added Image.middleware().
1 parent 2a03e1c commit b3f382b

4 files changed

Lines changed: 41 additions & 13 deletions

File tree

changes.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
- added: `U.get(obj, path)` reads a value from `obj` by path
77
- added: `U.set(obj, path, value)` sets a value into `obj` by path
88
- added: (IMPORTANT) `config['default-root']` can replace root relative path
9-
- added: FrameworkImage --> `instance.make(function(image) {})`
9+
- added: `FrameworkImage` --> `instance.make(function(image) {})`
10+
- added: `FrameworkImage` supports middleware `FrameworkImage.middleware(type, fn)`
1011

1112
- updated: (IMPORTANT) Array.async([NEW: threadCount (Number)], [callback]) supports `threads`
1213
- updated: Date.format(format, [resource_name]) supports name of months via `MMM` (short) and `MMMM` (full)

image.js

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ var exec = child.exec;
3131
var spawn = child.spawn;
3232
var path = require('path');
3333
var sof = { 0xc0: true, 0xc1: true, 0xc2: true, 0xc3: true, 0xc5: true, 0xc6: true, 0xc7: true, 0xc9: true, 0xca: true, 0xcb: true, 0xcd: true, 0xce: true, 0xcf: true };
34+
var middlewares = {};
35+
var Fs = require('fs');
3436

3537
function u16(buf, o) {
3638
return buf[o] << 8 | buf[o + 1];
@@ -244,10 +246,25 @@ Image.prototype.save = function(filename, callback, writer) {
244246
if (!callback)
245247
return;
246248

247-
if (error)
249+
if (error) {
248250
callback(error, false);
249-
else
250-
callback(null, true);
251+
return;
252+
}
253+
254+
var middleware = middlewares[self.outputType];
255+
if (!middleware)
256+
return callback(null, true);
257+
258+
var reader = Fs.createReadStream(filename);
259+
var writer = Fs.createWriteStream(filename + '_');
260+
261+
reader.pipe(middleware()).pipe(writer);
262+
263+
writer.on('finish', function() {
264+
Fs.rename(filename + '_', filename, function() {
265+
callback(null, true);
266+
});
267+
});
251268
});
252269

253270
if (self.currentStream) {
@@ -293,7 +310,7 @@ Image.prototype.pipe = function(stream, type, options) {
293310
if (!self.builder.length)
294311
return;
295312

296-
if (type === undefined || type === null)
313+
if (!type)
297314
type = self.outputType;
298315

299316
var cmd = spawn(self.isIM ? 'convert' : 'gm', self.arg(!self.filename ? '-' : self.filename, (type ? type + ':' : '') + '-'));
@@ -302,7 +319,12 @@ Image.prototype.pipe = function(stream, type, options) {
302319
cmd.stdout.on('data', stream.emit.bind(stream, 'data'));
303320
cmd.stdout.on('end', stream.emit.bind(stream, 'end'));
304321
cmd.on('error', stream.emit.bind(stream, 'error'));
305-
cmd.stdout.pipe(stream, options);
322+
323+
var middleware = middlewares[type];
324+
if (middleware)
325+
cmd.stdout.pipe(middleware()).pipe(stream, options);
326+
else
327+
cmd.stdout.pipe(stream, options);
306328

307329
if (self.currentStream) {
308330
if (self.currentStream instanceof Buffer)
@@ -341,7 +363,11 @@ Image.prototype.stream = function(type, writer) {
341363
if (writer)
342364
writer(cmd.stdin);
343365

344-
return cmd.stdout;
366+
var middleware = middlewares[type];
367+
if (!middleware)
368+
return cmd.stdout;
369+
370+
return cmd.stdout.pipe(middleware());
345371
};
346372

347373
/*
@@ -743,9 +769,6 @@ Image.prototype.colors = function(value) {
743769
return this.push('-colors', value, 10);
744770
};
745771

746-
/*
747-
@color {String}
748-
*/
749772
Image.prototype.background = function(color) {
750773
return this.push('-background', color, 2);
751774
};
@@ -785,4 +808,8 @@ exports.init = function(filename, imageMagick, width, height) {
785808

786809
exports.load = function(filename, imageMagick, width, height) {
787810
return new Image(filename, imageMagick, width, height);
788-
};
811+
};
812+
813+
exports.middleware = function(type, fn) {
814+
middlewares[type] = fn;
815+
};

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ function Framework() {
428428

429429
this.id = null;
430430
this.version = 1970;
431-
this.version_header = '1.9.7-17';
431+
this.version_header = '1.9.7-18';
432432

433433
var version = process.version.toString().replace('v', '').replace(/\./g, '');
434434
if (version[0] !== '0' || version[1] !== '0')

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"name": "Сковорода Никита Андреевич",
6464
"email": "chalkerx@gmail.com"
6565
}],
66-
"version": "1.9.7-17",
66+
"version": "1.9.7-18",
6767
"homepage": "http://www.totaljs.com",
6868
"bugs": {
6969
"url": "https://github.com/totaljs/framework/issues",

0 commit comments

Comments
 (0)