aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/eu/siacs/conversations/persistance/FileBackend.java')
-rw-r--r--src/main/java/eu/siacs/conversations/persistance/FileBackend.java67
1 files changed, 48 insertions, 19 deletions
diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
index 8abbb0cb..6f99ef55 100644
--- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
+++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java
@@ -20,9 +20,12 @@ import java.util.Locale;
import de.thedevstack.android.logcat.Logging;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
+import de.thedevstack.conversationsplus.entities.FileParams;
import de.thedevstack.conversationsplus.exceptions.FileCopyException;
import de.thedevstack.conversationsplus.persistance.observers.FileDeletionObserver;
import de.thedevstack.conversationsplus.utils.StreamUtil;
+import de.thedevstack.conversationsplus.utils.XmppConnectionServiceAccessor;
+
import eu.siacs.conversations.Config;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.DownloadableFile;
@@ -90,10 +93,10 @@ public class FileBackend {
}
}
- public static boolean deleteFile(Message message, XmppConnectionService xmppConnectionService) {
+ public static boolean deleteFile(Message message) {
File file = getFile(message);
if (file.delete()) {
- updateMediaScanner(file, xmppConnectionService);
+ updateMediaScanner(file, XmppConnectionServiceAccessor.xmppConnectionService);
return true;
} else {
return false;
@@ -105,29 +108,53 @@ public class FileBackend {
}
public static DownloadableFile getFile(Message message, boolean decrypted) {
+ return new DownloadableFile(getFilePath(message, decrypted));
+ }
+
+ protected static String getFilePath(Message message, String extension) {
+ String path = FileBackend.getFilePath(message, true);
+ if (!path.endsWith(extension)) {
+ path += "." + extension;
+ }
+
+ return path;
+ }
+
+ protected static String getFilePath(Message message, Uri uri) {
+ String mime = ConversationsPlusApplication.getInstance().getContentResolver().getType(uri);
+ String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
+
+ return getFilePath(message, extension);
+ }
+
+ protected static String getFilePath(Message message, boolean decrypted) {
final boolean encrypted = !decrypted
&& (message.getEncryption() == Message.ENCRYPTION_PGP
|| message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
- final DownloadableFile file;
- String path = message.getRelativeFilePath();
- if (path == null) {
- path = message.getUuid();
+ FileParams fileParams = message.getFileParams();
+ if (null == fileParams) {
+ fileParams = new FileParams();
+ message.setFileParams(fileParams);
}
- if (path.startsWith("/")) {
- file = new DownloadableFile(path);
- } else {
+ String path = fileParams.getPath();
+
+ if (null == path) { // File does not yet exist
+ path = message.getUuid();
String mime = message.getMimeType();
- if (mime != null && mime.startsWith("image")) {
- file = new DownloadableFile(getConversationsImageDirectory() + path);
+ if (mime != null && mime.startsWith("image")) { // TODO: Check if this can be determined in a better way
+ path = getConversationsImageDirectory() + path;
} else {
- file = new DownloadableFile(getConversationsFileDirectory() + path);
+ path = getConversationsFileDirectory() + path;
}
+ String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
+ path += "." + extension;
+
+ fileParams.setPath(path);
}
if (encrypted) {
- return new DownloadableFile(getConversationsFileDirectory() + file.getName() + ".pgp");
- } else {
- return file;
+ path += ".pgp";
}
+ return path;
}
public static String getConversationsFileDirectory() {
@@ -174,14 +201,16 @@ public class FileBackend {
public static void copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
Log.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage");
- String mime = ConversationsPlusApplication.getInstance().getContentResolver().getType(uri);
- String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
- message.setRelativeFilePath(message.getUuid() + "." + extension);
+ String path = getFilePath(message, uri);
+ message.getFileParams().setPath(path);
+ message.setRelativeFilePath(path); // TODO: Remove
copyFileToPrivateStorage(getFile(message), uri);
}
public static DownloadableFile compressImageAndCopyToPrivateStorage(Message message, Bitmap scaledBitmap) throws FileCopyException {
- message.setRelativeFilePath(FileBackend.getPrivateImageDirectoryPath() + message.getUuid() + ".jpg");
+ String path = getFilePath(message, "jpg");
+ message.getFileParams().setPath(path);
+ message.setRelativeFilePath(path); // TODO: Remove
DownloadableFile file = getFile(message);
file.getParentFile().mkdirs();
OutputStream os = null;