Created
March 23, 2018 11:55
-
-
Save Pringels/a37f0273b8b2943b22aa66adcc3017d0 to your computer and use it in GitHub Desktop.
Find and encode all Base64 strings to SVG files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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