Merge pull request #4110 from ByteHamster/fix-long-filename
Fixed adding feeds with long filename
This commit is contained in:
commit
8c77f54b37
@ -9,11 +9,13 @@ import java.io.File;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
||||||
import de.danoeh.antennapod.core.util.FileNameGenerator;
|
import de.danoeh.antennapod.core.util.FileNameGenerator;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertNotEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
@SmallTest
|
@SmallTest
|
||||||
@ -66,6 +68,23 @@ public class FilenameGeneratorTest {
|
|||||||
assertFalse(TextUtils.isEmpty(result));
|
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.
|
* Tests if files can be created.
|
||||||
*
|
*
|
||||||
|
@ -2,11 +2,18 @@ package de.danoeh.antennapod.core.util;
|
|||||||
|
|
||||||
import android.text.TextUtils;
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
import androidx.annotation.VisibleForTesting;
|
||||||
import org.apache.commons.lang3.ArrayUtils;
|
import org.apache.commons.lang3.ArrayUtils;
|
||||||
|
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
|
||||||
/** Generates valid filenames for a given string. */
|
/** Generates valid filenames for a given string. */
|
||||||
public class FileNameGenerator {
|
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 =
|
private static final char[] validChars =
|
||||||
("abcdefghijklmnopqrstuvwxyz"
|
("abcdefghijklmnopqrstuvwxyz"
|
||||||
@ -36,8 +43,11 @@ public class FileNameGenerator {
|
|||||||
String filename = buf.toString().trim();
|
String filename = buf.toString().trim();
|
||||||
if (TextUtils.isEmpty(filename)) {
|
if (TextUtils.isEmpty(filename)) {
|
||||||
return randomString(8);
|
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) {
|
private static String randomString(int length) {
|
||||||
@ -47,4 +57,18 @@ public class FileNameGenerator {
|
|||||||
}
|
}
|
||||||
return sb.toString();
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user