support for pgp files

This commit is contained in:
iNPUTmice 2014-11-14 03:27:18 +01:00
parent c7acfe85c3
commit 16847a30c8
5 changed files with 38 additions and 23 deletions

View File

@ -19,7 +19,7 @@ public final class Config {
public static final int MESSAGE_MERGE_WINDOW = 20;
public static final boolean PARSE_EMOTICONS = false;
public static final int PROGRESS_UI_UPDATE_INTERVAL = 750;
public static final int PROGRESS_UI_UPDATE_INTERVAL = 750;
private Config() {

View File

@ -80,12 +80,13 @@ public class PgpEngine {
}
}
});
} else if (message.getType() == Message.TYPE_IMAGE) {
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
try {
final DownloadableFile inputFile = this.mXmppConnectionService
.getFileBackend().getFile(message, false);
final DownloadableFile outputFile = this.mXmppConnectionService
.getFileBackend().getFile(message, true);
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
InputStream is = new FileInputStream(inputFile);
OutputStream os = new FileOutputStream(outputFile);
@ -199,12 +200,13 @@ public class PgpEngine {
}
}
});
} else if (message.getType() == Message.TYPE_IMAGE) {
} else if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_FILE) {
try {
DownloadableFile inputFile = this.mXmppConnectionService
.getFileBackend().getFile(message, true);
DownloadableFile outputFile = this.mXmppConnectionService
.getFileBackend().getFile(message, false);
outputFile.getParentFile().mkdirs();
outputFile.createNewFile();
InputStream is = new FileInputStream(inputFile);
OutputStream os = new FileOutputStream(outputFile);

View File

@ -2,14 +2,12 @@ package eu.siacs.conversations.persistance;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
@ -17,7 +15,6 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import android.content.ContentResolver;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
@ -60,30 +57,36 @@ public class FileBackend {
public DownloadableFile getFile(Message message, boolean decrypted) {
String path = message.getRelativeFilePath();
if (path != null && !path.isEmpty()) {
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) {
extension = ".webp";
} else {
extension = "";
}
return new DownloadableFile(getConversationsFileDirectory()+message.getUuid()+extension+".pgp");
} else if (path != null && !path.isEmpty()) {
if (path.startsWith("/")) {
return new DownloadableFile(path);
} else {
return new DownloadableFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)+"/"+path);
return new DownloadableFile(getConversationsFileDirectory()+path);
}
} else {
StringBuilder filename = new StringBuilder();
filename.append(getConversationsDirectory());
filename.append(message.getUuid());
if ((decrypted) || (message.getEncryption() == Message.ENCRYPTION_NONE)) {
filename.append(".webp");
} else {
if (message.getEncryption() == Message.ENCRYPTION_OTR) {
filename.append(".webp");
} else {
filename.append(".webp.pgp");
}
}
filename.append(getConversationsImageDirectory());
filename.append(message.getUuid()+".webp");
return new DownloadableFile(filename.toString());
}
}
public static String getConversationsDirectory() {
public static String getConversationsFileDirectory() {
return Environment.getExternalStorageDirectory().getAbsolutePath()+"/Conversations/";
}
public static String getConversationsImageDirectory() {
return Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES).getAbsolutePath()
+ "/Conversations/";

View File

@ -211,7 +211,7 @@ public class XmppConnectionService extends Service {
private Integer rosterChangedListenerCount = 0;
private SecureRandom mRandom;
private FileObserver fileObserver = new FileObserver(
FileBackend.getConversationsDirectory()) {
FileBackend.getConversationsImageDirectory()) {
@Override
public void onEvent(int event, String path) {
@ -311,7 +311,11 @@ public class XmppConnectionService extends Service {
if (path!=null) {
message.setRelativeFilePath(path);
getFileBackend().updateFileParams(message);
callback.success(message);
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
getPgpEngine().encrypt(message, callback);
} else {
callback.success(message);
}
} else {
new Thread(new Runnable() {
@Override
@ -319,7 +323,11 @@ public class XmppConnectionService extends Service {
try {
getFileBackend().copyFileToPrivateStorage(message, uri);
getFileBackend().updateFileParams(message);
callback.success(message);
if (message.getEncryption() == Message.ENCRYPTION_DECRYPTED) {
getPgpEngine().encrypt(message, callback);
} else {
callback.success(message);
}
} catch (FileBackend.FileCopyException e) {
callback.error(e.getResId(),message);
}

View File

@ -306,6 +306,8 @@ public class JingleConnection implements Downloadable {
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);