mirror of https://github.com/M66B/FairEmail.git
Improved type guessing
This commit is contained in:
parent
d893a81042
commit
d1eb596451
|
@ -26,7 +26,6 @@ import android.os.Bundle;
|
|||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.ImageButton;
|
||||
import android.widget.ImageView;
|
||||
import android.widget.ProgressBar;
|
||||
|
@ -110,7 +109,7 @@ public class AdapterAttachment extends RecyclerView.Adapter<AdapterAttachment.Vi
|
|||
ibDelete.setVisibility(readonly ? View.GONE : View.VISIBLE);
|
||||
|
||||
int resid = 0;
|
||||
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(attachment.getMimeType());
|
||||
String extension = Helper.guessExtension(attachment.getMimeType());
|
||||
if (extension != null)
|
||||
resid = context.getResources().getIdentifier("file_" + extension, "drawable", context.getPackageName());
|
||||
if (resid == 0)
|
||||
|
|
|
@ -22,7 +22,6 @@ 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;
|
||||
|
@ -35,7 +34,6 @@ 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;
|
||||
|
@ -173,26 +171,7 @@ public class EntityAttachment {
|
|||
if (extension == null)
|
||||
return type;
|
||||
|
||||
String gtype = MimeTypeMap.getSingleton()
|
||||
.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";
|
||||
// Fix types
|
||||
|
||||
if ("text/plain".equals(type) && "ics".equals(extension))
|
||||
return "text/calendar";
|
||||
|
@ -211,17 +190,20 @@ public class EntityAttachment {
|
|||
if ("application/vnd.ms-pps".equals(type))
|
||||
return "application/vnd.ms-powerpoint";
|
||||
|
||||
if (TextUtils.isEmpty(type) ||
|
||||
type.startsWith("unknown/") || type.endsWith("/unknown") ||
|
||||
"application/octet-stream".equals(type) || "application/zip".equals(type)) {
|
||||
if ("log".equalsIgnoreCase(extension))
|
||||
return "text/plain";
|
||||
// Guess types
|
||||
String gtype = Helper.guessMimeType(name);
|
||||
if (gtype != null) {
|
||||
if (TextUtils.isEmpty(type) ||
|
||||
"*/*".equals(type) ||
|
||||
type.startsWith("unknown/") ||
|
||||
type.endsWith("/unknown") ||
|
||||
"application/octet-stream".equals(type) ||
|
||||
"application/zip".equals(type))
|
||||
return gtype;
|
||||
|
||||
if (gtype == null || gtype.equals(type))
|
||||
return type;
|
||||
|
||||
Log.w("Guessing file=" + name + " type=" + gtype);
|
||||
return gtype;
|
||||
// Some servers erroneously remove dots from mime types
|
||||
if (gtype.replace(".", "").equals(type))
|
||||
return gtype;
|
||||
}
|
||||
|
||||
return type;
|
||||
|
|
|
@ -2879,7 +2879,9 @@ public class FragmentCompose extends FragmentBase {
|
|||
ftype = null;
|
||||
}
|
||||
|
||||
if (TextUtils.isEmpty(ftype) || "*/*".equals(ftype))
|
||||
if (TextUtils.isEmpty(ftype) ||
|
||||
"*/*".equals(ftype) ||
|
||||
"application/octet-stream".equals(ftype))
|
||||
ftype = Helper.guessMimeType(fname);
|
||||
|
||||
if (fsize != null && fsize <= 0)
|
||||
|
|
|
@ -941,16 +941,50 @@ public class Helper {
|
|||
String type = null;
|
||||
|
||||
String extension = Helper.getExtension(filename);
|
||||
if (extension != null)
|
||||
if (extension != null) {
|
||||
extension = extension.toLowerCase(Locale.ROOT);
|
||||
type = MimeTypeMap.getSingleton()
|
||||
.getMimeTypeFromExtension(extension.toLowerCase(Locale.ROOT));
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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 {
|
||||
try (FileOutputStream out = new FileOutputStream(file)) {
|
||||
if (content != null)
|
||||
|
|
Loading…
Reference in New Issue