mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-09-11 12:21:58 +02:00
Retry from beginning of file on failure and restore
This commit is contained in:
@@ -84,23 +84,17 @@ class DownloadItemManager(
|
|||||||
checkDownloadItemFinished(item)
|
checkDownloadItemFinished(item)
|
||||||
return@forEach
|
return@forEach
|
||||||
}
|
}
|
||||||
|
var resetFailed = false
|
||||||
item.downloadItemParts.forEach { part ->
|
item.downloadItemParts.forEach { part ->
|
||||||
if (part.moved) return@forEach
|
if (part.moved) return@forEach
|
||||||
if (item.terminalFailureAt != null) {
|
if (!resetPartForFreshDownload(part)) resetFailed = true
|
||||||
part.downloadId = null
|
|
||||||
part.isMoving = false
|
|
||||||
part.failed = true
|
|
||||||
part.waitingForSpace = false
|
|
||||||
part.bytesDownloaded = File(part.destinationPath).takeIf(File::exists)?.length() ?: 0L
|
|
||||||
return@forEach
|
|
||||||
}
|
}
|
||||||
part.downloadId = null
|
if (resetFailed) {
|
||||||
part.isMoving = false
|
item.terminalFailureAt = item.terminalFailureAt ?: System.currentTimeMillis()
|
||||||
part.failed = false
|
item.stagingCleanupAt = null
|
||||||
part.waitingForSpace = false
|
}
|
||||||
val stagingLength = File(part.destinationPath).takeIf(File::exists)?.length() ?: 0L
|
if (item.terminalFailureAt != null) {
|
||||||
part.bytesDownloaded = stagingLength
|
item.downloadItemParts.filter { !it.moved }.forEach { it.failed = true }
|
||||||
if (part.completed && stagingLength <= 0L) part.completed = false
|
|
||||||
}
|
}
|
||||||
downloadItemQueue.add(item)
|
downloadItemQueue.add(item)
|
||||||
if (item.terminalFailureAt != null) IncompleteDownloadCleanup.schedule(context, item)
|
if (item.terminalFailureAt != null) IncompleteDownloadCleanup.schedule(context, item)
|
||||||
@@ -127,19 +121,18 @@ class DownloadItemManager(
|
|||||||
if (item.downloadItemParts.any { it in currentDownloadItemParts }) return false
|
if (item.downloadItemParts.any { it in currentDownloadItemParts }) return false
|
||||||
if (item.isDownloadFinished) return false
|
if (item.isDownloadFinished) return false
|
||||||
synchronized(IncompleteDownloadCleanup) {
|
synchronized(IncompleteDownloadCleanup) {
|
||||||
|
var resetFailed = false
|
||||||
|
item.downloadItemParts.filter { !it.moved }.forEach { part ->
|
||||||
|
if (!resetPartForFreshDownload(part)) resetFailed = true
|
||||||
|
}
|
||||||
|
if (resetFailed) {
|
||||||
|
item.downloadItemParts.filter { !it.moved }.forEach { it.failed = true }
|
||||||
|
persist(item, force = true)
|
||||||
|
return false
|
||||||
|
}
|
||||||
item.terminalFailureAt = null
|
item.terminalFailureAt = null
|
||||||
item.stagingCleanupAt = null
|
item.stagingCleanupAt = null
|
||||||
IncompleteDownloadCleanup.cancel(context, item.id)
|
IncompleteDownloadCleanup.cancel(context, item.id)
|
||||||
item.downloadItemParts.filter { !it.moved }.forEach { part ->
|
|
||||||
part.failed = false
|
|
||||||
part.isMoving = false
|
|
||||||
part.downloadId = null
|
|
||||||
part.retryCount = 0
|
|
||||||
part.waitingForSpace = false
|
|
||||||
val stagingLength = File(part.destinationPath).takeIf(File::exists)?.length() ?: 0L
|
|
||||||
part.bytesDownloaded = stagingLength
|
|
||||||
part.completed = part.completed && stagingLength > 0L
|
|
||||||
}
|
|
||||||
persist(item, force = true)
|
persist(item, force = true)
|
||||||
}
|
}
|
||||||
clientEventEmitter.onDownloadItem(item)
|
clientEventEmitter.onDownloadItem(item)
|
||||||
@@ -562,6 +555,25 @@ class DownloadItemManager(
|
|||||||
return findSharedStorageFile(part) != null
|
return findSharedStorageFile(part) != null
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Resets an unmoved part when recovery crosses a service-session boundary. */
|
||||||
|
private fun resetPartForFreshDownload(part: DownloadItemPart): Boolean {
|
||||||
|
val stagingFile = File(part.destinationPath)
|
||||||
|
if (stagingFile.exists() && !stagingFile.delete()) {
|
||||||
|
Log.e(tag, "Could not delete staging file ${part.filename}")
|
||||||
|
part.failed = true
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
part.completed = false
|
||||||
|
part.bytesDownloaded = 0L
|
||||||
|
part.progress = 0L
|
||||||
|
part.failed = false
|
||||||
|
part.isMoving = false
|
||||||
|
part.downloadId = null
|
||||||
|
part.retryCount = 0
|
||||||
|
part.waitingForSpace = false
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
private fun findDocumentByFilename(folder: DocumentFile, part: DownloadItemPart): DocumentFile? {
|
private fun findDocumentByFilename(folder: DocumentFile, part: DownloadItemPart): DocumentFile? {
|
||||||
folder.findFile(part.filename)?.let { return it }
|
folder.findFile(part.filename)?.let { return it }
|
||||||
val expectedBaseName = part.filename.substringBeforeLast('.')
|
val expectedBaseName = part.filename.substringBeforeLast('.')
|
||||||
|
|||||||
Reference in New Issue
Block a user