desktop: catch unreadable crypto file (#3359)

This commit is contained in:
Stanislav Dmitrenko 2023-11-13 22:06:01 +08:00 committed by GitHub
parent 5beeff5cb6
commit 338417d963
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 41 additions and 9 deletions

View File

@ -35,7 +35,12 @@ actual fun shareFile(text: String, fileSource: CryptoFile) {
val tmpFile = File(tmpDir, fileSource.filePath) val tmpFile = File(tmpDir, fileSource.filePath)
tmpFile.deleteOnExit() tmpFile.deleteOnExit()
ChatModel.filesToDelete.add(tmpFile) ChatModel.filesToDelete.add(tmpFile)
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, tmpFile.absolutePath) try {
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
return
}
getAppFileUri(tmpFile.absolutePath) getAppFileUri(tmpFile.absolutePath)
} else { } else {
getAppFileUri(fileSource.filePath) getAppFileUri(fileSource.filePath)
@ -96,15 +101,21 @@ fun saveImage(ciFile: CIFile?) {
val outputStream = BufferedOutputStream(stream) val outputStream = BufferedOutputStream(stream)
if (ciFile.fileSource?.cryptoArgs != null) { if (ciFile.fileSource?.cryptoArgs != null) {
createTmpFileAndDelete { tmpFile -> createTmpFileAndDelete { tmpFile ->
decryptCryptoFile(filePath, ciFile.fileSource.cryptoArgs, tmpFile.absolutePath) try {
decryptCryptoFile(filePath, ciFile.fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
return@createTmpFileAndDelete
}
tmpFile.inputStream().use { it.copyTo(outputStream) } tmpFile.inputStream().use { it.copyTo(outputStream) }
showToast(generalGetString(MR.strings.image_saved))
} }
outputStream.close() outputStream.close()
} else { } else {
File(filePath).inputStream().use { it.copyTo(outputStream) } File(filePath).inputStream().use { it.copyTo(outputStream) }
outputStream.close() outputStream.close()
showToast(generalGetString(MR.strings.image_saved))
} }
showToast(generalGetString(MR.strings.image_saved))
} }
} }
} else { } else {

View File

@ -214,7 +214,13 @@ fun rememberSaveFileLauncher(ciFile: CIFile?): FileChooserLauncher =
if (filePath != null && to != null) { if (filePath != null && to != null) {
if (ciFile?.fileSource?.cryptoArgs != null) { if (ciFile?.fileSource?.cryptoArgs != null) {
createTmpFileAndDelete { tmpFile -> createTmpFileAndDelete { tmpFile ->
decryptCryptoFile(filePath, ciFile.fileSource.cryptoArgs, tmpFile.absolutePath) try {
decryptCryptoFile(filePath, ciFile.fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
tmpFile.delete()
return@createTmpFileAndDelete
}
copyFileToFile(tmpFile, to) {} copyFileToFile(tmpFile, to) {}
tmpFile.delete() tmpFile.delete()
} }

View File

@ -59,6 +59,7 @@ actual object AudioPlayer: AudioPlayerInterface {
} }
}.onFailure { }.onFailure {
Log.e(TAG, it.stackTraceToString()) Log.e(TAG, it.stackTraceToString())
fileSource.deleteTmpFile()
AlertManager.shared.showAlertMsg(generalGetString(MR.strings.unknown_error), it.message) AlertManager.shared.showAlertMsg(generalGetString(MR.strings.unknown_error), it.message)
return null return null
} }

View File

@ -27,7 +27,11 @@ actual fun shareFile(text: String, fileSource: CryptoFile) {
FileChooserLauncher(false) { to: URI? -> FileChooserLauncher(false) { to: URI? ->
if (to != null) { if (to != null) {
if (fileSource.cryptoArgs != null) { if (fileSource.cryptoArgs != null) {
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, to.path) try {
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, to.path)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
}
} else { } else {
copyFileToFile(File(fileSource.filePath), to) {} copyFileToFile(File(fileSource.filePath), to) {}
} }

View File

@ -48,7 +48,12 @@ actual fun copyItemToClipboard(cItem: ChatItem, clipboard: ClipboardManager) {
val filePath: String = if (fileSource.cryptoArgs != null) { val filePath: String = if (fileSource.cryptoArgs != null) {
val tmpFile = File(tmpDir, fileSource.filePath) val tmpFile = File(tmpDir, fileSource.filePath)
tmpFile.deleteOnExit() tmpFile.deleteOnExit()
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, tmpFile.absolutePath) try {
decryptCryptoFile(getAppFilePath(fileSource.filePath), fileSource.cryptoArgs, tmpFile.absolutePath)
} catch (e: Exception) {
Log.e(TAG, "Unable to decrypt crypto file: " + e.stackTraceToString())
return
}
tmpFile.absolutePath tmpFile.absolutePath
} else { } else {
getAppFilePath(fileSource.filePath) getAppFilePath(fileSource.filePath)

View File

@ -94,9 +94,14 @@ actual fun getAppFileUri(fileName: String): URI =
actual fun getLoadedImage(file: CIFile?): Pair<ImageBitmap, ByteArray>? { actual fun getLoadedImage(file: CIFile?): Pair<ImageBitmap, ByteArray>? {
val filePath = getLoadedFilePath(file) val filePath = getLoadedFilePath(file)
return if (filePath != null) { return if (filePath != null) {
val data = if (file?.fileSource?.cryptoArgs != null) readCryptoFile(filePath, file.fileSource.cryptoArgs) else File(filePath).readBytes() try {
val bitmap = getBitmapFromByteArray(data, false) val data = if (file?.fileSource?.cryptoArgs != null) readCryptoFile(filePath, file.fileSource.cryptoArgs) else File(filePath).readBytes()
if (bitmap != null) bitmap to data else null val bitmap = getBitmapFromByteArray(data, false)
if (bitmap != null) bitmap to data else null
} catch (e: Exception) {
Log.e(TAG, "Unable to read crypto file: " + e.stackTraceToString())
null
}
} else { } else {
null null
} }