diff --git a/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadItemManager.kt b/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadItemManager.kt index 8df4090a..fefbaed4 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadItemManager.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/managers/DownloadItemManager.kt @@ -121,6 +121,30 @@ class DownloadItemManager( } } + // Check if file already exists in shared storage + if (!downloadItemPart.isInternalStorage) { + val destinationFile = + DocumentFileCompat.fromFullPath(mainActivity, downloadItemPart.finalDestinationPath) + if (destinationFile != null && destinationFile.exists()) { + Log.d( + tag, + "File already exists in shared storage at path ${downloadItemPart.finalDestinationPath}" + ) + downloadItemPart.completed = true + downloadItemPart.moved = true + downloadItemPart.failed = false + downloadItemPart.downloadId = 1 + currentDownloadItemParts.add(downloadItemPart) + clientEventEmitter.onDownloadItemPartUpdate(downloadItemPart) + return + } else { + Log.d( + tag, + "File does not exist in shared storage at path ${downloadItemPart.finalDestinationPath}" + ) + } + } + Log.d( tag, "Start internal download to destination path ${downloadItemPart.finalDestinationPath} from ${downloadItemPart.serverUrl}" @@ -163,8 +187,9 @@ class DownloadItemManager( if (downloadItemPart.completed) { val downloadItem = downloadItemQueue.find { it.id == downloadItemPart.downloadItemId } - if (!downloadItemPart.isInternalStorage) { - // After downloading, move the downloaded file to the final destination + if (!downloadItemPart.isInternalStorage && !downloadItemPart.moved) { + // After downloading, move the downloaded file to the final destination if it was + // not already moved during a previous multipart download // After moving the file, checkDownloadItemFinished is called anyway downloadItem?.let { moveDownloadedFile(it, downloadItemPart) } } else { diff --git a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt index b1345cba..624e3ab2 100644 --- a/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt +++ b/android/app/src/main/java/com/audiobookshelf/app/plugins/AbsDownloader.kt @@ -265,6 +265,10 @@ class AbsDownloader : Plugin() { val finalDestinationFile = File("$itemFolderPath/$destinationFilename") val destinationFile = File("$tempFolderPath/$destinationFilename") + /* + // This logic would delete files if they exist. This has been removed to allow for continuing + // downloads if the app is restarted. This should be handled in the download manager + // allow a setting to continue downloads or delete incomplete downloads if (destinationFile.exists()) { Log.d( tag, @@ -280,6 +284,7 @@ class AbsDownloader : Plugin() { ) finalDestinationFile.delete() } + */ val downloadItemPart = DownloadItemPart.make(