From 8d1eb62f0bf3c5014a632acbbf98f06d07cf666e Mon Sep 17 00:00:00 2001 From: ByteHamster Date: Sat, 15 Jul 2023 16:27:12 +0200 Subject: Delete partially downloaded file when giving up to retry (#6530) --- .../service/download/DownloadRequestCreator.java | 2 -- .../service/download/EpisodeDownloadWorker.java | 18 ++++++++++++--- .../core/service/download/HttpDownloader.java | 26 ---------------------- 3 files changed, 15 insertions(+), 31 deletions(-) (limited to 'core') diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequestCreator.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequestCreator.java index 5ca904ff6..4d5f2883d 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequestCreator.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/DownloadRequestCreator.java @@ -28,7 +28,6 @@ public class DownloadRequestCreator { return new DownloadRequest.Builder(dest.toString(), feed) .withAuthentication(username, password) - .deleteOnFailure(true) .lastModified(feed.getLastUpdate()); } @@ -53,7 +52,6 @@ public class DownloadRequestCreator { ? media.getItem().getFeed().getPreferences().getPassword() : null; return new DownloadRequest.Builder(dest.toString(), media) - .deleteOnFailure(false) .withAuthentication(username, password); } diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java index c428bc861..62956894c 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/EpisodeDownloadWorker.java @@ -134,6 +134,7 @@ public class EpisodeDownloadWorker extends Worker { } else { sendErrorNotification(); } + FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination())); return Result.failure(); } @@ -162,6 +163,9 @@ public class EpisodeDownloadWorker extends Worker { Log.d(TAG, "Requested invalid range, restarting download from the beginning"); FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination())); sendMessage(request.getTitle(), true); + if (isLastRunAttempt()) { + FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination())); + } return retry3times(); } @@ -177,21 +181,29 @@ public class EpisodeDownloadWorker extends Worker { } else { sendErrorNotification(); } + FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination())); return Result.failure(); } sendMessage(request.getTitle(), true); + if (isLastRunAttempt()) { + FileUtils.deleteQuietly(new File(downloader.getDownloadRequest().getDestination())); + } return retry3times(); } private Result retry3times() { - if (getRunAttemptCount() < 2) { - return Result.retry(); - } else { + if (isLastRunAttempt()) { sendErrorNotification(); return Result.failure(); + } else { + return Result.retry(); } } + private boolean isLastRunAttempt() { + return getRunAttemptCount() >= 2; + } + private void sendMessage(String episodeTitle, boolean retrying) { if (episodeTitle.length() > 20) { episodeTitle = episodeTitle.substring(0, 19) + "…"; diff --git a/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java b/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java index 6de5e7006..94cd81337 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java +++ b/core/src/main/java/de/danoeh/antennapod/core/service/download/HttpDownloader.java @@ -49,12 +49,6 @@ public class HttpDownloader extends Downloader { File destination = new File(request.getDestination()); final boolean fileExists = destination.exists(); - if (request.isDeleteOnFailure() && fileExists) { - Log.w(TAG, "File already exists"); - onSuccess(); - return; - } - RandomAccessFile out = null; InputStream connection; ResponseBody responseBody = null; @@ -309,31 +303,11 @@ public class HttpDownloader extends Downloader { private void onFail(DownloadError reason, String reasonDetailed) { Log.d(TAG, "onFail() called with: " + "reason = [" + reason + "], reasonDetailed = [" + reasonDetailed + "]"); result.setFailed(reason, reasonDetailed); - if (request.isDeleteOnFailure()) { - cleanup(); - } } private void onCancelled() { Log.d(TAG, "Download was cancelled"); result.setCancelled(); cancelled = true; - cleanup(); - } - - /** - * Deletes unfinished downloads. - */ - private void cleanup() { - if (request.getDestination() != null) { - File dest = new File(request.getDestination()); - if (dest.exists()) { - boolean rc = dest.delete(); - Log.d(TAG, "Deleted file " + dest.getName() + "; Result: " - + rc); - } else { - Log.d(TAG, "cleanup() didn't delete file: does not exist."); - } - } } } -- cgit v1.2.3