aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
diff options
context:
space:
mode:
authorlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
committerlookshe <github@lookshe.org>2016-03-06 19:42:55 +0100
commit3c400703e082a1b180b35d891b8fb3460c7d5b87 (patch)
tree28738dd90fc41b4ab71897f38d324828778ad2e3 /src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
parent72114d732427266024cdd6e27cd8d1aa60afae2f (diff)
parentf28d77dc42f6bac5a026e0b1c78562dee8de45ac (diff)
Merge branch 'trz/rebase' into trz/rename
Diffstat (limited to 'src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java')
-rw-r--r--src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java116
1 files changed, 53 insertions, 63 deletions
diff --git a/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java b/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
index d58d0583..611f573c 100644
--- a/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
+++ b/src/main/java/de/thedevstack/conversationsplus/persistance/FileBackend.java
@@ -1,38 +1,32 @@
package de.thedevstack.conversationsplus.persistance;
+import android.graphics.Bitmap;
+import android.net.Uri;
+import android.os.Environment;
+import android.util.Log;
+import android.webkit.MimeTypeMap;
+
import java.io.File;
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.text.SimpleDateFormat;
-import java.util.Arrays;
import java.util.Date;
import java.util.Locale;
-import android.graphics.Bitmap;
-import android.graphics.BitmapFactory;
-import android.net.Uri;
-import android.os.Environment;
-import android.webkit.MimeTypeMap;
-
import de.thedevstack.android.logcat.Logging;
-import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.ConversationsPlusApplication;
import de.thedevstack.conversationsplus.ConversationsPlusPreferences;
+import de.thedevstack.conversationsplus.exceptions.FileCopyException;
+import de.thedevstack.conversationsplus.utils.StreamUtil;
+import de.thedevstack.conversationsplus.Config;
import de.thedevstack.conversationsplus.R;
-import de.thedevstack.conversationsplus.entities.Transferable;
import de.thedevstack.conversationsplus.entities.DownloadableFile;
import de.thedevstack.conversationsplus.entities.Message;
-import de.thedevstack.conversationsplus.exceptions.FileCopyException;
-import de.thedevstack.conversationsplus.utils.ImageUtil;
-import de.thedevstack.conversationsplus.utils.MessageUtil;
-import de.thedevstack.conversationsplus.utils.StreamUtil;
-
-public final class FileBackend {
+public class FileBackend {
private static final SimpleDateFormat imageDateFormat = new SimpleDateFormat("yyyyMMdd_HHmmssSSS", Locale.US);
public static DownloadableFile getFile(Message message) {
@@ -40,34 +34,28 @@ public final class FileBackend {
}
public static DownloadableFile getFile(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();
- String extension;
- if (path != null && !path.isEmpty()) {
- String[] parts = path.split("\\.");
- extension = "."+parts[parts.length - 1];
+ if (path == null) {
+ path = message.getUuid();
+ }
+ if (path.startsWith("/")) {
+ file = new DownloadableFile(path);
} else {
- if (message.getType() == Message.TYPE_IMAGE || message.getType() == Message.TYPE_TEXT) {
- extension = ".png";
+ String mime = message.getMimeType();
+ if (mime != null && mime.startsWith("image")) {
+ file = new DownloadableFile(getConversationsImageDirectory() + path);
} else {
- extension = "";
+ file = new DownloadableFile(getConversationsFileDirectory() + path);
}
- 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() + file.getName() + ".pgp");
} else {
- if (path.startsWith("/")) {
- return new DownloadableFile(path);
- } else {
- if (Arrays.asList(Transferable.VALID_IMAGE_EXTENSIONS).contains(extension)) {
- return new DownloadableFile(getConversationsImageDirectory() + path);
- } else {
- return new DownloadableFile(getConversationsFileDirectory() + path);
- }
- }
+ return file;
}
}
@@ -87,12 +75,7 @@ public final class FileBackend {
return FileBackend.getPrivateFileDirectoryPath() + File.separator + "Images" + File.separator;
}
- public static DownloadableFile copyFileToPrivateStorage(Message message, Uri uri) throws FileCopyException {
- Logging.d(Config.LOGTAG, "copy " + uri.toString() + " to private storage");
- String mime = ConversationsPlusApplication.getInstance().getContentResolver().getType(uri);
- String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mime);
- message.setRelativeFilePath(FileBackend.getPrivateFileDirectoryPath() + message.getUuid() + "." + extension);
- DownloadableFile file = getFile(message);
+ public static void copyFileToPrivateStorage(File file, Uri uri) throws FileCopyException {
file.getParentFile().mkdirs();
OutputStream os = null;
InputStream is = null;
@@ -116,36 +99,43 @@ public final class FileBackend {
StreamUtil.close(is);
}
Logging.d(Config.LOGTAG, "output file name " + file);
- return file;
}
- public static DownloadableFile compressImageAndCopyToPrivateStorage(Message message, Bitmap scaledBitmap) throws FileCopyException {
+ 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);
+ copyFileToPrivateStorage(getFile(message), uri);
+ }
+
+ public static DownloadableFile compressImageAndCopyToPrivateStorage(Message message, Bitmap scaledBitmap) throws FileCopyException {
message.setRelativeFilePath(FileBackend.getPrivateImageDirectoryPath() + message.getUuid() + ".png");
- DownloadableFile file = getFile(message);
- file.getParentFile().mkdirs();
- OutputStream os = null;
- try {
- file.createNewFile();
+ DownloadableFile file = getFile(message);
+ file.getParentFile().mkdirs();
+ OutputStream os = null;
+ try {
+ file.createNewFile();
os = new FileOutputStream(file);
- boolean success = scaledBitmap.compress(Bitmap.CompressFormat.PNG, 75, os);
- if (!success) {
- throw new FileCopyException(R.string.error_compressing_image);
- }
- os.flush();
+ boolean success = scaledBitmap.compress(Bitmap.CompressFormat.PNG, 75, os);
+ if (!success) {
+ throw new FileCopyException(R.string.error_compressing_image);
+ }
+ os.flush();
} catch (IOException e) {
throw new FileCopyException(R.string.error_io_exception, e);
- } catch (SecurityException e) {
+ } catch (SecurityException e) {
throw new FileCopyException(R.string.error_security_exception_during_image_copy);
} catch (NullPointerException e) {
- throw new FileCopyException(R.string.error_io_exception);
- } finally {
- StreamUtil.close(os);
- }
+ throw new FileCopyException(R.string.error_io_exception);
+ } finally {
+ StreamUtil.close(os);
+ }
return file;
- }
+ }
- public static Uri getTakePhotoUri() {
+ public static Uri getTakePhotoUri() {
StringBuilder pathBuilder = new StringBuilder();
pathBuilder.append(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM));
pathBuilder.append('/');
@@ -170,4 +160,4 @@ public final class FileBackend {
private FileBackend() {
// Static helper class
}
-} \ No newline at end of file
+}