keep proper image file extension

This commit is contained in:
Daniel Gultsch 2015-01-11 15:19:36 +01:00
parent 12d63f2612
commit b07b7519a6
4 changed files with 39 additions and 26 deletions

View file

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

View file

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

View file

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

View file

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