Skip to content

Commit 97b49aa

Browse files
authored
fix: crash when downloading file (WPB-19465) (#4189)
1 parent 4674ab0 commit 97b49aa

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

features/cells/src/main/java/com/wire/android/feature/cells/ui/CellViewModel.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,22 +187,20 @@ class CellViewModel @Inject constructor(
187187

188188
private fun downloadNode(node: CellNodeUi) = viewModelScope.launch {
189189

190-
if (node.name.isNullOrBlank()) {
191-
sendAction(ShowError(CellError.OTHER_ERROR))
192-
return@launch
193-
}
194-
195190
val (nodeName, nodeRemotePath) = when (node) {
196191
is CellNodeUi.File -> Pair(node.name, node.remotePath)
197192
is CellNodeUi.Folder -> Pair(node.name + ZIP_EXTENSION, node.remotePath + ZIP_EXTENSION)
198193
}
199194

195+
if (nodeName.isNullOrBlank()) {
196+
sendAction(ShowError(CellError.OTHER_ERROR))
197+
return@launch
198+
}
199+
200200
val publicDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
201201
val filePath = File(publicDir, nodeName).toPath().toOkioPath()
202202

203-
if (kaliumFileSystem.exists(filePath)) {
204-
kaliumFileSystem.delete(filePath)
205-
}
203+
deleteIfExists(filePath)
206204

207205
download(
208206
assetId = node.uuid,
@@ -225,6 +223,12 @@ class CellViewModel @Inject constructor(
225223
}
226224
}
227225

226+
private fun deleteIfExists(filePath: Path) = runCatching {
227+
if (kaliumFileSystem.exists(filePath)) {
228+
kaliumFileSystem.delete(filePath)
229+
}
230+
}
231+
228232
private fun updateDownloadProgress(progress: Long, it: Long, node: CellNodeUi, path: Path) = viewModelScope.launch {
229233

230234
val value = progress.toFloat() / it

0 commit comments

Comments
 (0)