diff options
author | steckbrief <steckbrief@chefmail.de> | 2017-07-31 08:44:32 +0200 |
---|---|---|
committer | steckbrief <steckbrief@chefmail.de> | 2017-07-31 08:44:32 +0200 |
commit | 11e2b1accd933eb9fcb4477a60dd0864d9f72a67 (patch) | |
tree | e1d27c488d5a697557095add001dca868f523ef4 /src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java | |
parent | 2d41916750e2c31b57cf6325441ff37a8639fe93 (diff) |
Implements FS#245: Implement FiletransferHttp (upload and delete), some minor bug fixes including to fail a JingleTransfer in case criterias are not met
Diffstat (limited to '')
-rw-r--r-- | src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java | 41 |
1 files changed, 31 insertions, 10 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); - path += "." + extension; + 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 { - String path = getFilePath(message, "jpg"); - message.getFileParams().setPath(path); - message.setRelativeFilePath(path); // TODO: Remove + if (null == message.getFileParams() || null == message.getFileParams().getPath()) { + String path = getFilePath(message, "jpg"); + message.setRelativeFilePath(path); // TODO: Remove + } + DownloadableFile file = getFile(message); file.getParentFile().mkdirs(); OutputStream os = null; |