mirror of
https://github.com/bitwarden/android.git
synced 2026-06-04 11:46:51 -05:00
Catch exception caused by trying to process large files on devices with low memory (#1101)
This commit is contained in:
committed by
Álison Fernandes
parent
b8ed52d587
commit
8cedd8ed33
@@ -35,7 +35,7 @@ interface FileManager {
|
||||
suspend fun stringToUri(fileUri: Uri, dataString: String): Boolean
|
||||
|
||||
/**
|
||||
* Reads the [fileUri] into memory and returns the raw [ByteArray]
|
||||
* Reads the [fileUri] into memory. A successful result will contain the raw [ByteArray].
|
||||
*/
|
||||
suspend fun uriToByteArray(fileUri: Uri): ByteArray
|
||||
suspend fun uriToByteArray(fileUri: Uri): Result<ByteArray>
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ import com.x8bit.bitwarden.data.platform.manager.dispatcher.DispatcherManager
|
||||
import com.x8bit.bitwarden.data.vault.datasource.network.service.DownloadService
|
||||
import com.x8bit.bitwarden.data.vault.manager.model.DownloadResult
|
||||
import kotlinx.coroutines.withContext
|
||||
import okio.use
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.File
|
||||
import java.io.FileInputStream
|
||||
@@ -113,21 +112,23 @@ class FileManagerImpl(
|
||||
}
|
||||
}
|
||||
|
||||
override suspend fun uriToByteArray(fileUri: Uri): ByteArray =
|
||||
withContext(dispatcherManager.io) {
|
||||
context
|
||||
.contentResolver
|
||||
.openInputStream(fileUri)
|
||||
?.use { inputStream ->
|
||||
ByteArrayOutputStream().use { outputStream ->
|
||||
val buffer = ByteArray(BUFFER_SIZE)
|
||||
var length: Int
|
||||
while (inputStream.read(buffer).also { length = it } != -1) {
|
||||
outputStream.write(buffer, 0, length)
|
||||
override suspend fun uriToByteArray(fileUri: Uri): Result<ByteArray> =
|
||||
runCatching {
|
||||
withContext(dispatcherManager.io) {
|
||||
context
|
||||
.contentResolver
|
||||
.openInputStream(fileUri)
|
||||
?.use { inputStream ->
|
||||
ByteArrayOutputStream().use { outputStream ->
|
||||
val buffer = ByteArray(BUFFER_SIZE)
|
||||
var length: Int
|
||||
while (inputStream.read(buffer).also { length = it } != -1) {
|
||||
outputStream.write(buffer, 0, length)
|
||||
}
|
||||
outputStream.toByteArray()
|
||||
}
|
||||
outputStream.toByteArray()
|
||||
}
|
||||
}
|
||||
?: byteArrayOf()
|
||||
?: throw IllegalStateException("Stream has crashed")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -868,12 +868,16 @@ class VaultRepositoryImpl(
|
||||
cipherView = cipherView,
|
||||
)
|
||||
.flatMap { cipher ->
|
||||
vaultSdkSource.encryptAttachment(
|
||||
userId = userId,
|
||||
cipher = cipher,
|
||||
attachmentView = attachmentView,
|
||||
fileBuffer = fileManager.uriToByteArray(fileUri = fileUri),
|
||||
)
|
||||
fileManager
|
||||
.uriToByteArray(fileUri = fileUri)
|
||||
.flatMap {
|
||||
vaultSdkSource.encryptAttachment(
|
||||
userId = userId,
|
||||
cipher = cipher,
|
||||
attachmentView = attachmentView,
|
||||
fileBuffer = it,
|
||||
)
|
||||
}
|
||||
}
|
||||
.flatMap { attachmentEncryptResult ->
|
||||
ciphersService
|
||||
@@ -990,12 +994,16 @@ class VaultRepositoryImpl(
|
||||
"File URI must be present to create a File Send.",
|
||||
)
|
||||
.asFailure()
|
||||
vaultSdkSource
|
||||
.encryptBuffer(
|
||||
userId = userId,
|
||||
send = send,
|
||||
fileBuffer = fileManager.uriToByteArray(fileUri = uri),
|
||||
)
|
||||
|
||||
fileManager
|
||||
.uriToByteArray(fileUri = uri)
|
||||
.flatMap {
|
||||
vaultSdkSource.encryptBuffer(
|
||||
userId = userId,
|
||||
send = send,
|
||||
fileBuffer = it,
|
||||
)
|
||||
}
|
||||
.flatMap { encryptedFile ->
|
||||
sendsService
|
||||
.createFileSend(
|
||||
|
||||
Reference in New Issue
Block a user