Skip to content

Instantly share code, notes, and snippets.

@auramo
Created January 26, 2018 06:31
Show Gist options
  • Save auramo/a67fc9ea0cb39755eee9c7e8c932c999 to your computer and use it in GitHub Desktop.
Save auramo/a67fc9ea0cb39755eee9c7e8c932c999 to your computer and use it in GitHub Desktop.
promise recursion
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