From ac03680f9ecccc3daa7299f904470f8e6140623d Mon Sep 17 00:00:00 2001 From: narcoticfresh Date: Sat, 17 Jan 2015 18:00:47 +0100 Subject: [PATCH 1/3] refactor exiftool to use the exiftool -json output method, makes everything leaner and cleaner. also all tags are returned now - return value structure is the same as before.. --- lib/exiftool.js | 58 ++++++++++++++++++++++++------------------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/exiftool.js b/lib/exiftool.js index 6a2e04c..509bf7b 100644 --- a/lib/exiftool.js +++ b/lib/exiftool.js @@ -9,15 +9,21 @@ exports.metadata = function (source, tags, doneGettingMetadata) { } // The dash specifies to read data from stdin. - var args = (tags === [] ? ['-'] : tags.push("-")); - var exif = ChildProcess.spawn('exiftool', tags); + var args = []; + if (typeof(tags) == 'object' && tags.length > 0) { + args = tags; + } + + args.push('-json'); + args.push('-'); + + var exif = ChildProcess.spawn('exiftool', args); //Check for error because of the child process not being found / launched. exif.on('error', function (err) { doneGettingMetadata('Fatal Error: Unable to load exiftool. ' + err); }); - // Read the binary data back var response = ''; var errorMessage = ''; exif.stdout.on("data", function (data) { @@ -37,35 +43,29 @@ exports.metadata = function (source, tags, doneGettingMetadata) { } else { - // Split the response into lines. - response = response.split("\n"); + try { + + var jsonResponse = JSON.parse(response); - //For each line of the response extract the meta data into a nice associative array - var metaData = []; - response.forEach(function (responseLine) { - var pieces = responseLine.split(": "); - //Is this a line with a meta data pair on it? - if (pieces.length == 2) - { - //Turn the plain text data key into a camel case key. - var key = pieces[0].trim().split(' ').map( - function (tokenInKey, tokenNumber) { - if (tokenNumber === 0) - return tokenInKey.toLowerCase(); - else - return tokenInKey[0].toUpperCase() + tokenInKey.slice(1); - } - ).join(''); - //Trim the value associated with the key to make it nice. - var value = pieces[1].trim(); - if (!isNaN(value)) - { - value = parseFloat(value, 10); + /** + * exiftool outputs a strange thing: an array with 1 object inside.. + * convert it to a simple array as that is what this component returned before.. + */ + + if (typeof(jsonResponse[0]) == 'object') { + var metaData = []; + for (i in jsonResponse[0]) { + metaData[i] = jsonResponse[0][i]; } - metaData[key] = value; + doneGettingMetadata(null, metaData); + } else { + throw new Error('trigger'); } - }); - doneGettingMetadata(null, metaData); + + } catch (Exception) { + doneGettingMetadata('Fatal Error: Unable to parse exiftool output.'); + } + } }); From b712b8d4cf3e0f4117e75450019521807781b4e5 Mon Sep 17 00:00:00 2001 From: narcoticfresh Date: Sat, 17 Jan 2015 21:42:57 +0100 Subject: [PATCH 2/3] fix field names; make it camelcase again.. --- lib/exiftool.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/exiftool.js b/lib/exiftool.js index 509bf7b..58985a8 100644 --- a/lib/exiftool.js +++ b/lib/exiftool.js @@ -55,7 +55,11 @@ exports.metadata = function (source, tags, doneGettingMetadata) { if (typeof(jsonResponse[0]) == 'object') { var metaData = []; for (i in jsonResponse[0]) { - metaData[i] = jsonResponse[0][i]; + // make it camelcase.. + if (i.length > 1) { + propName = i.substr(0, 1).toLowerCase() + i.substr(1); + metaData[propName] = jsonResponse[0][i]; + } } doneGettingMetadata(null, metaData); } else { From 09717fed92168787c0fe1132423a4c6d1d176de1 Mon Sep 17 00:00:00 2001 From: narcoticfresh Date: Sat, 17 Jan 2015 23:31:58 +0100 Subject: [PATCH 3/3] some better error handling; don't wrap call of cb with try/catch.. --- lib/exiftool.js | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/lib/exiftool.js b/lib/exiftool.js index 58985a8..8108a47 100644 --- a/lib/exiftool.js +++ b/lib/exiftool.js @@ -37,37 +37,32 @@ exports.metadata = function (source, tags, doneGettingMetadata) { // Handle the response to the callback to hand the metadata back. exif.on("close", function () { - if (errorMessage) - { + if (errorMessage) { doneGettingMetadata(errorMessage); } - else - { + else { try { - var jsonResponse = JSON.parse(response); + } catch (e) { + doneGettingMetadata('Fatal Error: Unable to parse exiftool output.'); + } - /** - * exiftool outputs a strange thing: an array with 1 object inside.. - * convert it to a simple array as that is what this component returned before.. - */ - - if (typeof(jsonResponse[0]) == 'object') { - var metaData = []; - for (i in jsonResponse[0]) { - // make it camelcase.. - if (i.length > 1) { - propName = i.substr(0, 1).toLowerCase() + i.substr(1); - metaData[propName] = jsonResponse[0][i]; - } + /** + * exiftool outputs a strange thing: an array with 1 object inside.. + * convert it to a simple array as that is what this component returned before.. + */ + if (typeof(jsonResponse[0]) == 'object') { + var metaData = []; + for (i in jsonResponse[0]) { + // make it camelcase.. + if (i.length > 1) { + propName = i.substr(0, 1).toLowerCase() + i.substr(1); + metaData[propName] = jsonResponse[0][i]; } - doneGettingMetadata(null, metaData); - } else { - throw new Error('trigger'); } - - } catch (Exception) { - doneGettingMetadata('Fatal Error: Unable to parse exiftool output.'); + doneGettingMetadata(null, metaData); + } else { + doneGettingMetadata('exiftool returned a wrong format. Did you override the format with arguments?'); } }