Merge pull request #2339 from ByteHamster/save-file-url

Setting File_url when starting download
This commit is contained in:
Martin Fietz 2018-01-04 20:05:59 +01:00 committed by GitHub
commit 204c97b8de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 18 deletions

View File

@ -424,6 +424,8 @@ public class DownloadService extends Service {
"ACTION_ENQUEUE_DOWNLOAD intent needs request extra"); "ACTION_ENQUEUE_DOWNLOAD intent needs request extra");
} }
writeFileUrl(request);
Downloader downloader = getDownloader(request); Downloader downloader = getDownloader(request);
if (downloader != null) { if (downloader != null) {
numberOfDownloads.incrementAndGet(); numberOfDownloads.incrementAndGet();
@ -905,6 +907,42 @@ public class DownloadService extends Service {
} }
/**
* Creates the destination file and writes FeedMedia File_url directly after starting download
* to make it possible to resume download after the service was killed by the system.
*/
private void writeFileUrl(DownloadRequest request) {
if (request.getFeedfileType() != FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
return;
}
File dest = new File(request.getDestination());
if (!dest.exists()) {
try {
dest.createNewFile();
} catch (IOException e) {
Log.e(TAG, "Unable to create file");
}
}
if (dest.exists()) {
Log.d(TAG, "Writing file url");
FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId());
if (media == null) {
Log.d(TAG, "No media");
return;
}
media.setFile_url(request.getDestination());
try {
DBWriter.setFeedMedia(media).get();
} catch (InterruptedException e) {
Log.e(TAG, "writeFileUrl was interrupted");
} catch (ExecutionException e) {
Log.e(TAG, "ExecutionException in writeFileUrl: " + e.getMessage());
}
}
}
/** /**
* Handles failed downloads. * Handles failed downloads.
* <p/> * <p/>
@ -929,23 +967,6 @@ public class DownloadService extends Service {
DBWriter.setFeedLastUpdateFailed(request.getFeedfileId(), true); DBWriter.setFeedLastUpdateFailed(request.getFeedfileId(), true);
} else if (request.isDeleteOnFailure()) { } else if (request.isDeleteOnFailure()) {
Log.d(TAG, "Ignoring failed download, deleteOnFailure=true"); Log.d(TAG, "Ignoring failed download, deleteOnFailure=true");
} else {
File dest = new File(request.getDestination());
if (dest.exists() && request.getFeedfileType() == FeedMedia.FEEDFILETYPE_FEEDMEDIA) {
Log.d(TAG, "File has been partially downloaded. Writing file url");
FeedMedia media = DBReader.getFeedMedia(request.getFeedfileId());
if (media == null) {
return;
}
media.setFile_url(request.getDestination());
try {
DBWriter.setFeedMedia(media).get();
} catch (InterruptedException e) {
Log.e(TAG, "FailedDownloadHandler was interrupted");
} catch (ExecutionException e) {
Log.e(TAG, "ExecutionException in FailedDownloadHandler: " + e.getMessage());
}
}
} }
} }
} }

View File

@ -11,8 +11,10 @@ import android.webkit.URLUtil;
import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.FilenameUtils;
import java.io.File; import java.io.File;
import java.io.IOException;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import de.danoeh.antennapod.core.BuildConfig; import de.danoeh.antennapod.core.BuildConfig;
import de.danoeh.antennapod.core.feed.Feed; import de.danoeh.antennapod.core.feed.Feed;
@ -89,7 +91,9 @@ public class DownloadRequester {
private void download(Context context, FeedFile item, FeedFile container, File dest, private void download(Context context, FeedFile item, FeedFile container, File dest,
boolean overwriteIfExists, String username, String password, boolean overwriteIfExists, String username, String password,
String lastModified, boolean deleteOnFailure, Bundle arguments) { String lastModified, boolean deleteOnFailure, Bundle arguments) {
final boolean partiallyDownloadedFileExists = item.getFile_url() != null; final boolean partiallyDownloadedFileExists = item.getFile_url() != null && new File(item.getFile_url()).exists();
Log.d(TAG, "partiallyDownloadedFileExists: " + partiallyDownloadedFileExists);
if (isDownloadingFile(item)) { if (isDownloadingFile(item)) {
Log.e(TAG, "URL " + item.getDownload_url() Log.e(TAG, "URL " + item.getDownload_url()
+ " is already being downloaded"); + " is already being downloaded");