aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java35
1 files changed, 28 insertions, 7 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java b/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
index 6643c276..d1da4a8e 100644
--- a/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
+++ b/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
@@ -23,6 +23,7 @@ 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.FileUtils;
import de.thedevstack.conversationsplus.utils.StreamUtil;
import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.R;
@@ -44,10 +45,25 @@ public class FileBackend {
if (null == INSTANCE) {
INSTANCE = new FileBackend();
}
+ INSTANCE.checkIfDirectoriesExistAndCreateIfNot();
INSTANCE.initFileObservers();
INSTANCE.createNoMedia();
}
+ private void checkIfDirectoriesExistAndCreateIfNot() {
+ this.checkIfDirectoryExistsAndCreateIfNot(FileBackend.getPrivateFileDirectoryPath());
+ this.checkIfDirectoryExistsAndCreateIfNot(FileBackend.getConversationsFileDirectory());
+ this.checkIfDirectoryExistsAndCreateIfNot(FileBackend.getConversationsImageDirectory());
+ this.checkIfDirectoryExistsAndCreateIfNot(FileBackend.getPrivateImageDirectoryPath());
+ }
+
+ private void checkIfDirectoryExistsAndCreateIfNot(String directoryPath) {
+ File directory = new File(directoryPath);
+ if (!directory.exists()) {
+ directory.mkdirs();
+ }
+ }
+
private void initFileObservers() {
this.privateFilesDirectoryObserver = new FileDeletionObserver(FileBackend.getPrivateFileDirectoryPath());
this.privateFilesImageDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsFileDirectory());
@@ -72,6 +88,7 @@ public class FileBackend {
}
public static void onFileTransferFolderChanged() {
+ INSTANCE.checkIfDirectoryExistsAndCreateIfNot(FileBackend.getConversationsFileDirectory());
INSTANCE.conversationsFilesDirectoryObserver.stopWatching();
INSTANCE.conversationsFilesDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsFileDirectory());
INSTANCE.conversationsFilesDirectoryObserver.startWatching();
@@ -79,6 +96,7 @@ public class FileBackend {
}
public static void onImageTransferFolderChanged() {
+ INSTANCE.checkIfDirectoryExistsAndCreateIfNot(FileBackend.getConversationsImageDirectory());
INSTANCE.conversationsImagesDirectoryObserver.stopWatching();
INSTANCE.conversationsImagesDirectoryObserver = new FileDeletionObserver(FileBackend.getConversationsImageDirectory());
INSTANCE.conversationsImagesDirectoryObserver.startWatching();
@@ -107,7 +125,7 @@ public class FileBackend {
}
public static DownloadableFile getFile(Message message, boolean decrypted) {
- DownloadableFile downloadableFile = new DownloadableFile(getFilePath(message, decrypted));
+ DownloadableFile downloadableFile = new DownloadableFile(getFilePath(message, decrypted, null));
FileParams fileParams = message.getFileParams();
if (null != fileParams) {
if (null != fileParams.getKey()) {
@@ -122,10 +140,7 @@ public class FileBackend {
}
protected static String getFilePath(Message message, String extension) {
- String path = FileBackend.getFilePath(message, true);
- if (!path.endsWith(extension)) {
- path += "." + extension;
- }
+ String path = FileBackend.getFilePath(message, true, extension);
return path;
}
@@ -137,7 +152,7 @@ public class FileBackend {
return getFilePath(message, extension);
}
- protected static String getFilePath(Message message, boolean decrypted) {
+ protected static String getFilePath(Message message, boolean decrypted, String fallbackExtension) {
final boolean encrypted = !decrypted
&& (message.getEncryption() == Message.ENCRYPTION_PGP
|| message.getEncryption() == Message.ENCRYPTION_DECRYPTED);
@@ -157,7 +172,11 @@ public class FileBackend {
path = getConversationsFileDirectory() + path;
}
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
+ if (null != extension) {
path += "." + extension;
+ } else if (null != fallbackExtension) {
+ path += "." +fallbackExtension;
+ }
fileParams.setPath(path);
}
@@ -218,9 +237,11 @@ public class FileBackend {
}
public static DownloadableFile compressImageAndCopyToPrivateStorage(Message message, Bitmap scaledBitmap) throws FileCopyException {
+ if (null == message.getFileParams() || null == message.getFileParams().getPath()) {
String path = getFilePath(message, "jpg");
- message.getFileParams().setPath(path);
message.setRelativeFilePath(path); // TODO: Remove
+ }
+
DownloadableFile file = getFile(message);
file.getParentFile().mkdirs();
OutputStream os = null;