diff --git a/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java b/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java index 6d24fa526..13e8b9582 100644 --- a/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java +++ b/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java @@ -34,6 +34,16 @@ public class FilenameGeneratorTest extends AndroidTestCase { createFiles(result); } + public void testFeedTitleContainsApostrophe() { + String result = FileNameGenerator.generateFileName("Feed's Title ..."); + assertEquals("Feeds Title", result); + } + + public void testFeedTitleContainsDash() { + String result = FileNameGenerator.generateFileName("Left - Right"); + assertEquals("Left Right", result); + } + /** * Tests if files can be created. * diff --git a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java index 04afc504b..7051d7f4d 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java +++ b/core/src/main/java/de/danoeh/antennapod/core/storage/DownloadRequester.java @@ -345,9 +345,7 @@ public class DownloadRequester { // Try to generate the filename by the item title if (media.getItem() != null && media.getItem().getTitle() != null) { String title = media.getItem().getTitle(); - // Delete reserved characters - titleBaseFilename = title.replaceAll("[^a-zA-Z0-9 ._()-]", ""); - titleBaseFilename = titleBaseFilename.trim(); + titleBaseFilename = FileNameGenerator.generateFileName(title); } String URLBaseFilename = URLUtil.guessFileName(media.getDownload_url(), diff --git a/core/src/main/java/de/danoeh/antennapod/core/util/FileNameGenerator.java b/core/src/main/java/de/danoeh/antennapod/core/util/FileNameGenerator.java index 30c2e62f2..8da176c82 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/util/FileNameGenerator.java +++ b/core/src/main/java/de/danoeh/antennapod/core/util/FileNameGenerator.java @@ -1,18 +1,21 @@ package de.danoeh.antennapod.core.util; +import org.apache.commons.lang3.ArrayUtils; + import java.util.Arrays; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** Generates valid filenames for a given string. */ public class FileNameGenerator { - private static final char[] ILLEGAL_CHARACTERS = { '/', '\\', '?', '%', - '*', ':', '|', '"', '<', '>', '\n' }; - static { - Arrays.sort(ILLEGAL_CHARACTERS); - } + private static final char[] validChars = ( + "abcdefghijklmnopqrstuvwxyz" + + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "0123456789" + + " _-").toCharArray(); private FileNameGenerator() { - } /** @@ -20,17 +23,17 @@ public class FileNameGenerator { * characters of the given string. */ public static String generateFileName(String string) { - StringBuilder builder = new StringBuilder(); + StringBuilder buf = 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); + if(Character.isSpaceChar(c) && (buf.length() == 0 || Character.isSpaceChar(buf.charAt(buf.length()-1)))) { + continue; + } + if (ArrayUtils.contains(validChars, c)) { + buf.append(c); } } - return builder.toString().replaceFirst(" *$",""); + return buf.toString().trim(); } - public static long generateLong(final String str) { - return str.hashCode(); - } }