Skip to content

Commit

Permalink
Updated secure-archive with Arrow FX
Browse files Browse the repository at this point in the history
  • Loading branch information
vihangpatil committed Nov 30, 2019
1 parent ef87c09 commit 654a814
Showing 1 changed file with 24 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package org.ostelco.prime.securearchive

import arrow.core.Either
import arrow.core.fix
import arrow.effects.IO
import arrow.instances.either.monad.monad
import arrow.core.extensions.fx
import org.ostelco.prime.getLogger
import org.ostelco.prime.securearchive.ConfigRegistry.config
import org.ostelco.prime.securearchive.util.Encrypter.Companion.getEncrypter
Expand All @@ -23,31 +21,29 @@ class SecureArchiver : SecureArchiveService {
fileName: String,
dataMap: Map<String, ByteArray>): Either<StoreError, Unit> {

return IO {
Either.monad<StoreError>().binding {
val bucketName = config.storageBucket
logger.info("Generating Plain Zip data for customerId = {}", customerId)
val plainZipData = generateZipFile(fileName, dataMap).bind()
(regionCodes
.filter(config.regions::contains)
+ "global")
.map(String::toLowerCase)
.forEach { regionCode ->
logger.info("Encrypt for region: {} for customerId = {}", regionCode, customerId)
val zipData = getEncrypter(regionCode).encrypt(plainZipData)
if (bucketName.isEmpty()) {
val filePath = "${regionCode}_$fileName.zip.tk"
logger.info("No bucket set, saving file locally {}", filePath)
saveLocalFile(filePath, zipData).bind()
} else {
val filePath = "$customerId/${fileName}_${Instant.now()}.zip.tk"
val bucket = "$bucketName-$regionCode"
logger.info("Saving in cloud storage {} --> {}", bucket, filePath)
uploadFileToCloudStorage(bucket, filePath, zipData).bind()
}
return Either.fx {
val bucketName = config.storageBucket
logger.info("Generating Plain Zip data for customerId = {}", customerId)
val plainZipData = generateZipFile(fileName, dataMap).bind()
(regionCodes
.filter(config.regions::contains)
+ "global")
.map(String::toLowerCase)
.forEach { regionCode ->
logger.info("Encrypt for region: {} for customerId = {}", regionCode, customerId)
val zipData = getEncrypter(regionCode).encrypt(plainZipData)
if (bucketName.isEmpty()) {
val filePath = "${regionCode}_$fileName.zip.tk"
logger.info("No bucket set, saving file locally {}", filePath)
saveLocalFile(filePath, zipData).bind()
} else {
val filePath = "$customerId/${fileName}_${Instant.now()}.zip.tk"
val bucket = "$bucketName-$regionCode"
logger.info("Saving in cloud storage {} --> {}", bucket, filePath)
uploadFileToCloudStorage(bucket, filePath, zipData).bind()
}
Unit
}.fix()
}.unsafeRunSync()
}
Unit
}
}
}

0 comments on commit 654a814

Please sign in to comment.