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 | |
parent | 7a67de94628dfe12b8790c333e5c5c19e647caab (diff) |
discover file extension in original filename from pgp
Diffstat (limited to '')
5 files changed, 74 insertions, 44 deletions
diff --git a/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java b/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java index f96e45082..f1c3e277c 100644 --- a/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java +++ b/src/main/java/de/pixart/messenger/crypto/PgpDecryptionService.java @@ -2,7 +2,9 @@ package de.pixart.messenger.crypto; import android.app.PendingIntent; import android.content.Intent; +import android.util.Log; +import org.openintents.openpgp.OpenPgpMetadata; import org.openintents.openpgp.util.OpenPgpApi; import java.io.ByteArrayInputStream; @@ -17,11 +19,13 @@ import java.util.ArrayDeque; import java.util.HashSet; import java.util.List; +import de.pixart.messenger.Config; import de.pixart.messenger.entities.Conversation; import de.pixart.messenger.entities.DownloadableFile; import de.pixart.messenger.entities.Message; import de.pixart.messenger.http.HttpConnectionManager; import de.pixart.messenger.services.XmppConnectionService; +import de.pixart.messenger.utils.MimeUtils; public class PgpDecryptionService { @@ -176,13 +180,31 @@ public class PgpDecryptionService { try { final DownloadableFile inputFile = mXmppConnectionService.getFileBackend().getFile(message, false); final DownloadableFile outputFile = mXmppConnectionService.getFileBackend().getFile(message, true); - outputFile.getParentFile().mkdirs(); + if (outputFile.getParentFile().mkdirs()) { + Log.d(Config.LOGTAG, "created parent directories for " + outputFile.getAbsolutePath()); + } outputFile.createNewFile(); InputStream is = new FileInputStream(inputFile); OutputStream os = new FileOutputStream(outputFile); Intent result = getOpenPgpApi().executeApi(params, is, os); switch (result.getIntExtra(OpenPgpApi.RESULT_CODE, OpenPgpApi.RESULT_CODE_ERROR)) { case OpenPgpApi.RESULT_CODE_SUCCESS: + OpenPgpMetadata openPgpMetadata = result.getParcelableExtra(OpenPgpApi.RESULT_METADATA); + String originalFilename = openPgpMetadata.getFilename(); + String originalExtension = originalFilename == null ? null : MimeUtils.extractRelevantExtension(originalFilename); + if (originalExtension != null && MimeUtils.extractRelevantExtension(outputFile.getName()) == null) { + Log.d(Config.LOGTAG, "detected original filename during pgp decryption"); + String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension); + String path = outputFile.getName() + "." + originalExtension; + DownloadableFile fixedFile = mXmppConnectionService.getFileBackend().getFileForPath(path, mime); + if (fixedFile.getParentFile().mkdirs()) { + Log.d(Config.LOGTAG, "created parent directories for " + fixedFile.getAbsolutePath()); + } + if (outputFile.renameTo(fixedFile)) { + Log.d(Config.LOGTAG, "renamed " + outputFile.getAbsolutePath() + " to " + fixedFile.getAbsolutePath()); + message.setRelativeFilePath(path); + } + } URL url = message.getFileParams().url; mXmppConnectionService.getFileBackend().updateFileParams(message, url); message.setEncryption(Message.ENCRYPTION_DECRYPTED); diff --git a/src/main/java/de/pixart/messenger/entities/Message.java b/src/main/java/de/pixart/messenger/entities/Message.java index 1e087b69b..9f4bcade6 100644 --- a/src/main/java/de/pixart/messenger/entities/Message.java +++ b/src/main/java/de/pixart/messenger/entities/Message.java @@ -684,46 +684,19 @@ public class Message extends AbstractEntity { this.oob = isOob; } - private static String extractRelevantExtension(URL url) { - String path = url.getPath(); - return extractRelevantExtension(path); - } - - private static String extractRelevantExtension(String path) { - 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 (Transferable.VALID_CRYPTO_EXTENSIONS.contains(extension)) { - return extractRelevantExtension(filename.substring(0, dotPosition)); - } else { - return extension; - } - } - return null; - } - public String getMimeType() { + String extension; if (relativeFilePath != null) { - int start = relativeFilePath.lastIndexOf('.') + 1; - if (start < relativeFilePath.length()) { - return MimeUtils.guessMimeTypeFromExtension(relativeFilePath.substring(start)); - } else { - return null; - } + extension = MimeUtils.extractRelevantExtension(relativeFilePath); } else { try { - return MimeUtils.guessMimeTypeFromExtension(extractRelevantExtension(new URL(body.trim()))); + final URL url = new URL(body.split("\n")[0]); + extension = MimeUtils.extractRelevantExtension(url); } catch (MalformedURLException e) { return null; } } + return MimeUtils.guessMimeTypeFromExtension(extension); } public synchronized boolean treatAsDownloadable() { diff --git a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java index d8fdfb79e..9985cf929 100644 --- a/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java +++ b/src/main/java/de/pixart/messenger/http/HttpDownloadConnection.java @@ -95,7 +95,7 @@ public class HttpDownloadConnection implements Transferable { extension = lastPart; } String filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4); - message.setRelativeFilePath(filename + "." + extension); + message.setRelativeFilePath(filename + (extension != null ? ("." + extension) : "")); this.file = mXmppConnectionService.getFileBackend().getFile(message, false); final String reference = mUrl.getRef(); if (reference != null && AesGcmURLStreamHandler.IV_KEY.matcher(reference).matches()) { diff --git a/src/main/java/de/pixart/messenger/persistance/FileBackend.java b/src/main/java/de/pixart/messenger/persistance/FileBackend.java index 21fa21b46..9c904ccd7 100644 --- a/src/main/java/de/pixart/messenger/persistance/FileBackend.java +++ b/src/main/java/de/pixart/messenger/persistance/FileBackend.java @@ -123,20 +123,11 @@ public class FileBackend { return getFile(message, true); } - public DownloadableFile getFile(Message message, boolean decrypted) { - final boolean encrypted = !decrypted - && (message.getEncryption() == Message.ENCRYPTION_PGP - || message.getEncryption() == Message.ENCRYPTION_DECRYPTED); + public DownloadableFile getFileForPath(String path, String mime) { final DownloadableFile file; - String path = message.getRelativeFilePath(); - if (path == null) { - String filename = fileDateFormat.format(new Date(message.getTimeSent())) + "_" + message.getUuid().substring(0, 4); - path = filename; - } if (path.startsWith("/")) { file = new DownloadableFile(path); } else { - String mime = message.getMimeType(); if (mime != null && mime.startsWith("image")) { file = new DownloadableFile(getConversationsDirectory("Images", true) + path); } else if (mime != null && mime.startsWith("video")) { @@ -147,6 +138,18 @@ public class FileBackend { file = new DownloadableFile(getConversationsDirectory("Files", true) + path); } } + return file; + } + + public DownloadableFile getFile(Message message, boolean decrypted) { + final boolean encrypted = !decrypted + && (message.getEncryption() == Message.ENCRYPTION_PGP + || message.getEncryption() == Message.ENCRYPTION_DECRYPTED); + String path = message.getRelativeFilePath(); + if (path == null) { + path = message.getUuid(); + } + final DownloadableFile file = getFileForPath(path, message.getMimeType()); if (encrypted) { return new DownloadableFile(getConversationsDirectory("Files", true) + file.getName() + ".pgp"); } else { 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; + } } |