Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Catch excecption on Asset uploading [WPB-10700] #3024

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading