From b0c9a5f23c21fbb736861ae1d5a97062f3e655eb Mon Sep 17 00:00:00 2001 From: "Merrifield, Jay" Date: Wed, 28 Oct 2015 16:57:11 -0400 Subject: [PATCH 1/2] Added support for node streams to image magic --- imagemagick.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/imagemagick.js b/imagemagick.js index b846c0c..48d7e8b 100644 --- a/imagemagick.js +++ b/imagemagick.js @@ -140,8 +140,8 @@ exports.identify = function(pathOrArgs, callback) { isData = true; pathOrArgs = args[args.length-1]; args[args.length-1] = '-'; - if (!pathOrArgs.data) - throw new Error('first argument is missing the "data" member'); + if (!pathOrArgs.data && !pathOrArgs.stream) + throw new Error('first argument must contain "data" or "stream" member'); } else if (typeof pathOrArgs === 'function') { args[args.length-1] = '-'; callback = pathOrArgs; @@ -165,7 +165,9 @@ exports.identify = function(pathOrArgs, callback) { callback(err, result); }); if (isData) { - if ('string' === typeof pathOrArgs.data) { + if (pathOrArgs.stream) { + pathOrArgs.stream.pipe(proc.stdin); + } else if ('string' === typeof pathOrArgs.data) { proc.stdin.setEncoding('binary'); proc.stdin.write(pathOrArgs.data, 'binary'); proc.stdin.end(); From fd2c81c71a824a2280d4d42931c9a0365003e664 Mon Sep 17 00:00:00 2001 From: dkrantsberg Date: Tue, 9 May 2017 15:17:17 -0400 Subject: [PATCH 2/2] changed the logic to first check whether the next line is indented and if it is then make the current property an object as opposed to a string value. --- imagemagick.js | 65 ++++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 29 deletions(-) diff --git a/imagemagick.js b/imagemagick.js index 48d7e8b..1536d22 100644 --- a/imagemagick.js +++ b/imagemagick.js @@ -99,36 +99,43 @@ function exec2(file, args /*, options, callback */) { function parseIdentify(input) { - var lines = input.split("\n"), - prop = {}, - props = [prop], - prevIndent = 0, - indents = [indent], - currentLine, comps, indent, i; - - lines.shift(); //drop first line (Image: name.jpg) - - for (i in lines) { - currentLine = lines[i]; - indent = currentLine.search(/\S/); - if (indent >= 0) { - comps = currentLine.split(': '); - if (indent > prevIndent) indents.push(indent); - while (indent < prevIndent && props.length) { - indents.pop(); - prop = props.pop(); - prevIndent = indents[indents.length - 1]; - } - if (comps.length < 2) { - props.push(prop); - prop = prop[currentLine.split(':')[0].trim().toLowerCase()] = {}; - } else { - prop[comps[0].trim().toLowerCase()] = comps[1].trim() - } - prevIndent = indent; + var lines = input.split("\n"), + prop = {}, + props = [prop], + prevIndent = 0, + indents = [indent], + currentLine, comps, indent, i, nextIndent; + + lines.shift(); //drop first line (Image: name.jpg) + + for (i in lines) { + currentLine = lines[i]; + indent = nextIndent || currentLine.search(/\S/); + if (indent >= 0) { + comps = currentLine.split(': '); + if (indent > prevIndent) indents.push(indent); + while (indent < prevIndent && props.length) { + indents.pop(); + prop = props.pop(); + prevIndent = indents[indents.length - 1]; + } + nextIndent = getLineIndent(parseInt(i) + 1, lines); + if (comps.length < 2 || nextIndent > indent) { + props.push(prop); + prop = prop[currentLine.split(':')[0].trim().toLowerCase()] = {}; + } else { + prop[comps[0].trim().toLowerCase()] = comps[1].trim() + } + prevIndent = indent; + } + } + return prop; +}; + +function getLineIndent(lineNum, lines){ + if(lineNum < lines.length){ + return lines[lineNum].search(/\S/); } - } - return prop; }; exports.identify = function(pathOrArgs, callback) {