Skip to content

Commit

Permalink
Expose the mime mapping class as API
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Sep 27, 2024
1 parent 22ce154 commit 4b6aea1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
*/

package io.vertx.core.http.impl;
package io.vertx.core.http;

import java.util.HashMap;
import java.util.Map;
Expand All @@ -20,7 +20,7 @@
*
* @author <a href="http://tfox.org">Tim Fox</a>
*/
public class MimeMapping {
public final class MimeMapping {
private static final Map<String, String> m = new HashMap<>();

static {
Expand Down Expand Up @@ -1013,14 +1013,23 @@ public class MimeMapping {
m.put("ice", "x-conference/x-cooltalk");
}

public static String getMimeTypeForExtension(String ext) {
/**
* @param ext the file name extension
* @return the matching mime type for a file extension (e.g. {@code .mkv}) or {@code null}
*/
public static String mimeTypeForExtension(String ext) {
return m.get(ext);
}
public static String getMimeTypeForFilename(String filename) {

/**
* @param filename the file name
* @return the matching mime type for a file name or {@code null}, the file extension is used for lookup
*/
public static String mimeTypeForFilename(String filename) {
int li = filename.lastIndexOf('.');
if (li != -1 && li != filename.length() - 1) {
String ext = filename.substring(li + 1, filename.length());
return MimeMapping.getMimeTypeForExtension(ext);
String ext = filename.substring(li + 1);
return mimeTypeForExtension(ext);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,8 @@
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.internal.buffer.BufferInternal;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.HttpClosedException;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import io.vertx.core.internal.ContextInternal;
import io.vertx.core.internal.VertxInternal;
Expand Down Expand Up @@ -459,7 +455,7 @@ public Future<Void> sendFile(String filename, long offset, long length) {
}

if (!headers.contains(HttpHeaders.CONTENT_TYPE)) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
String contentType = MimeMapping.mimeTypeForFilename(filename);
if (contentType != null) {
headers.set(HttpHeaders.CONTENT_TYPE, contentType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,8 @@
import io.vertx.core.MultiMap;
import io.vertx.core.Promise;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.*;
import io.vertx.core.internal.buffer.BufferInternal;
import io.vertx.core.http.Cookie;
import io.vertx.core.http.HttpHeaders;
import io.vertx.core.http.HttpMethod;
import io.vertx.core.http.HttpServerResponse;
import io.vertx.core.http.StreamPriority;
import io.vertx.core.http.StreamResetException;
import io.vertx.core.http.impl.headers.Http2HeadersAdaptor;
import io.vertx.core.internal.PromiseInternal;
import io.vertx.core.net.HostAndPort;
Expand Down Expand Up @@ -559,7 +554,7 @@ public Future<Void> sendFile(String filename, long offset, long length) {
putHeader(HttpHeaderNames.CONTENT_LENGTH, String.valueOf(contentLength));
}
if (headers.get(HttpHeaderNames.CONTENT_TYPE) == null) {
String contentType = MimeMapping.getMimeTypeForFilename(filename);
String contentType = MimeMapping.mimeTypeForFilename(filename);
if (contentType != null) {
putHeader(HttpHeaderNames.CONTENT_TYPE, contentType);
}
Expand Down

0 comments on commit 4b6aea1

Please sign in to comment.