Skip to content

Commit c558dcd

Browse files
committed
fix: refine download cancellation logic to prevent accidental file deletion
- Update `AndroidDownloader` and `DesktopDownloader` to only delete local files if an active download was successfully cancelled. - Move file deletion logic inside the active download check to ensure only incomplete files associated with a current session are removed. - Simplify the `cancelDownload` return value to strictly reflect the cancellation status.
1 parent 00ae20d commit c558dcd

2 files changed

Lines changed: 16 additions & 13 deletions

File tree

core/data/src/androidMain/kotlin/zed/rainxch/core/data/services/AndroidDownloader.kt

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,6 @@ class AndroidDownloader(
210210
override suspend fun cancelDownload(fileName: String): Boolean =
211211
withContext(Dispatchers.IO) {
212212
var cancelled = false
213-
var deleted = false
214213

215214
val downloadId = activeFileNames[fileName]
216215
if (downloadId != null) {
@@ -222,13 +221,16 @@ class AndroidDownloader(
222221
activeDownloads.remove(downloadId)
223222
}
224223
activeFileNames.remove(fileName)
225-
}
226224

227-
val file = File(files.appDownloadsDir(), fileName)
228-
if (file.exists()) {
229-
deleted = file.delete()
225+
// Only delete the file if we cancelled an active download (incomplete file)
226+
if (cancelled) {
227+
val file = File(files.appDownloadsDir(), fileName)
228+
if (file.exists()) {
229+
file.delete()
230+
}
231+
}
230232
}
231233

232-
cancelled || deleted
234+
cancelled
233235
}
234236
}

core/data/src/jvmMain/kotlin/zed/rainxch/core/data/services/DesktopDownloader.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,6 @@ class DesktopDownloader(
197197
override suspend fun cancelDownload(fileName: String): Boolean =
198198
withContext(Dispatchers.IO) {
199199
var cancelled = false
200-
var deleted = false
201-
202200
val downloadId = nameToId[fileName]
203201
if (downloadId != null) {
204202
activeDownloads[downloadId]?.let { call ->
@@ -209,14 +207,17 @@ class DesktopDownloader(
209207
}
210208
activeDownloads.remove(downloadId)
211209
nameToId.remove(fileName)
212-
}
213210

214-
val file = File(files.userDownloadsDir(), fileName)
215-
if (file.exists()) {
216-
deleted = file.delete()
211+
// Only delete the file if we cancelled an active download (incomplete file)
212+
if (cancelled) {
213+
val file = File(files.userDownloadsDir(), fileName)
214+
if (file.exists()) {
215+
file.delete()
216+
}
217+
}
217218
}
218219

219-
cancelled || deleted
220+
cancelled
220221
}
221222

222223
companion object {

0 commit comments

Comments
 (0)