diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-01-30 20:15:01 +0100 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-01-30 20:15:01 +0100 |
commit | af8b48b3400b5eac6ddb22b1efde19b10dac3183 (patch) | |
tree | 785d9d36740829f63a3bb6518556de4a95e2f3e9 /src/main/java/de/pixart/messenger/utils | |
parent | 7a67de94628dfe12b8790c333e5c5c19e647caab (diff) |
discover file extension in original filename from pgp
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/pixart/messenger/utils/MimeUtils.java | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/main/java/de/pixart/messenger/utils/MimeUtils.java b/src/main/java/de/pixart/messenger/utils/MimeUtils.java index bfcf71c6e..bf3b962ab 100644 --- a/src/main/java/de/pixart/messenger/utils/MimeUtils.java +++ b/src/main/java/de/pixart/messenger/utils/MimeUtils.java @@ -22,10 +22,13 @@ import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Properties; +import de.pixart.messenger.entities.Transferable; + /** * Utilities for dealing with MIME types. * Used to implement java.net.URLConnection and android.webkit.MimeTypeMap. @@ -529,4 +532,33 @@ public final class MimeUtils { } return mimeType; } + + public static String extractRelevantExtension(URL url) { + String path = url.getPath(); + return extractRelevantExtension(path, true); + } + + public static String extractRelevantExtension(final String path) { + return extractRelevantExtension(path, false); + } + + public static String extractRelevantExtension(final String path, final boolean ignoreCryptoExtension) { + if (path == null || path.isEmpty()) { + return null; + } + + String filename = path.substring(path.lastIndexOf('/') + 1).toLowerCase(); + int dotPosition = filename.lastIndexOf("."); + + if (dotPosition != -1) { + String extension = filename.substring(dotPosition + 1); + // we want the real file extension, not the crypto one + if (ignoreCryptoExtension && Transferable.VALID_CRYPTO_EXTENSIONS.contains(extension)) { + return extractRelevantExtension(filename.substring(0, dotPosition)); + } else { + return extension; + } + } + return null; + } } |