android, desktop: alert for local encryption/decryption errors (#4081)

* android, desktop: more alert in case of local encryption/decryption errors

* sending a message without a file
This commit is contained in:
Stanislav Dmitrenko
2024-04-24 20:39:06 +07:00
committed by GitHub
parent 5ba7b58d7d
commit 68966a1551
10 changed files with 62 additions and 13 deletions
@@ -46,6 +46,7 @@ actual fun shareFile(text: String, fileSource: CryptoFile) {
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
return
}
getAppFileUri(tmpFile.absolutePath)
@@ -112,6 +113,7 @@ fun saveImage(ciFile: CIFile?) {
decryptCryptoFile(filePath, ciFile.fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
return@createTmpFileAndDelete
}
tmpFile.inputStream().use { it.copyTo(outputStream) }
@@ -27,6 +27,7 @@ import androidx.core.text.HtmlCompat
import chat.simplex.common.helpers.*
import chat.simplex.common.model.*
import chat.simplex.common.platform.*
import chat.simplex.res.MR
import dev.icerock.moko.resources.StringResource
import java.io.*
import java.net.URI
@@ -187,7 +188,13 @@ actual suspend fun getLoadedImage(file: CIFile?): Pair<ImageBitmap, ByteArray>?
return if (filePath != null && file != null) {
try {
val data = if (file.fileSource?.cryptoArgs != null) {
readCryptoFile(getAppFilePath(file.fileSource.filePath), file.fileSource.cryptoArgs)
try {
readCryptoFile(getAppFilePath(file.fileSource.filePath), file.fileSource.cryptoArgs)
} catch (e: Exception) {
Log.e(TAG, "Unable to read crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
return null
}
} else {
File(getAppFilePath(file.fileName)).readBytes()
}
@@ -2696,7 +2696,12 @@ data class CryptoFile(
private fun decryptToTmpFile(): URI? {
val absoluteFilePath = if (isAbsolutePath) filePath else getAppFilePath(filePath)
val tmpFile = createTmpFileIfNeeded()
decryptCryptoFile(absoluteFilePath, cryptoArgs ?: return null, tmpFile.absolutePath)
try {
decryptCryptoFile(absoluteFilePath, cryptoArgs ?: return null, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
}
return tmpFile.toURI()
}
@@ -534,23 +534,34 @@ fun ComposeView(
AudioPlayer.stop(tmpFile.absolutePath)
if (remoteHost == null) {
val actualFile = File(getAppFilePath(tmpFile.name.replaceAfter(RecorderInterface.extension, "")))
files.add(withContext(Dispatchers.IO) {
val file = withContext(Dispatchers.IO) {
if (chatController.appPrefs.privacyEncryptLocalFiles.get()) {
val args = encryptCryptoFile(tmpFile.absolutePath, actualFile.absolutePath)
tmpFile.delete()
val args = try {
encryptCryptoFile(tmpFile.absolutePath, actualFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to encrypt plain file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
return@withContext null
} finally {
tmpFile.delete()
}
CryptoFile(actualFile.name, args)
} else {
Files.move(tmpFile.toPath(), actualFile.toPath())
CryptoFile.plain(actualFile.name)
}
})
}
if (file != null) {
files.add(file)
msgs.add(MsgContent.MCVoice(if (msgs.isEmpty()) msgText else "", preview.durationMs / 1000))
}
deleteUnusedFiles()
} else {
files.add(CryptoFile.plain(tmpFile.absolutePath))
// It will be deleted on JVM shutdown or next start (if the app crashes unexpectedly)
filesToDelete.remove(tmpFile)
msgs.add(MsgContent.MCVoice(if (msgs.isEmpty()) msgText else "", preview.durationMs / 1000))
}
msgs.add(MsgContent.MCVoice(if (msgs.isEmpty()) msgText else "", preview.durationMs / 1000))
}
is ComposePreview.FilePreview -> {
val file = if (remoteHost == null) {
@@ -222,6 +222,7 @@ fun rememberSaveFileLauncher(ciFile: CIFile?): FileChooserLauncher =
decryptCryptoFile(filePath, ciFile.fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
tmpFile.delete()
return@createTmpFileAndDelete
}
@@ -16,6 +16,7 @@ import chat.simplex.common.model.CryptoFile
import chat.simplex.common.platform.*
import chat.simplex.common.views.chat.ProviderMedia
import chat.simplex.common.views.helpers.*
import chat.simplex.res.MR
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.launch
import java.net.URI
@@ -169,8 +169,14 @@ fun saveImage(image: ImageBitmap): CryptoFile? {
val destFileName = generateNewFileName("IMG", ext, File(getAppFilePath("")))
val destFile = File(getAppFilePath(destFileName))
if (encrypted) {
val args = writeCryptoFile(destFile.absolutePath, dataResized.toByteArray())
CryptoFile(destFileName, args)
try {
val args = writeCryptoFile(destFile.absolutePath, dataResized.toByteArray())
CryptoFile(destFileName, args)
} catch (e: Exception) {
Log.e(TAG, "Unable to write crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
null
}
} else {
val output = FileOutputStream(destFile)
dataResized.writeTo(output)
@@ -216,8 +222,14 @@ fun saveAnimImage(uri: URI): CryptoFile? {
val destFileName = generateNewFileName("IMG", ext, File(getAppFilePath("")))
val destFile = File(getAppFilePath(destFileName))
if (encrypted) {
val args = writeCryptoFile(destFile.absolutePath, uri.inputStream()?.readBytes() ?: return null)
CryptoFile(destFileName, args)
try {
val args = writeCryptoFile(destFile.absolutePath, uri.inputStream()?.readBytes() ?: return null)
CryptoFile(destFileName, args)
} catch (e: Exception) {
Log.e(TAG, "Unable to read crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
null
}
} else {
Files.copy(uri.inputStream(), destFile.toPath())
CryptoFile.plain(destFileName)
@@ -241,8 +253,14 @@ fun saveFileFromUri(uri: URI, withAlertOnException: Boolean = true): CryptoFile?
if (encrypted) {
createTmpFileAndDelete { tmpFile ->
Files.copy(inputStream, tmpFile.toPath())
val args = encryptCryptoFile(tmpFile.absolutePath, destFile.absolutePath)
CryptoFile(destFileName, args)
try {
val args = encryptCryptoFile(tmpFile.absolutePath, destFile.absolutePath)
CryptoFile(destFileName, args)
} catch (e: Exception) {
Log.e(TAG, "Unable to encrypt plain file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
null
}
}
} else {
Files.copy(inputStream, destFile.toPath())
@@ -32,6 +32,7 @@ actual fun shareFile(text: String, fileSource: CryptoFile) {
showToast(generalGetString(MR.strings.file_saved))
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
}
} else {
copyFileToFile(File(absolutePath), to) {}
@@ -66,6 +66,7 @@ actual fun copyItemToClipboard(cItem: ChatItem, clipboard: ClipboardManager) = w
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs ?: return@withLongRunningApi, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
return@withLongRunningApi
}
tmpFile.absolutePath
@@ -12,6 +12,7 @@ import chat.simplex.common.model.CIFile
import chat.simplex.common.model.readCryptoFile
import chat.simplex.common.platform.*
import chat.simplex.common.simplexWindowState
import chat.simplex.res.MR
import kotlinx.coroutines.delay
import java.io.ByteArrayInputStream
import java.io.File
@@ -142,6 +143,7 @@ actual suspend fun getLoadedImage(file: CIFile?): Pair<ImageBitmap, ByteArray>?
if (bitmap != null) bitmap to data else null
} catch (e: Exception) {
Log.e(TAG, "Unable to read crypto file: " + e.stackTraceToString())
AlertManager.shared.showAlertMsg(title = generalGetString(MR.strings.error), text = e.stackTraceToString())
null
}
} else {