Return random filename if sanitized filename would be empty

This commit is contained in:
Martin Fietz 2017-12-11 20:13:14 +01:00
parent 90cc07c9bb
commit dfa5066de7
2 changed files with 15 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import java.io.IOException;
import de.danoeh.antennapod.core.util.FileNameGenerator;
import android.test.AndroidTestCase;
import android.text.TextUtils;
public class FilenameGeneratorTest extends AndroidTestCase {
@ -41,7 +42,12 @@ public class FilenameGeneratorTest extends AndroidTestCase {
public void testFeedTitleContainsDash() {
String result = FileNameGenerator.generateFileName("Left - Right");
assertEquals("Left Right", result);
assertEquals("Left - Right", result);
}
public void testInvalidInput() {
String result = FileNameGenerator.generateFileName("???");
assertTrue(!TextUtils.isEmpty(result));
}
/**

View File

@ -1,10 +1,9 @@
package de.danoeh.antennapod.core.util;
import org.apache.commons.lang3.ArrayUtils;
import android.text.TextUtils;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.RandomStringUtils;
/** Generates valid filenames for a given string. */
public class FileNameGenerator {
@ -33,7 +32,11 @@ public class FileNameGenerator {
buf.append(c);
}
}
return buf.toString().trim();
String filename = buf.toString().trim();
if(TextUtils.isEmpty(filename)) {
return RandomStringUtils.randomAlphanumeric(8);
}
return filename;
}
}