Determine mime type based on file extension

This commit is contained in:
boedy 2019-05-11 15:43:50 +02:00
parent c9f998b642
commit c1d68dd88b

View File

@ -1,29 +1,24 @@
package org.apache.cordova.plugin; package org.apache.cordova.plugin;
import android.annotation.TargetApi; import android.net.Uri;
import android.os.Build;
import android.util.Log; import android.util.Log;
import android.webkit.MimeTypeMap;
import org.apache.cordova.PluginResult; import org.apache.cordova.PluginResult;
import org.json.JSONException; import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import fi.iki.elonen.NanoHTTPD;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.UUID;
import java.util.HashMap;
import java.util.Map;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
import fi.iki.elonen.NanoHTTPD;
public class NanoHTTPDWebserver extends NanoHTTPD { public class NanoHTTPDWebserver extends NanoHTTPD {
@ -191,6 +186,20 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
return res; return res;
} }
/**
* Get mime type based of file extension
* @param url
* @return
*/
public static String getMimeType(String url) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(url);
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}
return type;
}
@Override @Override
public Response serve(IHTTPSession session) { public Response serve(IHTTPSession session) {
Log.d(this.getClass().getName(), "New request is incoming!"); Log.d(this.getClass().getName(), "New request is incoming!");
@ -220,16 +229,16 @@ public class NanoHTTPDWebserver extends NanoHTTPD {
Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString()); Log.d(this.getClass().getName(), "responseObject: " + responseObject.toString());
if (responseObject.has("path")) { if (responseObject.has("path")) {
// TODO should specify a more correct mime-type
try { try {
return serveFile(session.getHeaders(), new File(responseObject.getString("path")), responseObject.getString("type")); File file = new File(responseObject.getString("path"));
} Uri uri = Uri.fromFile(file);
catch (JSONException e) { String mime = getMimeType(uri.toString());
return serveFile(session.getHeaders(), file, mime);
} catch (JSONException e) {
e.printStackTrace(); e.printStackTrace();
} }
return response; return response;
} } else {
else {
try { try {
response = newFixedLengthResponse( response = newFixedLengthResponse(
Response.Status.lookup(responseObject.getInt("status")), Response.Status.lookup(responseObject.getInt("status")),