From ae4af87c751b55f3fccb46696dd0a105ab3acc8e Mon Sep 17 00:00:00 2001 From: Gibran Chevalley Date: Wed, 23 Oct 2024 13:41:49 +0200 Subject: [PATCH] Refactor input and output stream when copying files locally --- .../ui/screen/newtransfer/UploadLocalStorage.kt | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/UploadLocalStorage.kt b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/UploadLocalStorage.kt index 737c2b3cf..4a48856ad 100644 --- a/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/UploadLocalStorage.kt +++ b/app/src/main/java/com/infomaniak/swisstransfer/ui/screen/newtransfer/UploadLocalStorage.kt @@ -51,19 +51,17 @@ class UploadLocalStorage @Inject constructor(@ApplicationContext private val app runCatching { createNewFile() }.onFailure { return null } runCatching { - val inputStream = appContext.contentResolver.openInputStream(uri) ?: run { + appContext.contentResolver.openInputStream(uri)?.use { inputStream -> + outputStream().use { outputStream -> + inputStream.copyTo(outputStream) + } + } ?: run { Sentry.withScope { scope -> scope.level = SentryLevel.ERROR Sentry.addBreadcrumb("During local copy of the file openInputStream returned null") } return null } - - inputStream.use { inputStream -> - outputStream().use { outputStream -> - inputStream.copyTo(outputStream) - } - } }.onFailure { Sentry.withScope { scope -> scope.level = SentryLevel.ERROR