Created
January 26, 2018 06:31
-
-
Save auramo/a67fc9ea0cb39755eee9c7e8c932c999 to your computer and use it in GitHub Desktop.
promise recursion
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
const password = 'kuikka' | |
const keyIv = cryptoUtils.deriveAES256KeyAndIv(password.replace(/\s/g, '')) | |
const maxFileSize = 2 * 1024 * 1024 * 1024 | |
const isMeb = fileName => /\.meb$/.test(fileName) | |
const isZip = fileName => isMeb(fileName) || /\.zip$/.test(fileName) | |
const isEncryptedZip = fileName => /\.zip\.bin$/.test(fileName) | |
const isEncryptedJson = fileName => /\.json\.bin$/.test(fileName) | |
const readZipContents = (zipFileName, zipContents) => { | |
const handleFileEntry = ([name, data]) => { | |
if (isZip(name)) { | |
return zipUtils.extractZip(data, maxFileSize) | |
.then(zipContents => readZipContents(name, zipContents)) | |
.then(zipContentResult => [name, zipContentResult]) | |
} else if (isEncryptedZip(name)) { | |
return cryptoUtils.decryptAES256Async(data, keyIv.key, keyIv.iv) | |
.then(decryptedData => zipUtils.extractZip(decryptedData, maxFileSize)) | |
.then(zipContents => readZipContents(name, zipContents)) | |
.then(zipContentResult => [name, zipContentResult]) | |
} else if (isEncryptedJson(name)) { | |
return cryptoUtils.decryptAES256Async(data, keyIv.key, keyIv.iv) | |
.then(jsonData => [name, jsonData]) | |
} else { | |
return Promise.resolve([name, data]) | |
} | |
} | |
const fileEntries = R.toPairs(zipContents) | |
return BPromise.map(fileEntries, handleFileEntry) | |
.then(results => R.fromPairs(results)) | |
} | |
const readZipFile = fileName => { | |
console.log('readZipFile', fileName) | |
return fs.readFileAsync(fileName) | |
.then(dataBuffer => zipUtils.extractZip(dataBuffer, maxFileSize)) | |
.then(zipContents => readZipContents(fileName, zipContents)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment