keep proper image file extension

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/entities/MucOptions.java          |  1 
src/main/java/eu/siacs/conversations/http/HttpConnection.java          | 17 
src/main/java/eu/siacs/conversations/persistance/FileBackend.java      | 31 
src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java | 16 
4 files changed, 39 insertions(+), 26 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/entities/MucOptions.java 🔗

@@ -4,7 +4,6 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
 
-import eu.siacs.conversations.Config;
 import eu.siacs.conversations.R;
 import eu.siacs.conversations.crypto.PgpEngine;
 import eu.siacs.conversations.xml.Element;

src/main/java/eu/siacs/conversations/http/HttpConnection.java 🔗

@@ -14,6 +14,7 @@ import java.net.MalformedURLException;
 import java.net.URL;
 import java.security.KeyManagementException;
 import java.security.NoSuchAlgorithmException;
+import java.util.Arrays;
 
 import javax.net.ssl.HostnameVerifier;
 import javax.net.ssl.HttpsURLConnection;
@@ -65,14 +66,22 @@ public class HttpConnection implements Downloadable {
 		this.message.setDownloadable(this);
 		try {
 			mUrl = new URL(message.getBody());
-			String path = mUrl.getPath().toLowerCase();
-			if (path != null && (path.endsWith(".pgp") || path.endsWith(".gpg"))) {
+			String[] parts = mUrl.getPath().toLowerCase().split("\\.");
+			String lastPart = parts.length >= 1 ? parts[parts.length - 1] : null;
+			String secondToLast = parts.length >= 2 ? parts[parts.length -2] : null;
+			if ("pgp".equals(lastPart) || "gpg".equals(lastPart)) {
 				this.message.setEncryption(Message.ENCRYPTION_PGP);
 			} else if (message.getEncryption() != Message.ENCRYPTION_OTR) {
 				this.message.setEncryption(Message.ENCRYPTION_NONE);
 			}
-			this.file = mXmppConnectionService.getFileBackend().getFile(
-					message, false);
+			String extension;
+			if (Arrays.asList(VALID_CRYPTO_EXTENSIONS).contains(lastPart)) {
+				extension = secondToLast;
+			} else {
+				extension = lastPart;
+			}
+			message.setRelativeFilePath(message.getUuid()+"."+extension);
+			this.file = mXmppConnectionService.getFileBackend().getFile(message, false);
 			String reference = mUrl.getRef();
 			if (reference != null && reference.length() == 96) {
 				this.file.setKey(CryptoHelper.hexToBytes(reference));

src/main/java/eu/siacs/conversations/persistance/FileBackend.java 🔗

@@ -57,28 +57,33 @@ public class FileBackend {
 
 	public DownloadableFile getFile(Message message, boolean decrypted) {
 		String path = message.getRelativeFilePath();
-		if (!decrypted && (message.getEncryption() == Message.ENCRYPTION_PGP || message.getEncryption() == Message.ENCRYPTION_DECRYPTED)) {
-			String extension;
-			if (path != null && !path.isEmpty()) {
-				String[] parts = path.split("\\.");
-				extension = "."+parts[parts.length - 1];
-			} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_TEXT) {
+		String extension;
+		if (path != null && !path.isEmpty()) {
+			String[] parts = path.split("\\.");
+			extension = "."+parts[parts.length - 1];
+		} else {
+			if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_TEXT) {
 				extension = ".webp";
 			} else {
 				extension = "";
 			}
+			path = message.getUuid()+extension;
+		}
+		final boolean encrypted = !decrypted
+				&& (message.getEncryption() == Message.ENCRYPTION_PGP
+				|| message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
+		if (encrypted) {
 			return new DownloadableFile(getConversationsFileDirectory()+message.getUuid()+extension+".pgp");
-		} else if (path != null && !path.isEmpty()) {
+		} else {
 			if (path.startsWith("/")) {
 				return new DownloadableFile(path);
 			} else {
-				return new DownloadableFile(getConversationsFileDirectory()+path);
+				if (message.getType() == Message.TYPE_FILE) {
+					return new DownloadableFile(getConversationsFileDirectory() + path);
+				} else {
+					return new DownloadableFile(getConversationsImageDirectory()+path);
+				}
 			}
-		} else {
-			StringBuilder filename = new StringBuilder();
-			filename.append(getConversationsImageDirectory());
-			filename.append(message.getUuid()+".webp");
-			return new DownloadableFile(filename.toString());
 		}
 	}
 

src/main/java/eu/siacs/conversations/xmpp/jingle/JingleConnection.java 🔗

@@ -101,11 +101,9 @@ public class JingleConnection implements Downloadable {
 					file.delete();
 				}
 			}
-			Log.d(Config.LOGTAG,
-					"sucessfully transmitted file:" + file.getAbsolutePath());
+			Log.d(Config.LOGTAG,"sucessfully transmitted file:" + file.getAbsolutePath());
 			if (message.getEncryption() != Message.ENCRYPTION_PGP) {
-				Intent intent = new Intent(
-						Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
+				Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
 				intent.setData(Uri.fromFile(file));
 				mXmppConnectionService.sendBroadcast(intent);
 			}
@@ -281,15 +279,17 @@ public class JingleConnection implements Downloadable {
 			if (fileNameElement != null) {
 				String[] filename = fileNameElement.getContent()
 						.toLowerCase(Locale.US).toLowerCase().split("\\.");
-				if (Arrays.asList(VALID_IMAGE_EXTENSIONS).contains(
-						filename[filename.length - 1])) {
+				String extension = filename[filename.length - 1];
+				if (Arrays.asList(VALID_IMAGE_EXTENSIONS).contains(extension)) {
 					message.setType(Message.TYPE_IMAGE);
+					message.setRelativeFilePath(message.getUuid()+"."+extension);
 				} else if (Arrays.asList(VALID_CRYPTO_EXTENSIONS).contains(
 						filename[filename.length - 1])) {
 					if (filename.length == 3) {
-						if (Arrays.asList(VALID_IMAGE_EXTENSIONS).contains(
-								filename[filename.length - 2])) {
+						extension = filename[filename.length - 2];
+						if (Arrays.asList(VALID_IMAGE_EXTENSIONS).contains(extension)) {
 							message.setType(Message.TYPE_IMAGE);
+							message.setRelativeFilePath(message.getUuid()+"."+extension);
 						} else {
 							message.setType(Message.TYPE_FILE);
 						}