Skip to content

Commit

Permalink
fix: Catch excecption on Asset uploading [WPB-10700] (#3024) (#3030)
Browse files Browse the repository at this point in the history
* fix: Catch excecption on Asset uploading

* Review fixes

Co-authored-by: boris <[email protected]>
Co-authored-by: Vitor Hugo Schwaab <[email protected]>
  • Loading branch information
3 people authored Sep 25, 2024
1 parent ee5615f commit 6282928
Showing 1 changed file with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,29 +156,33 @@ internal class AssetDataSource(
otrKey: AES256Key,
extension: String?
): Either<CoreFailure, Pair<UploadedAssetId, SHA256Key>> {

val tempEncryptedDataPath = kaliumFileSystem.tempFilePath("${assetDataPath.name}.aes")
val assetDataSource = kaliumFileSystem.source(assetDataPath)
val assetDataSink = kaliumFileSystem.sink(tempEncryptedDataPath)

// Encrypt the data on the provided temp path
val encryptedDataSize = encryptFileWithAES256(assetDataSource, otrKey, assetDataSink)
val encryptedDataSource = kaliumFileSystem.source(tempEncryptedDataPath)

// Calculate the SHA of the encrypted data
val sha256 = calcFileSHA256(encryptedDataSource)
assetDataSink.close()
encryptedDataSource.close()
assetDataSource.close()

val encryptionSucceeded = (encryptedDataSize > 0L && sha256 != null)

return if (encryptionSucceeded) {
val uploadAssetData = UploadAssetData(tempEncryptedDataPath, encryptedDataSize, mimeType, false, RetentionType.PERSISTENT)
uploadAndPersistAsset(uploadAssetData, assetDataPath, extension).map { it to SHA256Key(sha256!!) }
} else {
kaliumLogger.e("Something went wrong when encrypting the Asset Message")
Either.Left(EncryptionFailure.GenericEncryptionError)
try {
val tempEncryptedDataPath = kaliumFileSystem.tempFilePath("${assetDataPath.name}.aes")
val assetDataSource = kaliumFileSystem.source(assetDataPath)
val assetDataSink = kaliumFileSystem.sink(tempEncryptedDataPath)

// Encrypt the data on the provided temp path
val encryptedDataSize = encryptFileWithAES256(assetDataSource, otrKey, assetDataSink)
val encryptedDataSource = kaliumFileSystem.source(tempEncryptedDataPath)

// Calculate the SHA of the encrypted data
val sha256 = calcFileSHA256(encryptedDataSource)
assetDataSink.close()
encryptedDataSource.close()
assetDataSource.close()

val encryptionSucceeded = (encryptedDataSize > 0L && sha256 != null)

return if (encryptionSucceeded) {
val uploadAssetData = UploadAssetData(tempEncryptedDataPath, encryptedDataSize, mimeType, false, RetentionType.PERSISTENT)
uploadAndPersistAsset(uploadAssetData, assetDataPath, extension).map { it to SHA256Key(sha256!!) }
} else {
kaliumLogger.e("Something went wrong when encrypting the Asset Message")
Either.Left(EncryptionFailure.GenericEncryptionError)
}
} catch (e: IOException) {
kaliumLogger.e("Something went wrong when uploading the Asset Message. $e")
return Either.Left(CoreFailure.Unknown(e))
}
}

Expand Down

0 comments on commit 6282928

Please sign in to comment.