Skip to content

Added support for node streams to image magick #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 41 additions & 32 deletions imagemagick.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -140,8 +147,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;
Expand All @@ -165,7 +172,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();
Expand Down