store encrypted pgp files in private cache dir

This commit is contained in:
Daniel Gultsch 2022-02-23 16:59:40 +01:00
parent a3085fbf1f
commit be1fcfe4f9
3 changed files with 19 additions and 7 deletions

View file

@ -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));
}

View file

@ -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);

View file

@ -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() {
}
}