summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorByteHamster <info@bytehamster.com>2020-05-04 10:38:48 +0200
committerByteHamster <info@bytehamster.com>2020-05-04 10:38:48 +0200
commit00de47acdd9ccde41235abf3c1119b22ab03873c (patch)
treeeef3cea6faccda9a2e7f4b4c410e209bcf0be3cd
parentf5d289d776bcd21d54adf3e7d6e10479be61ddff (diff)
downloadAntennaPod-00de47acdd9ccde41235abf3c1119b22ab03873c.zip
Fixed adding feeds with long filename
-rw-r--r--app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java19
-rw-r--r--core/src/main/java/de/danoeh/antennapod/core/util/FileNameGenerator.java26
2 files changed, 44 insertions, 1 deletions
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 d33eb55b8..6f8042d61 100644
--- a/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java
+++ b/app/src/androidTest/java/de/test/antennapod/util/FilenameGeneratorTest.java
@@ -9,11 +9,13 @@ import java.io.File;
import java.io.IOException;
import de.danoeh.antennapod.core.util.FileNameGenerator;
+import org.apache.commons.lang3.StringUtils;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
@SmallTest
@@ -66,6 +68,23 @@ public class FilenameGeneratorTest {
assertFalse(TextUtils.isEmpty(result));
}
+ @Test
+ public void testLongFilename() throws IOException {
+ String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
+ String result = FileNameGenerator.generateFileName(longName);
+ assertTrue(result.length() <= FileNameGenerator.MAX_FILENAME_LENGTH);
+ createFiles(result);
+ }
+
+ @Test
+ public void testLongFilenameNotEquals() {
+ // Verify that the name is not just trimmed and different suffixes end up with the same name
+ String longName = StringUtils.repeat("x", 20 + FileNameGenerator.MAX_FILENAME_LENGTH);
+ String result1 = FileNameGenerator.generateFileName(longName + "a");
+ String result2 = FileNameGenerator.generateFileName(longName + "b");
+ assertNotEquals(result1, result2);
+ }
+
/**
* Tests if files can be created.
*
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 5579ec27d..220a783f3 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
@@ -2,11 +2,18 @@ package de.danoeh.antennapod.core.util;
import android.text.TextUtils;
+import androidx.annotation.VisibleForTesting;
import org.apache.commons.lang3.ArrayUtils;
+import java.io.UnsupportedEncodingException;
+import java.security.NoSuchAlgorithmException;
+
/** Generates valid filenames for a given string. */
public class FileNameGenerator {
+ @VisibleForTesting
+ public static final int MAX_FILENAME_LENGTH = 255; // Limited by ext4
+ private static final int MD5_HEX_LENGTH = 32;
private static final char[] validChars =
("abcdefghijklmnopqrstuvwxyz"
@@ -36,8 +43,11 @@ public class FileNameGenerator {
String filename = buf.toString().trim();
if (TextUtils.isEmpty(filename)) {
return randomString(8);
+ } else if (filename.length() >= MAX_FILENAME_LENGTH) {
+ return filename.substring(0, MAX_FILENAME_LENGTH - MD5_HEX_LENGTH - 1) + "_" + md5(filename);
+ } else {
+ return filename;
}
- return filename;
}
private static String randomString(int length) {
@@ -47,4 +57,18 @@ public class FileNameGenerator {
}
return sb.toString();
}
+
+ private static String md5(String md5) {
+ try {
+ java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
+ byte[] array = md.digest(md5.getBytes("UTF-8"));
+ StringBuilder sb = new StringBuilder();
+ for (byte b : array) {
+ sb.append(Integer.toHexString((b & 0xFF) | 0x100).substring(1, 3));
+ }
+ return sb.toString();
+ } catch (NoSuchAlgorithmException | UnsupportedEncodingException e) {
+ return null;
+ }
+ }
}