From 7e2d87f39c683c8ca0cab1be4058e0ca1fbd6e3d Mon Sep 17 00:00:00 2001 From: Daniel Gultsch Date: Tue, 9 Jun 2020 21:08:27 +0200 Subject: [PATCH] =?UTF-8?q?recover=20if=20attachImage=20can=E2=80=99t=20ge?= =?UTF-8?q?nerate=20scalled=20down=20version=20of=20image.=20fixes=20#3773?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../persistance/FileBackend.java | 17 +++++++----- .../services/XmppConnectionService.java | 26 +++++++++++-------- .../conversations/ui/util/Attachment.java | 22 ++++++++-------- 3 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java index 1ff94c7c0..98d0f3de8 100644 --- a/src/main/java/eu/siacs/conversations/persistance/FileBackend.java +++ b/src/main/java/eu/siacs/conversations/persistance/FileBackend.java @@ -704,7 +704,7 @@ public class FileBackend { return pos > 0 ? filename.substring(pos + 1) : null; } - private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException { + private void copyImageToPrivateStorage(File file, Uri image, int sampleSize) throws FileCopyException, NotAnImageFileException { file.getParentFile().mkdirs(); InputStream is = null; OutputStream os = null; @@ -724,7 +724,7 @@ public class FileBackend { originalBitmap = BitmapFactory.decodeStream(is, null, options); is.close(); if (originalBitmap == null) { - throw new FileCopyException(R.string.error_not_an_image_file); + throw new NotAnImageFileException(); } Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE); int rotation = getRotation(image); @@ -763,12 +763,12 @@ public class FileBackend { } } - public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException { + public void copyImageToPrivateStorage(File file, Uri image) throws FileCopyException, NotAnImageFileException { Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath()); copyImageToPrivateStorage(file, image, 0); } - public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException { + public void copyImageToPrivateStorage(Message message, Uri image) throws FileCopyException, NotAnImageFileException { switch (Config.IMAGE_FORMAT) { case JPEG: message.setRelativeFilePath(message.getUuid() + ".jpg"); @@ -1420,11 +1420,14 @@ public class FileBackend { } } - public class FileCopyException extends Exception { - private static final long serialVersionUID = -1010013599132881427L; + public static class NotAnImageFileException extends Exception { + + } + + public static class FileCopyException extends Exception { private int resId; - public FileCopyException(int resId) { + private FileCopyException(int resId) { this.resId = resId; } diff --git a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java index 6f1ff65cc..ebac40f99 100644 --- a/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java +++ b/src/main/java/eu/siacs/conversations/services/XmppConnectionService.java @@ -567,19 +567,23 @@ public class XmppConnectionService extends Service { mFileAddingExecutor.execute(() -> { try { getFileBackend().copyImageToPrivateStorage(message, uri); - if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { - final PgpEngine pgpEngine = getPgpEngine(); - if (pgpEngine != null) { - pgpEngine.encrypt(message, callback); - } else if (callback != null) { - callback.error(R.string.unable_to_connect_to_keychain, null); - } - } else { - sendMessage(message); - callback.success(message); - } + } catch (FileBackend.NotAnImageFileException e) { + attachFileToConversation(conversation, uri, mimeType, callback); + return; } catch (final FileBackend.FileCopyException e) { callback.error(e.getResId(), message); + return; + } + if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) { + final PgpEngine pgpEngine = getPgpEngine(); + if (pgpEngine != null) { + pgpEngine.encrypt(message, callback); + } else if (callback != null) { + callback.error(R.string.unable_to_connect_to_keychain, null); + } + } else { + sendMessage(message); + callback.success(message); } }); } diff --git a/src/main/java/eu/siacs/conversations/ui/util/Attachment.java b/src/main/java/eu/siacs/conversations/ui/util/Attachment.java index d4bd33bb2..14d5749a1 100644 --- a/src/main/java/eu/siacs/conversations/ui/util/Attachment.java +++ b/src/main/java/eu/siacs/conversations/ui/util/Attachment.java @@ -35,7 +35,6 @@ import android.content.Intent; import android.net.Uri; import android.os.Parcel; import android.os.Parcelable; -import android.util.Log; import java.io.File; import java.util.ArrayList; @@ -43,7 +42,6 @@ import java.util.Collections; import java.util.List; import java.util.UUID; -import eu.siacs.conversations.Config; import eu.siacs.conversations.utils.Compatibility; import eu.siacs.conversations.utils.MimeUtils; @@ -113,7 +111,7 @@ public class Attachment implements Parcelable { } public static boolean canBeSendInband(final List attachments) { - for(Attachment attachment : attachments) { + for (Attachment attachment : attachments) { if (attachment.type != Type.LOCATION) { return false; } @@ -122,21 +120,21 @@ public class Attachment implements Parcelable { } public static List of(final Context context, Uri uri, Type type) { - final String mime = type == Type.LOCATION ?null :MimeUtils.guessMimeTypeFromUri(context, uri); + final String mime = type == Type.LOCATION ? null : MimeUtils.guessMimeTypeFromUri(context, uri); return Collections.singletonList(new Attachment(uri, type, mime)); } public static List of(final Context context, List uris) { List attachments = new ArrayList<>(); - for(Uri uri : uris) { + for (Uri uri : uris) { final String mime = MimeUtils.guessMimeTypeFromUri(context, uri); - attachments.add(new Attachment(uri, mime != null && mime.startsWith("image/") ? Type.IMAGE : Type.FILE,mime)); + attachments.add(new Attachment(uri, mime != null && isImage(mime) ? Type.IMAGE : Type.FILE, mime)); } return attachments; } public static Attachment of(UUID uuid, final File file, String mime) { - return new Attachment(uuid, Uri.fromFile(file),mime != null && (mime.startsWith("image/") || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime); + return new Attachment(uuid, Uri.fromFile(file), mime != null && (isImage(mime) || mime.startsWith("video/")) ? Type.IMAGE : Type.FILE, mime); } public static List extractAttachments(final Context context, final Intent intent, Type type) { @@ -151,9 +149,7 @@ public class Attachment implements Parcelable { if (clipData != null) { for (int i = 0; i < clipData.getItemCount(); ++i) { final Uri uri = clipData.getItemAt(i).getUri(); - Log.d(Config.LOGTAG,"uri="+uri+" contentType="+contentType); final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType); - Log.d(Config.LOGTAG,"mime="+mime); uris.add(new Attachment(uri, type, mime)); } } @@ -165,12 +161,12 @@ public class Attachment implements Parcelable { } public boolean renderThumbnail() { - return type == Type.IMAGE || (type == Type.FILE && mime != null && renderFileThumbnail(mime)); + return type == Type.IMAGE || (type == Type.FILE && mime != null && renderFileThumbnail(mime)); } private static boolean renderFileThumbnail(final String mime) { return mime.startsWith("video/") - || mime.startsWith("image/") + || isImage(mime) || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime)); } @@ -181,4 +177,8 @@ public class Attachment implements Parcelable { public UUID getUuid() { return uuid; } + + private static boolean isImage(final String mime) { + return mime.startsWith("image/") && !mime.equals("image/svg+xml"); + } }