From 1c0874dabedee1eac0174c6e99885be92843f161 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 31 Oct 2012 16:56:23 +0100 Subject: [PATCH 1/4] Created FileNameGenerator class --- .../antennapod/storage/DownloadRequester.java | 13 ++++--- .../antennapod/util/FileNameGenerator.java | 36 +++++++++++++++++++ .../antennapod/util/NumberGenerator.java | 21 ----------- 3 files changed, 44 insertions(+), 26 deletions(-) create mode 100644 src/de/danoeh/antennapod/util/FileNameGenerator.java delete mode 100644 src/de/danoeh/antennapod/util/NumberGenerator.java diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index 928b923fd..d977b4a3e 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -14,7 +14,7 @@ import de.danoeh.antennapod.feed.FeedFile; import de.danoeh.antennapod.feed.FeedImage; import de.danoeh.antennapod.feed.FeedMedia; import de.danoeh.antennapod.service.download.DownloadService; -import de.danoeh.antennapod.util.NumberGenerator; +import de.danoeh.antennapod.util.FileNameGenerator; import de.danoeh.antennapod.util.URLChecker; public class DownloadRequester { @@ -94,7 +94,8 @@ public class DownloadRequester { } } - public void downloadMedia(Context context, FeedMedia feedmedia) throws DownloadRequestException { + public void downloadMedia(Context context, FeedMedia feedmedia) + throws DownloadRequestException { if (feedFileValid(feedmedia)) { download(context, feedmedia, new File(getMediafilePath(context, feedmedia), @@ -199,7 +200,8 @@ public class DownloadRequester { } public String getFeedfileName(Feed feed) { - return "feed-" + NumberGenerator.generateLong(feed.getDownload_url()); + return "feed-" + + FileNameGenerator.generateFileName(feed.getDownload_url()); } public String getImagefilePath(Context context) @@ -209,7 +211,8 @@ public class DownloadRequester { } public String getImagefileName(FeedImage image) { - return "image-" + NumberGenerator.generateLong(image.getDownload_url()); + return "image-" + + FileNameGenerator.generateFileName(image.getDownload_url()); } public String getMediafilePath(Context context, FeedMedia media) @@ -217,7 +220,7 @@ public class DownloadRequester { File externalStorage = getExternalFilesDirOrThrowException( context, MEDIA_DOWNLOADPATH - + NumberGenerator.generateLong(media.getItem() + + FileNameGenerator.generateFileName(media.getItem() .getFeed().getTitle()) + "/"); return externalStorage.toString(); } diff --git a/src/de/danoeh/antennapod/util/FileNameGenerator.java b/src/de/danoeh/antennapod/util/FileNameGenerator.java new file mode 100644 index 000000000..3bc193080 --- /dev/null +++ b/src/de/danoeh/antennapod/util/FileNameGenerator.java @@ -0,0 +1,36 @@ +package de.danoeh.antennapod.util; + +import java.util.Arrays; + +/** Generates valid filenames for a given string. */ +public class FileNameGenerator { + + private static final char[] ILLEGAL_CHARACTERS = { '/', '\\', '?', '%', + '*', ':', '|', '"', '<', '>' }; + static { + Arrays.sort(ILLEGAL_CHARACTERS); + } + + private FileNameGenerator() { + + } + + /** + * This method will return a new string that doesn't contain any illegal + * characters of the given string. + */ + public static String generateFileName(String string) { + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < string.length(); i++) { + char c = string.charAt(i); + if (Arrays.binarySearch(ILLEGAL_CHARACTERS, c) < 0) { + builder.append(c); + } + } + return builder.toString(); + } + + public static long generateLong(final String str) { + return str.hashCode(); + } +} diff --git a/src/de/danoeh/antennapod/util/NumberGenerator.java b/src/de/danoeh/antennapod/util/NumberGenerator.java deleted file mode 100644 index ff89180e1..000000000 --- a/src/de/danoeh/antennapod/util/NumberGenerator.java +++ /dev/null @@ -1,21 +0,0 @@ -package de.danoeh.antennapod.util; - - -/**Utility class for creating numbers.*/ -public final class NumberGenerator { - /** Class shall not be instantiated.*/ - private NumberGenerator() { - } - - /**Logging tag.*/ - private static final String TAG = "NumberGenerator"; - - /** Takes a string and generates a random value out of - * the hash-value of that string. - * @param strSeed The string to take for the return value - * @return The generated random value - * */ - public static long generateLong(final String strSeed) { - return strSeed.hashCode(); - } -} From fe6460dde54ff93acd5d539386c6db44fada3f3a Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 31 Oct 2012 17:36:59 +0100 Subject: [PATCH 2/4] Use title of feed for filename if available --- .../danoeh/antennapod/storage/DownloadRequester.java | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index d977b4a3e..b32649345 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -200,8 +200,12 @@ public class DownloadRequester { } public String getFeedfileName(Feed feed) { + String filename = feed.getDownload_url(); + if (feed.getTitle() != null && !feed.getTitle().isEmpty()) { + filename = feed.getTitle(); + } return "feed-" - + FileNameGenerator.generateFileName(feed.getDownload_url()); + + FileNameGenerator.generateFileName(filename); } public String getImagefilePath(Context context) @@ -211,8 +215,12 @@ public class DownloadRequester { } public String getImagefileName(FeedImage image) { + String filename = image.getDownload_url(); + if (image.getFeed() != null && image.getFeed().getTitle() != null) { + filename = image.getFeed().getTitle(); + } return "image-" - + FileNameGenerator.generateFileName(image.getDownload_url()); + + FileNameGenerator.generateFileName(filename); } public String getMediafilePath(Context context, FeedMedia media) From 0f857e6a3b09d6c8cc4d04601c44e07215bca6a2 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Wed, 31 Oct 2012 18:03:47 +0100 Subject: [PATCH 3/4] Added option to take different filename if a file with that name exists --- .../antennapod/storage/DownloadRequester.java | 48 +++++++++++++++---- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/src/de/danoeh/antennapod/storage/DownloadRequester.java b/src/de/danoeh/antennapod/storage/DownloadRequester.java index b32649345..0201cc542 100644 --- a/src/de/danoeh/antennapod/storage/DownloadRequester.java +++ b/src/de/danoeh/antennapod/storage/DownloadRequester.java @@ -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) From a0eabe55559d60f36916579119f83dbabb134fc6 Mon Sep 17 00:00:00 2001 From: daniel oeh Date: Thu, 1 Nov 2012 19:16:32 +0100 Subject: [PATCH 4/4] Added test for FilenameGenerator --- .../test/FilenameGeneratorTest.java | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 tests/src/de/danoeh/antennapod/test/FilenameGeneratorTest.java diff --git a/tests/src/de/danoeh/antennapod/test/FilenameGeneratorTest.java b/tests/src/de/danoeh/antennapod/test/FilenameGeneratorTest.java new file mode 100644 index 000000000..90b1224a7 --- /dev/null +++ b/tests/src/de/danoeh/antennapod/test/FilenameGeneratorTest.java @@ -0,0 +1,48 @@ +package de.danoeh.antennapod.test; + +import java.io.File; +import java.io.IOException; + +import de.danoeh.antennapod.util.FileNameGenerator; +import android.test.AndroidTestCase; + +public class FilenameGeneratorTest extends AndroidTestCase { + + private static final String VALID1 = "abc abc"; + private static final String INVALID1 = "ab/c: