From d6be6ddd18335d6a4cf437a8e4f2b425c57914f1 Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 22 Feb 2022 16:05:02 +0100 Subject: [PATCH] use full file name for all new files --- .../crypto/PgpDecryptionService.java | 9 +++-- .../http/HttpDownloadConnection.java | 9 ++--- .../persistance/FileBackend.java | 40 ++++++++++++++++--- .../AttachFileToConversationRunnable.java | 2 +- .../siacs/conversations/ui/util/ViewUtil.java | 14 +++---- .../jingle/JingleFileTransferConnection.java | 8 ++-- 6 files changed, 53 insertions(+), 29 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java index 0ad103155..9a2288884 100644 --- a/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java +++ b/src/main/java/eu/siacs/conversations/crypto/PgpDecryptionService.java @@ -9,6 +9,7 @@ import org.openintents.openpgp.util.OpenPgpApi; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; @@ -194,9 +195,9 @@ public class PgpDecryptionService { String originalExtension = originalFilename == null ? null : MimeUtils.extractRelevantExtension(originalFilename); if (originalExtension != null && MimeUtils.extractRelevantExtension(outputFile.getName()) == null) { Log.d(Config.LOGTAG,"detected original filename during pgp decryption"); - String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension); - String path = outputFile.getName()+"."+originalExtension; - DownloadableFile fixedFile = mXmppConnectionService.getFileBackend().getFileForPath(path,mime); + final String mime = MimeUtils.guessMimeTypeFromExtension(originalExtension); + final String filename = outputFile.getName()+"."+originalExtension; + final File fixedFile = mXmppConnectionService.getFileBackend().getStorageLocation(filename,mime); if (fixedFile.getParentFile().mkdirs()) { Log.d(Config.LOGTAG,"created parent directories for "+fixedFile.getAbsolutePath()); } @@ -205,7 +206,7 @@ public class PgpDecryptionService { } if (outputFile.renameTo(fixedFile)) { Log.d(Config.LOGTAG, "renamed " + outputFile.getAbsolutePath() + " to " + fixedFile.getAbsolutePath()); - message.setRelativeFilePath(path); + message.setRelativeFilePath(fixedFile.getAbsolutePath()); } } final String url = message.getFileParams().url; diff --git a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java index 15dc6eac6..3c9ceb978 100644 --- a/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java +++ b/src/main/java/eu/siacs/conversations/http/HttpDownloadConnection.java @@ -96,11 +96,8 @@ public class HttpDownloadConnection implements Transferable { this.message.setEncryption(Message.ENCRYPTION_NONE); } final String ext = extension.getExtension(); - if (ext != null) { - message.setRelativeFilePath(String.format("%s.%s", message.getUuid(), ext)); - } else if (Strings.isNullOrEmpty(message.getRelativeFilePath())) { - message.setRelativeFilePath(message.getUuid()); - } + final String filename = Strings.isNullOrEmpty(ext) ? message.getUuid() : String.format("%s.%s", message.getUuid(), ext); + mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, filename); setupFile(); if (this.message.getEncryption() == Message.ENCRYPTION_AXOLOTL && this.file.getKey() == null) { this.message.setEncryption(Message.ENCRYPTION_NONE); @@ -326,7 +323,7 @@ public class HttpDownloadConnection implements Transferable { if (Strings.isNullOrEmpty(extension.getExtension()) && contentType != null) { final String fileExtension = MimeUtils.guessExtensionFromMimeType(contentType); if (fileExtension != null) { - message.setRelativeFilePath(String.format("%s.%s", message.getUuid(), fileExtension)); + mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), fileExtension), contentType); Log.d(Config.LOGTAG, "rewriting name after not finding extension in url but in content type"); setupFile(); } diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index ce7c1f8dd..505a3ebb5 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -533,7 +533,7 @@ public class FileBackend { MimeUtils.guessMimeTypeFromExtension(MimeUtils.extractRelevantExtension(path))); } - public DownloadableFile getFileForPath(final String path, final String mime) { + private DownloadableFile getFileForPath(final String path, final String mime) { if (path.startsWith("/")) { return new DownloadableFile(path); } else { @@ -719,7 +719,7 @@ public class FileBackend { if ("ogg".equals(extension) && type != null && type.startsWith("audio/")) { extension = "oga"; } - message.setRelativeFilePath(message.getUuid() + "." + extension); + setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), extension)); copyFileToPrivateStorage(mXmppConnectionService.getFileBackend().getFile(message), uri); } @@ -852,21 +852,51 @@ public class FileBackend { public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, ImageCompressionException { + final String filename; switch (Config.IMAGE_FORMAT) { case JPEG: - message.setRelativeFilePath(message.getUuid() + ".jpg"); + filename = String.format("%s.%s", message.getUuid(), "jpg"); break; case PNG: - message.setRelativeFilePath(message.getUuid() + ".png"); + filename = String.format("%s.%s", message.getUuid(), "png"); break; case WEBP: - message.setRelativeFilePath(message.getUuid() + ".webp"); + filename = String.format("%s.%s", message.getUuid(), "webp"); break; + default: + throw new IllegalStateException("Unknown image format"); } + setupRelativeFilePath(message, filename); copyImageToPrivateStorage(getFile(message), image); updateFileParams(message); } + public void setupRelativeFilePath(final Message message, final String filename) { + final String extension = MimeUtils.extractRelevantExtension(filename); + final String mime = MimeUtils.guessMimeTypeFromExtension(extension); + setupRelativeFilePath(message, filename, mime); + } + + public File getStorageLocation(final String filename, final String mime) { + final File parentDirectory; + if (Strings.isNullOrEmpty(mime)) { + parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); + } else if (mime.startsWith("image/")) { + parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); + } else if (mime.startsWith("video/")) { + parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES); + } else { + parentDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS); + } + final File appDirectory = new File(parentDirectory, mXmppConnectionService.getString(R.string.app_name)); + return new File(appDirectory, filename); + } + + public void setupRelativeFilePath(final Message message, final String filename, final String mime) { + final File file = getStorageLocation(filename, mime); + message.setRelativeFilePath(file.getAbsolutePath()); + } + public boolean unusualBounds(final Uri image) { try { final BitmapFactory.Options options = new BitmapFactory.Options(); diff --git a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java index db879799d..1ddee27b5 100644 --- a/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java +++ b/src/main/java/eu/siacs/conversations/services/AttachFileToConversationRunnable.java @@ -91,7 +91,7 @@ public class AttachFileToConversationRunnable implements Runnable, TranscoderLis private void processAsVideo() throws FileNotFoundException { Log.d(Config.LOGTAG, "processing file as video"); mXmppConnectionService.startForcingForegroundNotification(); - message.setRelativeFilePath(message.getUuid() + ".mp4"); + mXmppConnectionService.getFileBackend().setupRelativeFilePath(message, String.format("%s.%s", message.getUuid(), "mp4")); final DownloadableFile file = mXmppConnectionService.getFileBackend().getFile(message); if (Objects.requireNonNull(file.getParentFile()).mkdirs()) { Log.d(Config.LOGTAG, "created parent directory for video file"); diff --git a/src/main/java/eu/siacs/conversations/ui/util/ViewUtil.java b/src/main/java/eu/siacs/conversations/ui/util/ViewUtil.java index 013927c4e..1cc630ad3 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/ViewUtil.java +++ b/src/main/java/eu/siacs/conversations/ui/util/ViewUtil.java @@ -37,9 +37,10 @@ public class ViewUtil { view(context, file, mime); } - public static void view(Context context, File file, String mime) { - Intent openIntent = new Intent(Intent.ACTION_VIEW); - Uri uri; + private static void view(Context context, File file, String mime) { + Log.d(Config.LOGTAG,"viewing "+file.getAbsolutePath()+" "+mime); + final Intent openIntent = new Intent(Intent.ACTION_VIEW); + final Uri uri; try { uri = FileBackend.getUriForFile(context, file); } catch (SecurityException e) { @@ -49,14 +50,9 @@ public class ViewUtil { } openIntent.setDataAndType(uri, mime); openIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); - PackageManager manager = context.getPackageManager(); - List info = manager.queryIntentActivities(openIntent, 0); - if (info.size() == 0) { - openIntent.setDataAndType(uri, "*/*"); - } try { context.startActivity(openIntent); - } catch (ActivityNotFoundException e) { + } catch (final ActivityNotFoundException e) { Toast.makeText(context, R.string.no_application_found_to_open_file, Toast.LENGTH_SHORT).show(); } } diff --git a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java index 40edf0b2c..43aaa54b5 100644 --- a/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java +++ b/src/main/java/eu/siacs/conversations/xmpp/jingle/JingleFileTransferConnection.java @@ -492,19 +492,19 @@ public class JingleFileTransferConnection extends AbstractJingleConnection imple AbstractConnectionManager.Extension extension = AbstractConnectionManager.Extension.of(path); if (VALID_IMAGE_EXTENSIONS.contains(extension.main)) { message.setType(Message.TYPE_IMAGE); - message.setRelativeFilePath(message.getUuid() + "." + extension.main); + xmppConnectionService.getFileBackend().setupRelativeFilePath(message, message.getUuid() + "." + extension.main); } else if (VALID_CRYPTO_EXTENSIONS.contains(extension.main)) { if (VALID_IMAGE_EXTENSIONS.contains(extension.secondary)) { message.setType(Message.TYPE_IMAGE); - message.setRelativeFilePath(message.getUuid() + "." + extension.secondary); + xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + "." + extension.secondary); } else { message.setType(Message.TYPE_FILE); - message.setRelativeFilePath(message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : "")); + xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + (extension.secondary != null ? ("." + extension.secondary) : "")); } message.setEncryption(Message.ENCRYPTION_PGP); } else { message.setType(Message.TYPE_FILE); - message.setRelativeFilePath(message.getUuid() + (extension.main != null ? ("." + extension.main) : "")); + xmppConnectionService.getFileBackend().setupRelativeFilePath(message,message.getUuid() + (extension.main != null ? ("." + extension.main) : "")); } long size = parseLong(fileSize, 0); message.setBody(Long.toString(size)); -- 2.45.2