Skip to content

Instantly share code, notes, and snippets.

@unusualbob
Created April 8, 2017 04:37
Show Gist options
  • Save unusualbob/f4551beb2f1e2a7e9015c83ebd307321 to your computer and use it in GitHub Desktop.
Save unusualbob/f4551beb2f1e2a7e9015c83ebd307321 to your computer and use it in GitHub Desktop.
Script to extract images from hex photo recall export file
'use strict';
var fs = require('fs');
var stringHex = fs.createReadStream('./myOutputFile.txt');
var lastIndex = 0;
var imageBuffer = new Buffer('');
var dirname = process.argv[2];
if (!dirname) {
return console.log('Usage: ./imageExtractor.js <localDirectoryToExportImagesTo>');
}
if (!fs.existsSync('./' + dirname)) {
fs.mkdirSync('./' + dirname);
}
var rowChunk = '';
var leftOverChunk = '';
stringHex.on('data', function (chunk) {
stringHex.pause();
console.log('chunk');
rowChunk += chunk.toString();
if (rowChunk.indexOf('\n') === -1) {
return stringHex.resume();
}
else {
var rows = rowChunk.toString().split('\n');
leftOverChunk = rows[rows.length - 1];
rows.pop();
rows.forEach(function (row) {
row = row.split('|||');
if (row.length !== 7) {
return;
}
var filename = row[0];
var data = row[4];
console.log('file', filename);
fs.appendFileSync('./' + dirname + '/img-' + filename + '.jpg', new Buffer(data, 'hex'), {encoding: 'binary'});
});
rowChunk = leftOverChunk;
stringHex.resume();
}
});
stringHex.on('end', function () {
var row = rowChunk.split('\n')[0];
row = row.split('|||');
if (row.length !== 7) {
return;
}
var filename = row[0];
var data = row[4];
fs.appendFileSync('./' + dirname + '/img-' + filename + '.jpg', new Buffer(data, 'hex'), {encoding: 'binary'});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment