store encrypted pgp files in private cache dir

Daniel Gultsch created

Change summary

src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java |  7 
src/main/java/eu/siacs/conversations/persistance/FileBackend.java     |  9 
src/main/java/eu/siacs/conversations/utils/FileWriterException.java   | 10 
3 files changed, 19 insertions(+), 7 deletions(-)

Detailed changes

src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java 🔗

@@ -119,7 +119,7 @@ public class HttpDownloadConnection implements Transferable {
     private void setupFile() {
         final String reference = mUrl.fragment();
         if (reference != null && AesGcmURL.IV_KEY.matcher(reference).matches()) {
-            this.file = new DownloadableFile(mXmppConnectionService.getCacheDir().getAbsolutePath() + "/" + message.getUuid());
+            this.file = new DownloadableFile(mXmppConnectionService.getCacheDir(), message.getUuid());
             this.file.setKeyAndIv(CryptoHelper.hexToBytes(reference));
             Log.d(Config.LOGTAG, "create temporary OMEMO encrypted file: " + this.file.getAbsolutePath() + "(" + message.getMimeType() + ")");
         } else {
@@ -416,8 +416,9 @@ public class HttpDownloadConnection implements Transferable {
                     Log.d(Config.LOGTAG, "content-length reported on GET (" + size + ") did not match Content-Length reported on HEAD (" + expected + ")");
                 }
                 file.getParentFile().mkdirs();
+                Log.d(Config.LOGTAG,"creating file: "+file.getAbsolutePath());
                 if (!file.exists() && !file.createNewFile()) {
-                    throw new FileWriterException();
+                    throw new FileWriterException(file);
                 }
                 outputStream = AbstractConnectionManager.createOutputStream(file, false, false);
             }
@@ -428,7 +429,7 @@ public class HttpDownloadConnection implements Transferable {
                 try {
                     outputStream.write(buffer, 0, count);
                 } catch (IOException e) {
-                    throw new FileWriterException();
+                    throw new FileWriterException(file);
                 }
                 updateProgress(Math.round(((double) transmitted / expected) * 100));
             }

src/main/java/eu/siacs/conversations/persistance/FileBackend.java 🔗

@@ -1,6 +1,5 @@
 package eu.siacs.conversations.persistance;
 
-import android.annotation.TargetApi;
 import android.content.ContentResolver;
 import android.content.Context;
 import android.database.Cursor;
@@ -535,7 +534,9 @@ public class FileBackend {
         }
         final DownloadableFile file = getFileForPath(path, message.getMimeType());
         if (encrypted) {
-            return new DownloadableFile(getLegacyStorageLocation("Files"), file.getName() + ".pgp");
+            return new DownloadableFile(
+                    mXmppConnectionService.getCacheDir(),
+                    String.format("%s.%s", file.getName(), "pgp"));
         } else {
             return file;
         }
@@ -651,12 +652,12 @@ public class FileBackend {
             try {
                 ByteStreams.copy(is, os);
             } catch (IOException e) {
-                throw new FileWriterException();
+                throw new FileWriterException(file);
             }
             try {
                 os.flush();
             } catch (IOException e) {
-                throw new FileWriterException();
+                throw new FileWriterException(file);
             }
         } catch (final FileNotFoundException e) {
             cleanup(file);

src/main/java/eu/siacs/conversations/utils/FileWriterException.java 🔗

@@ -1,4 +1,14 @@
 package eu.siacs.conversations.utils;
 
+import java.io.File;
+
 public class FileWriterException extends Exception {
+
+    public FileWriterException(File file) {
+        super(String.format("Could not write to %s", file.getAbsolutePath()));
+    }
+
+    FileWriterException() {
+
+    }
 }