Guess mime types at run time

pull/164/head
M66B 5 years ago
parent bfa7bcdb85
commit d92cacf952

@ -212,7 +212,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
new Intent(FragmentMessages.ACTION_STORE_ATTACHMENT)
.putExtra("id", attachment.id)
.putExtra("name", Helper.sanitizeFilename(attachment.name))
.putExtra("type", attachment.type));
.putExtra("type", attachment.getMimeType()));
}
private void onShare(EntityAttachment attachment) {
@ -221,13 +221,15 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID, file);
Log.i("uri=" + uri);
String type = attachment.getMimeType();
// Build intent
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(uri, attachment.type);
intent.setDataAndType(uri, type);
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (!TextUtils.isEmpty(attachment.name))
intent.putExtra(Intent.EXTRA_TITLE, Helper.sanitizeFilename(attachment.name));
Log.i("Intent=" + intent + " type=" + attachment.type);
Log.i("Intent=" + intent + " type=" + type);
// Get targets
PackageManager pm = context.getPackageManager();
@ -241,7 +243,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
if (ris.size() == 0)
Snackbar.make(
(View) itemView.getParent(),
context.getString(R.string.title_no_viewer, attachment.type),
context.getString(R.string.title_no_viewer, type),
Snackbar.LENGTH_LONG).show();
else
context.startActivity(intent);

@ -22,6 +22,7 @@ package eu.faircode.email;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import androidx.annotation.NonNull;
import androidx.room.Entity;
@ -34,6 +35,7 @@ import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import javax.mail.Part;
@ -146,6 +148,30 @@ public class EntityAttachment {
}
}
String getMimeType() {
// Try to guess a better content type
// For example, sometimes PDF files are sent as application/octet-stream
if (encryption != null)
return type;
String extension = Helper.getExtension(name);
if (extension == null)
return type;
if ("application/zip".equals(type) ||
"application/octet-stream".equals(type)) {
String gtype = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
if (gtype == null || gtype.equals(type))
return type;
Log.w("Guessing file=" + name + " type=" + gtype);
return gtype;
}
return type;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof EntityAttachment) {

@ -25,7 +25,6 @@ import android.net.MailTo;
import android.net.Uri;
import android.text.Html;
import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import androidx.preference.PreferenceManager;
@ -1367,27 +1366,10 @@ public class MessageHelper {
apart.attachment.cid = (cid == null || cid.length == 0 ? null : MimeUtility.unfold(cid[0]));
apart.attachment.encryption = (apart.pgp ? EntityAttachment.PGP_MESSAGE : null);
if ("text/calendar".equalsIgnoreCase(apart.attachment.type) && TextUtils.isEmpty(apart.attachment.name))
if ("text/calendar".equalsIgnoreCase(apart.attachment.type) &&
TextUtils.isEmpty(apart.attachment.name))
apart.attachment.name = "invite.ics";
// Try to guess a better content type
// For example, sometimes PDF files are sent as application/octet-stream
if (!apart.pgp) {
String extension = Helper.getExtension(apart.attachment.name);
if (extension != null) {
if ("application/zip".equals(apart.attachment.type) ||
"application/octet-stream".equals(apart.attachment.type)) {
String type = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
if (type != null) {
if (!type.equals(apart.attachment.type))
Log.w("Guessing file=" + apart.attachment.name + " type=" + type);
apart.attachment.type = type;
}
}
}
}
if (apart.attachment.size <= 0)
apart.attachment.size = null;

Loading…
Cancel
Save