Skip to content

Instantly share code, notes, and snippets.

@Pringels
Created March 23, 2018 11:55
Show Gist options
  • Save Pringels/a37f0273b8b2943b22aa66adcc3017d0 to your computer and use it in GitHub Desktop.
Save Pringels/a37f0273b8b2943b22aa66adcc3017d0 to your computer and use it in GitHub Desktop.
Find and encode all Base64 strings to SVG files
var fs = require('fs');
var grepWithShell = function(files, outFile, done) {
var exec = require('child_process').exec;
var res = '';
exec('grep -oh "\\"data:image[^\\"]*\\"" ./**/*.* > b64_temp.txt', function(
err
) {
done(outFile);
});
};
function extractImages(file) {
var total = 0;
fs.readFile('b64_temp.txt', 'utf8', function(err, contents) {
var arr = contents.split('"');
var dir = __dirname + '/b64_to_SVG_images';
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}
arr.forEach((string, i) => {
if (string.length <= 1) {
return;
}
total += 1;
fs.writeFile(
`./b64_to_SVG_images/image_${i}.svg`,
new Buffer(string.replace('data:image/svg+xml;base64,', ''), 'base64'),
() => {}
);
});
console.log('Images saved: ', total);
});
fs.unlink('b64_temp.txt', err => {
if (err) throw err;
});
}
grepWithShell('./**/*.*', 'b64_temp.txt', extractImages);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment