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.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import android.content.ContentResolver; import android.content.ContentResolver;
@ -88,7 +87,7 @@ public class AttachmentContentLoader extends AsyncTaskLoader<Attachment> {
cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath()); cachedResultAttachment = sourceAttachment.deriveWithLoadComplete(file.getAbsolutePath());
return cachedResultAttachment; return cachedResultAttachment;
} catch (IOException e) { } catch (Exception e) {
Timber.e(e, "Error saving attachment!"); Timber.e(e, "Error saving attachment!");
} }

View file

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