fix(app/download_processor): detect deleted files

This commit is contained in:
rhunk
2023-12-10 11:28:05 +01:00
parent 9583d67bf1
commit 368d70226d
2 changed files with 18 additions and 0 deletions

View File

@@ -341,6 +341,16 @@ class DownloadProcessor (
if (task.status.isFinalStage()) {
if (task.status != TaskStatus.SUCCESS) return@let
// check if the media file has been deleted
if (task.type == TaskType.DOWNLOAD) {
val outputFile = runCatching {
DocumentFile.fromTreeUri(remoteSideContext.androidContext, Uri.parse(task.extra))
}.getOrNull()
if (outputFile != null && !outputFile.exists()) {
return@let
}
}
callbackOnFailure(translation["already_downloaded_toast"], null)
} else {
callbackOnFailure(translation["already_queued_toast"], null)

View File

@@ -50,6 +50,13 @@ class TaskManager(
return runBlocking {
suspendCoroutine {
queueExecutor.execute {
taskDatabase.rawQuery("SELECT * FROM tasks WHERE hash = ?", arrayOf(task.hash)).use { cursor ->
if (cursor.moveToNext()) {
it.resumeWith(Result.success(cursor.getLong("id")))
return@execute
}
}
val result = taskDatabase.insert("tasks", null, ContentValues().apply {
put("type", task.type.key)
put("hash", task.hash)
@@ -57,6 +64,7 @@ class TaskManager(
put("status", task.status.key)
put("extra", task.extra)
})
it.resumeWith(Result.success(result))
}
}