recover if attachImage can’t generate scalled down version of image. fixes #3773

This commit is contained in:
Daniel Gultsch 2020-06-09 21:08:27 +02:00
parent 1853242c66
commit 7e2d87f39c
3 changed files with 36 additions and 29 deletions

View file

@ -704,7 +704,7 @@ public class FileBackend {
return pos > 0 ? filename.substring(pos + 1) : null; 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(); file.getParentFile().mkdirs();
InputStream is = null; InputStream is = null;
OutputStream os = null; OutputStream os = null;
@ -724,7 +724,7 @@ public class FileBackend {
originalBitmap = BitmapFactory.decodeStream(is, null, options); originalBitmap = BitmapFactory.decodeStream(is, null, options);
is.close(); is.close();
if (originalBitmap == null) { if (originalBitmap == null) {
throw new FileCopyException(R.string.error_not_an_image_file); throw new NotAnImageFileException();
} }
Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE); Bitmap scaledBitmap = resize(originalBitmap, Config.IMAGE_SIZE);
int rotation = getRotation(image); 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()); Log.d(Config.LOGTAG, "copy image (" + image.toString() + ") to private storage " + file.getAbsolutePath());
copyImageToPrivateStorage(file, image, 0); 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) { switch (Config.IMAGE_FORMAT) {
case JPEG: case JPEG:
message.setRelativeFilePath(message.getUuid() + ".jpg"); message.setRelativeFilePath(message.getUuid() + ".jpg");
@ -1420,11 +1420,14 @@ public class FileBackend {
} }
} }
public class FileCopyException extends Exception { public static class NotAnImageFileException extends Exception {
private static final long serialVersionUID = -1010013599132881427L;
}
public static class FileCopyException extends Exception {
private int resId; private int resId;
public FileCopyException(int resId) { private FileCopyException(int resId) {
this.resId = resId; this.resId = resId;
} }

View file

@ -567,6 +567,13 @@ public class XmppConnectionService extends Service {
mFileAddingExecutor.execute(() -> { mFileAddingExecutor.execute(() -> {
try { try {
getFileBackend().copyImageToPrivateStorage(message, uri); getFileBackend().copyImageToPrivateStorage(message, uri);
} 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) { if (conversation.getNextEncryption() == Message.ENCRYPTION_PGP) {
final PgpEngine pgpEngine = getPgpEngine(); final PgpEngine pgpEngine = getPgpEngine();
if (pgpEngine != null) { if (pgpEngine != null) {
@ -578,9 +585,6 @@ public class XmppConnectionService extends Service {
sendMessage(message); sendMessage(message);
callback.success(message); callback.success(message);
} }
} catch (final FileBackend.FileCopyException e) {
callback.error(e.getResId(), message);
}
}); });
} }

View file

@ -35,7 +35,6 @@ import android.content.Intent;
import android.net.Uri; import android.net.Uri;
import android.os.Parcel; import android.os.Parcel;
import android.os.Parcelable; import android.os.Parcelable;
import android.util.Log;
import java.io.File; import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
@ -43,7 +42,6 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
import eu.siacs.conversations.Config;
import eu.siacs.conversations.utils.Compatibility; import eu.siacs.conversations.utils.Compatibility;
import eu.siacs.conversations.utils.MimeUtils; import eu.siacs.conversations.utils.MimeUtils;
@ -113,7 +111,7 @@ public class Attachment implements Parcelable {
} }
public static boolean canBeSendInband(final List<Attachment> attachments) { public static boolean canBeSendInband(final List<Attachment> attachments) {
for(Attachment attachment : attachments) { for (Attachment attachment : attachments) {
if (attachment.type != Type.LOCATION) { if (attachment.type != Type.LOCATION) {
return false; return false;
} }
@ -122,21 +120,21 @@ public class Attachment implements Parcelable {
} }
public static List<Attachment> of(final Context context, Uri uri, Type type) { public static List<Attachment> 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)); return Collections.singletonList(new Attachment(uri, type, mime));
} }
public static List<Attachment> of(final Context context, List<Uri> uris) { public static List<Attachment> of(final Context context, List<Uri> uris) {
List<Attachment> attachments = new ArrayList<>(); List<Attachment> attachments = new ArrayList<>();
for(Uri uri : uris) { for (Uri uri : uris) {
final String mime = MimeUtils.guessMimeTypeFromUri(context, uri); 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; return attachments;
} }
public static Attachment of(UUID uuid, final File file, String mime) { 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<Attachment> extractAttachments(final Context context, final Intent intent, Type type) { public static List<Attachment> extractAttachments(final Context context, final Intent intent, Type type) {
@ -151,9 +149,7 @@ public class Attachment implements Parcelable {
if (clipData != null) { if (clipData != null) {
for (int i = 0; i < clipData.getItemCount(); ++i) { for (int i = 0; i < clipData.getItemCount(); ++i) {
final Uri uri = clipData.getItemAt(i).getUri(); final Uri uri = clipData.getItemAt(i).getUri();
Log.d(Config.LOGTAG,"uri="+uri+" contentType="+contentType);
final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType); final String mime = MimeUtils.guessMimeTypeFromUriAndMime(context, uri, contentType);
Log.d(Config.LOGTAG,"mime="+mime);
uris.add(new Attachment(uri, type, mime)); uris.add(new Attachment(uri, type, mime));
} }
} }
@ -170,7 +166,7 @@ public class Attachment implements Parcelable {
private static boolean renderFileThumbnail(final String mime) { private static boolean renderFileThumbnail(final String mime) {
return mime.startsWith("video/") return mime.startsWith("video/")
|| mime.startsWith("image/") || isImage(mime)
|| (Compatibility.runsTwentyOne() && "application/pdf".equals(mime)); || (Compatibility.runsTwentyOne() && "application/pdf".equals(mime));
} }
@ -181,4 +177,8 @@ public class Attachment implements Parcelable {
public UUID getUuid() { public UUID getUuid() {
return uuid; return uuid;
} }
private static boolean isImage(final String mime) {
return mime.startsWith("image/") && !mime.equals("image/svg+xml");
}
} }