Merge pull request #6255 from thundernest/fix_shared_attachment_crash

Don't crash when trying to access attachment (meta) data
This commit is contained in:
cketti 2022-08-29 14:04:36 +02:00 committed by GitHub
commit 1de1f54518
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 52 additions and 46 deletions

View file

@ -3,7 +3,6 @@ package com.fsck.k9.activity.loader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import android.content.ContentResolver;
@ -88,7 +87,7 @@ public class AttachmentContentLoader extends AsyncTaskLoader<Attachment> {
cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath());
return cachedResultAttachment;
} catch (IOException e) {
} catch (Exception e) {
Timber.e(e, "Error saving attachment!");
}

View file

@ -46,63 +46,70 @@ public class AttachmentInfoLoader extends AsyncTaskLoader<Attachment> {
@Override
public Attachment loadInBackground() {
Uri uri = sourceAttachment.uri;
String contentType = sourceAttachment.contentType;
try {
Uri uri = sourceAttachment.uri;
String contentType = sourceAttachment.contentType;
long size = -1;
String name = null;
long size = -1;
String name = null;
ContentResolver contentResolver = getContext().getContentResolver();
ContentResolver contentResolver = getContext().getContentResolver();
Cursor metadataCursor = contentResolver.query(
uri,
new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE },
null,
null,
null);
Cursor metadataCursor = contentResolver.query(
uri,
new String[] { OpenableColumns.DISPLAY_NAME, OpenableColumns.SIZE },
null,
null,
null);
if (metadataCursor != null) {
try {
if (metadataCursor.moveToFirst()) {
name = metadataCursor.getString(0);
size = metadataCursor.getInt(1);
if (metadataCursor != null) {
try {
if (metadataCursor.moveToFirst()) {
name = metadataCursor.getString(0);
size = metadataCursor.getInt(1);
}
} finally {
metadataCursor.close();
}
} finally {
metadataCursor.close();
}
}
if (name == null) {
name = uri.getLastPathSegment();
}
if (name == null) {
name = uri.getLastPathSegment();
}
String usableContentType = contentResolver.getType(uri);
if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) {
usableContentType = contentType;
}
String usableContentType = contentResolver.getType(uri);
if (usableContentType == null && contentType != null && contentType.indexOf('*') != -1) {
usableContentType = contentType;
}
if (usableContentType == null) {
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
}
if (usableContentType == null) {
usableContentType = MimeTypeUtil.getMimeTypeByExtension(name);
}
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
}
if (!sourceAttachment.allowMessageType && MimeUtility.isMessageType(usableContentType)) {
usableContentType = MimeTypeUtil.DEFAULT_ATTACHMENT_MIME_TYPE;
}
if (size <= 0) {
String uriString = uri.toString();
if (uriString.startsWith("file://")) {
File f = new File(uriString.substring("file://".length()));
size = f.length();
if (size <= 0) {
String uriString = uri.toString();
if (uriString.startsWith("file://")) {
File f = new File(uriString.substring("file://".length()));
size = f.length();
} else {
Timber.v("Not a file: %s", uriString);
}
} else {
Timber.v("Not a file: %s", uriString);
Timber.v("old attachment.size: %d", size);
}
} else {
Timber.v("old attachment.size: %d", size);
}
Timber.v("new attachment.size: %d", size);
Timber.v("new attachment.size: %d", size);
cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size);
return cachedResultAttachment;
cachedResultAttachment = sourceAttachment.deriveWithMetadataLoaded(usableContentType, name, size);
return cachedResultAttachment;
} catch (Exception e) {
Timber.e(e, "Error getting attachment meta data");
cachedResultAttachment = sourceAttachment.deriveWithLoadCancelled();
return cachedResultAttachment;
}
}
}