diff options
author | ByteHamster <info@bytehamster.com> | 2020-05-04 10:38:48 +0200 |
---|---|---|
committer | ByteHamster <info@bytehamster.com> | 2020-05-04 10:38:48 +0200 |
commit | 00de47acdd9ccde41235abf3c1119b22ab03873c (patch) | |
tree | eef3cea6faccda9a2e7f4b4c410e209bcf0be3cd /core/src/main/java | |
parent | f5d289d776bcd21d54adf3e7d6e10479be61ddff (diff) | |
download | AntennaPod-00de47acdd9ccde41235abf3c1119b22ab03873c.zip |
Fixed adding feeds with long filename
Diffstat (limited to 'core/src/main/java')
-rw-r--r-- | core/src/main/java/de/danoeh/antennapod/core/util/FileNameGenerator.java | 26 |
1 files changed, 25 insertions, 1 deletions
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; + } + } } |