diff options
Diffstat (limited to 'src/main/java/eu/siacs')
4 files changed, 41 insertions, 36 deletions
diff --git a/src/main/java/eu/siacs/conversations/entities/Message.java b/src/main/java/eu/siacs/conversations/entities/Message.java index c377ed4a..12127ecc 100644 --- a/src/main/java/eu/siacs/conversations/entities/Message.java +++ b/src/main/java/eu/siacs/conversations/entities/Message.java @@ -7,6 +7,7 @@ import java.net.MalformedURLException; import java.net.URL; import eu.siacs.conversations.crypto.axolotl.XmppAxolotlSession; +import eu.siacs.conversations.utils.FileUtils; import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.xmpp.jid.InvalidJidException; import eu.siacs.conversations.xmpp.jid.Jid; @@ -480,16 +481,12 @@ public class Message extends AbstractEntity { String filename = path.substring(path.lastIndexOf('/') + 1).toLowerCase(); - final int lastDotPosition = filename.lastIndexOf('.'); - final int secondToLastDotPosition = filename.lastIndexOf('.', lastDotPosition - 1); - final String lastPart = lastDotPosition != -1 ? - filename.substring(lastDotPosition + 1) : ""; - final String secondToLastPart = secondToLastDotPosition != -1 ? - filename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; + final String lastPart = FileUtils.getLastExtension(filename); - if (lastDotPosition != -1) { + if (!lastPart.isEmpty()) { // we want the real file extension, not the crypto one - if (secondToLastDotPosition != -1 && Transferable.VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { + final String secondToLastPart = FileUtils.getSecondToLastExtension(filename); + if (!secondToLastPart.isEmpty() && Transferable.VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { return secondToLastPart; } else { return lastPart; diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index c8076811..6d8024ee 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -36,6 +36,7 @@ import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.XmppConnectionService; import eu.siacs.conversations.utils.CryptoHelper; +import eu.siacs.conversations.utils.FileUtils; public class HttpDownloadConnection implements Transferable { @@ -83,12 +84,7 @@ public class HttpDownloadConnection implements Transferable { mUrl = new URL(message.getBody()); } final String sUrlFilename = mUrl.getPath().substring(mUrl.getPath().lastIndexOf('/')).toLowerCase(); - final int lastDotPosition = sUrlFilename.lastIndexOf('.'); - final int secondToLastDotPosition = sUrlFilename.lastIndexOf('.', lastDotPosition - 1); - final String lastPart = lastDotPosition != -1 ? - sUrlFilename.substring(lastDotPosition + 1) : ""; - final String secondToLastPart = secondToLastDotPosition != -1 ? - sUrlFilename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; + final String lastPart = FileUtils.getLastExtension(sUrlFilename); if (!lastPart.isEmpty() && ("pgp".equals(lastPart) || "gpg".equals(lastPart))) { this.message.setEncryption(Message.ENCRYPTION_PGP); @@ -98,7 +94,7 @@ public class HttpDownloadConnection implements Transferable { } String extension; if (!lastPart.isEmpty() && VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { - extension = secondToLastPart; + extension = FileUtils.getSecondToLastExtension(sUrlFilename); } else { extension = lastPart; } diff --git a/src/main/java/eu/siacs/conversations/utils/FileUtils.java b/src/main/java/eu/siacs/conversations/utils/FileUtils.java index c5d0c3b0..727891ae 100644 --- a/src/main/java/eu/siacs/conversations/utils/FileUtils.java +++ b/src/main/java/eu/siacs/conversations/utils/FileUtils.java @@ -147,4 +147,27 @@ public class FileUtils { public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } + + /** + * @param filename The filename to extract extension from + * @return last extension or empty string + */ + public static String getLastExtension(final String filename) { + final int lastDotPosition = filename.lastIndexOf('.'); + final String lastPart = lastDotPosition != -1 ? + filename.substring(lastDotPosition + 1) : ""; + return lastPart; + } + + /** + * @param filename The filename to extract extension from + * @return second to last extension or empty string + */ + public static String getSecondToLastExtension(final String filename) { + final int lastDotPosition = filename.lastIndexOf('.'); + final int secondToLastDotPosition = filename.lastIndexOf('.', lastDotPosition - 1); + final String secondToLastPart = secondToLastDotPosition != -1 ? + filename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; + return secondToLastPart; + } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java index 147d53e2..8185c8e1 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java @@ -32,6 +32,7 @@ import eu.siacs.conversations.entities.TransferablePlaceholder; import eu.siacs.conversations.persistance.FileBackend; import eu.siacs.conversations.services.AbstractConnectionManager; import eu.siacs.conversations.services.XmppConnectionService; +import eu.siacs.conversations.utils.FileUtils; import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xmpp.OnIqPacketReceived; import eu.siacs.conversations.xmpp.jid.Jid; @@ -325,23 +326,21 @@ public class JingleConnection implements Transferable { if (fileNameElement != null) { String filename = fileNameElement.getContent() .toLowerCase(Locale.US).toLowerCase(); - final int lastDotPosition = filename.lastIndexOf('.'); - final int secondToLastDotPosition = filename.lastIndexOf('.', lastDotPosition - 1); - final String lastPart = lastDotPosition != -1 ? - filename.substring(lastDotPosition + 1) : ""; - final String secondToLastPart = secondToLastDotPosition != -1 ? - filename.substring(secondToLastDotPosition + 1, lastDotPosition - 1) : ""; - if (lastDotPosition != -1) { + final String lastPart = FileUtils.getLastExtension(filename); + final String secondToLastPart = FileUtils.getSecondToLastExtension(filename); + if (!lastPart.isEmpty()) { if (VALID_IMAGE_EXTENSIONS.contains(lastPart)) { message.setType(Message.TYPE_IMAGE); message.setRelativeFilePath(message.getUuid()+"."+lastPart); } else if (VALID_CRYPTO_EXTENSIONS.contains(lastPart)) { - if (secondToLastDotPosition != -1) { + if (!secondToLastPart.isEmpty()) { if (VALID_IMAGE_EXTENSIONS.contains(secondToLastPart)) { message.setType(Message.TYPE_IMAGE); message.setRelativeFilePath(message.getUuid()+"."+secondToLastPart); } else { message.setType(Message.TYPE_FILE); + message.setRelativeFilePath(message.getUuid() + "_" + + filename.substring(0, filename.length() - (secondToLastPart.length() + 1))); } if (lastPart.equals("otr")) { message.setEncryption(Message.ENCRYPTION_OTR); @@ -351,23 +350,13 @@ public class JingleConnection implements Transferable { } } else { message.setType(Message.TYPE_FILE); + message.setRelativeFilePath(message.getUuid() + "_" + filename); } } else { message.setType(Message.TYPE_FILE); + message.setRelativeFilePath(message.getUuid() + "_" + filename); } - if (message.getType() == Message.TYPE_FILE) { - String suffix = ""; - if (!fileNameElement.getContent().isEmpty()) { - String parts[] = fileNameElement.getContent().split("/"); - suffix = parts[parts.length - 1]; - if (message.getEncryption() == Message.ENCRYPTION_OTR && suffix.endsWith(".otr")) { - suffix = suffix.substring(0,suffix.length() - 4); - } else if (message.getEncryption() == Message.ENCRYPTION_PGP && (suffix.endsWith(".pgp") || suffix.endsWith(".gpg"))) { - suffix = suffix.substring(0,suffix.length() - 4); - } - } - message.setRelativeFilePath(message.getUuid()+"_"+suffix); - } + long size = Long.parseLong(fileSize.getContent()); message.setBody(Long.toString(size)); conversation.add(message); |