Improved type guessing

pull/184/head
M66B 4 years ago
parent d893a81042
commit d1eb596451

@ -26,7 +26,6 @@ import android.os.Bundle;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.ImageButton; import android.widget.ImageButton;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.ProgressBar; import android.widget.ProgressBar;
@ -110,7 +109,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
ibDelete.setVisibility(readonly ? View.GONE : View.VISIBLE); ibDelete.setVisibility(readonly ? View.GONE : View.VISIBLE);
int resid = 0; int resid = 0;
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(attachment.getMimeType()); String extension = Helper.guessExtension(attachment.getMimeType());
if (extension != null) if (extension != null)
resid = context.getResources().getIdentifier("file_" + extension, "drawable", context.getPackageName()); resid = context.getResources().getIdentifier("file_" + extension, "drawable", context.getPackageName());
if (resid == 0) if (resid == 0)

@ -22,7 +22,6 @@ package eu.faircode.email;
import android.content.Context; import android.content.Context;
import android.os.Build; import android.os.Build;
import android.text.TextUtils; import android.text.TextUtils;
import android.webkit.MimeTypeMap;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
import androidx.room.Entity; import androidx.room.Entity;
@ -35,7 +34,6 @@ import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Locale;
import java.util.Objects; import java.util.Objects;
import javax.mail.Part; import javax.mail.Part;
@ -173,26 +171,7 @@ public class EntityAttachment {
if (extension == null) if (extension == null)
return type; return type;
String gtype = MimeTypeMap.getSingleton() // Fix types
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
if (gtype != null) {
if (TextUtils.isEmpty(type) || "*/*".equals(type))
type = gtype;
// Some servers erroneously remove dots from mime types
if (gtype.replace(".", "").equals(type))
type = gtype;
}
if ("csv".equals(extension))
return "text/csv";
if ("eml".equals(extension))
return "message/rfc822";
if ("gpx".equals(extension))
return "application/gpx+xml";
if ("text/plain".equals(type) && "ics".equals(extension)) if ("text/plain".equals(type) && "ics".equals(extension))
return "text/calendar"; return "text/calendar";
@ -211,17 +190,20 @@ public class EntityAttachment {
if ("application/vnd.ms-pps".equals(type)) if ("application/vnd.ms-pps".equals(type))
return "application/vnd.ms-powerpoint"; return "application/vnd.ms-powerpoint";
if (TextUtils.isEmpty(type) || // Guess types
type.startsWith("unknown/") || type.endsWith("/unknown") || String gtype = Helper.guessMimeType(name);
"application/octet-stream".equals(type) || "application/zip".equals(type)) { if (gtype != null) {
if ("log".equalsIgnoreCase(extension)) if (TextUtils.isEmpty(type) ||
return "text/plain"; "*/*".equals(type) ||
type.startsWith("unknown/") ||
if (gtype == null || gtype.equals(type)) type.endsWith("/unknown") ||
return type; "application/octet-stream".equals(type) ||
"application/zip".equals(type))
return gtype;
Log.w("Guessing file=" + name + " type=" + gtype); // Some servers erroneously remove dots from mime types
return gtype; if (gtype.replace(".", "").equals(type))
return gtype;
} }
return type; return type;

@ -2879,7 +2879,9 @@ public class FragmentCompose extends FragmentBase {
ftype = null; ftype = null;
} }
if (TextUtils.isEmpty(ftype) || "*/*".equals(ftype)) if (TextUtils.isEmpty(ftype) ||
"*/*".equals(ftype) ||
"application/octet-stream".equals(ftype))
ftype = Helper.guessMimeType(fname); ftype = Helper.guessMimeType(fname);
if (fsize != null && fsize <= 0) if (fsize != null && fsize <= 0)

@ -941,16 +941,50 @@ public class Helper {
String type = null; String type = null;
String extension = Helper.getExtension(filename); String extension = Helper.getExtension(filename);
if (extension != null) if (extension != null) {
extension = extension.toLowerCase(Locale.ROOT);
type = MimeTypeMap.getSingleton() type = MimeTypeMap.getSingleton()
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT)); .getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
}
if (TextUtils.isEmpty(type)) if (TextUtils.isEmpty(type))
type = "application/octet-stream"; if ("csv".equals(extension))
return "text/csv";
else if ("eml".equals(extension))
return "message/rfc822";
else if ("gpx".equals(extension))
return "application/gpx+xml";
else if ("log".equals(extension))
return "text/plain";
else if ("ovpn".equals(extension))
return "application/x-openvpn-profile";
else
return "application/octet-stream";
return type; return type;
} }
static String guessExtension(String mimeType) {
String extension = null;
if (mimeType != null) {
mimeType = mimeType.toLowerCase(Locale.ROOT);
extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
}
if (TextUtils.isEmpty(extension))
if ("text/csv".equals(mimeType))
return "csv";
else if ("message/rfc822".equals(mimeType))
return "eml";
else if ("application/gpx+xml".equals(mimeType))
return "gpx";
else if ("application/x-openvpn-profile".equals(mimeType))
return "ovpn";
return extension;
}
static void writeText(File file, String content) throws IOException { static void writeText(File file, String content) throws IOException {
try (FileOutputStream out = new FileOutputStream(file)) { try (FileOutputStream out = new FileOutputStream(file)) {
if (content != null) if (content != null)

Loading…
Cancel
Save