Added option to take different filename if a file with that name exists

This commit is contained in:
daniel oeh 2012-10-31 18:03:47 +01:00
parent fe6460dde5
commit 0f857e6a3b
1 changed files with 38 additions and 10 deletions

View File

@ -4,6 +4,8 @@ import java.io.File;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.commons.io.FilenameUtils;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
@ -44,12 +46,40 @@ public class DownloadRequester {
return downloader;
}
private void download(Context context, FeedFile item, File dest) {
private void download(Context context, FeedFile item, File dest,
boolean overwriteIfExists) {
if (!isDownloadingFile(item)) {
if (dest.exists()) {
if (AppConfig.DEBUG)
Log.d(TAG, "File already exists. Deleting !");
dest.delete();
Log.d(TAG, "File already exists.");
if (overwriteIfExists) {
boolean result = dest.delete();
if (AppConfig.DEBUG)
Log.d(TAG, "Deleting file. Result: " + result);
} else {
// find different name
File newDest = null;
for (int i = 1; i < Integer.MAX_VALUE; i++) {
String newName = FilenameUtils.getBaseName(dest
.getName())
+ "-"
+ i
+ "."
+ FilenameUtils.getExtension(dest.getName());
if (AppConfig.DEBUG)
Log.d(TAG, "Testing filename " + newName);
newDest = new File(dest.getParent(), newName);
if (!newDest.exists()) {
if (AppConfig.DEBUG)
Log.d(TAG, "File doesn't exist yet. Using "
+ newName);
break;
}
}
if (newDest != null) {
dest = newDest;
}
}
}
if (AppConfig.DEBUG)
Log.d(TAG,
@ -82,7 +112,7 @@ public class DownloadRequester {
throws DownloadRequestException {
if (feedFileValid(feed)) {
download(context, feed, new File(getFeedfilePath(context),
getFeedfileName(feed)));
getFeedfileName(feed)), true);
}
}
@ -90,7 +120,7 @@ public class DownloadRequester {
throws DownloadRequestException {
if (feedFileValid(image)) {
download(context, image, new File(getImagefilePath(context),
getImagefileName(image)));
getImagefileName(image)), true);
}
}
@ -99,7 +129,7 @@ public class DownloadRequester {
if (feedFileValid(feedmedia)) {
download(context, feedmedia,
new File(getMediafilePath(context, feedmedia),
getMediafilename(feedmedia)));
getMediafilename(feedmedia)), false);
}
}
@ -204,8 +234,7 @@ public class DownloadRequester {
if (feed.getTitle() != null && !feed.getTitle().isEmpty()) {
filename = feed.getTitle();
}
return "feed-"
+ FileNameGenerator.generateFileName(filename);
return "feed-" + FileNameGenerator.generateFileName(filename);
}
public String getImagefilePath(Context context)
@ -219,8 +248,7 @@ public class DownloadRequester {
if (image.getFeed() != null && image.getFeed().getTitle() != null) {
filename = image.getFeed().getTitle();
}
return "image-"
+ FileNameGenerator.generateFileName(filename);
return "image-" + FileNameGenerator.generateFileName(filename);
}
public String getMediafilePath(Context context, FeedMedia media)