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

View file

@ -567,6 +567,13 @@ public class XmppConnectionService extends Service {
mFileAddingExecutor.execute(() -> {
try {
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) {
final PgpEngine pgpEngine = getPgpEngine();
if (pgpEngine != null) {
@ -578,9 +585,6 @@ public class XmppConnectionService extends Service {
sendMessage(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.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;
@ -130,13 +128,13 @@ public class Attachment implements Parcelable {
List<Attachment> attachments = new ArrayList<>();
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<Attachment> 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));
}
}
@ -170,7 +166,7 @@ public class Attachment implements Parcelable {
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");
}
}