Skip to content

Commit 4fea789

Browse files
authored
chore: use io dispatcher for file operations (WPB-21977) (#3768)
1 parent cde12d0 commit 4fea789

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

domain/cells/src/commonMain/kotlin/com/wire/kalium/cells/domain/usecase/RefreshCellAssetStateUseCase.kt

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ import com.wire.kalium.logic.data.asset.AssetTransferStatus
3737
import com.wire.kalium.logic.data.message.CellAssetContent
3838
import com.wire.kalium.logic.data.message.localPath
3939
import com.wire.kalium.network.exceptions.KaliumException
40+
import com.wire.kalium.util.KaliumDispatcher
41+
import com.wire.kalium.util.KaliumDispatcherImpl
4042
import io.ktor.http.HttpStatusCode
43+
import kotlinx.coroutines.withContext
4144
import okio.FileSystem
45+
import okio.Path
4246
import okio.Path.Companion.toPath
4347
import okio.SYSTEM
4448

@@ -57,6 +61,7 @@ internal class RefreshCellAssetStateUseCaseImpl internal constructor(
5761
private val cellsRepository: CellsRepository,
5862
private val attachmentsRepository: CellAttachmentsRepository,
5963
private val fileSystem: FileSystem = FileSystem.SYSTEM,
64+
private val dispatchers: KaliumDispatcher = KaliumDispatcherImpl,
6065
) : RefreshCellAssetStateUseCase {
6166

6267
private companion object {
@@ -113,7 +118,7 @@ internal class RefreshCellAssetStateUseCaseImpl internal constructor(
113118
attachmentsRepository.setAssetTransferStatus(assetId, AssetTransferStatus.NOT_FOUND)
114119
attachmentsRepository.getAttachment(assetId).map { attachment ->
115120
attachment.localPath()?.takeIf { it.isNotBlank() }?.let { localPath ->
116-
fileSystem.delete(localPath.toPath())
121+
deleteLocalFile(localPath.toPath())
117122
}
118123
}
119124
}
@@ -128,14 +133,14 @@ internal class RefreshCellAssetStateUseCaseImpl internal constructor(
128133

129134
// Check if asset was updated
130135
if (attachment.contentHash != node.contentHash && attachment.contentHash != null) {
131-
localPath?.let { fileSystem.delete(it.toPath()) }
136+
localPath?.let { deleteLocalFile(it.toPath()) }
132137
attachmentsRepository.saveLocalPath(attachment.id, null)
133138
localPath = null
134139
}
135140

136141
// Check if local file is still available
137-
localPath?.toPath()?.let {
138-
if (!fileSystem.exists(it)) {
142+
localPath?.toPath()?.let { path ->
143+
if (!localFileExists(path)) {
139144
attachmentsRepository.saveLocalPath(attachment.id, null)
140145
localPath = null
141146
}
@@ -160,6 +165,14 @@ internal class RefreshCellAssetStateUseCaseImpl internal constructor(
160165
}
161166
}
162167
}
168+
169+
private suspend fun deleteLocalFile(path: Path) = withContext(dispatchers.io) {
170+
fileSystem.delete(path)
171+
}
172+
173+
private suspend fun localFileExists(path: Path) = withContext(dispatchers.io) {
174+
fileSystem.exists(path)
175+
}
163176
}
164177

165178
@Suppress("ReturnCount")

0 commit comments

Comments
 (0)