Skip to content

Commit

Permalink
Fixed issue #48
Browse files Browse the repository at this point in the history
  • Loading branch information
danieloeh committed Dec 16, 2012
1 parent 3692119 commit 256cfd2
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions src/de/danoeh/antennapod/storage/DownloadRequester.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public static DownloadRequester getInstance() {
private void download(Context context, FeedFile item, File dest,
boolean overwriteIfExists) {
if (!isDownloadingFile(item)) {
if (dest.exists()) {
if (!isFilenameAvailable(dest.toString()) || dest.exists()) {
if (AppConfig.DEBUG)
Log.d(TAG, "File already exists.");
if (overwriteIfExists) {
Log.d(TAG, "Filename already used.");
if (isFilenameAvailable(dest.toString()) && overwriteIfExists) {
boolean result = dest.delete();
if (AppConfig.DEBUG)
Log.d(TAG, "Deleting file. Result: " + result);
Expand All @@ -69,7 +69,7 @@ private void download(Context context, FeedFile item, File dest,
if (AppConfig.DEBUG)
Log.d(TAG, "Testing filename " + newName);
newDest = new File(dest.getParent(), newName);
if (!newDest.exists()) {
if (!newDest.exists() && isFilenameAvailable(newDest.toString())) {
if (AppConfig.DEBUG)
Log.d(TAG, "File doesn't exist yet. Using "
+ newName);
Expand Down Expand Up @@ -108,6 +108,24 @@ private void download(Context context, FeedFile item, File dest,
}
}

/**
* Returns true if a filename is available and false if it has already been
* taken by another requested download.
*/
private boolean isFilenameAvailable(String path) {
for (String key : downloads.keySet()) {
FeedFile f = downloads.get(key);
if (f.getFile_url().equals(path)) {
if (AppConfig.DEBUG)
Log.d(TAG, path
+ " is already used by another requested download");
return false;
}
}
if (AppConfig.DEBUG) Log.d(TAG, path + " is available as a download destination");
return true;
}

public void downloadFeed(Context context, Feed feed)
throws DownloadRequestException {
if (feedFileValid(feed)) {
Expand Down

0 comments on commit 256cfd2

Please sign in to comment.