|
|
|
package com.fr.design.jxbrowser;
|
|
|
|
|
|
|
|
import com.fr.stable.StringUtils;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Path;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Optional;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jxbrowser 使用的一些媒体类型
|
|
|
|
*
|
|
|
|
* @author vito
|
|
|
|
* @since 11.0
|
|
|
|
* Created on 2023/6/13
|
|
|
|
*/
|
|
|
|
public enum MimeType {
|
|
|
|
/**
|
|
|
|
* html 格式
|
|
|
|
*/
|
|
|
|
HTML(".html", "text/html"),
|
|
|
|
/**
|
|
|
|
* CSS 格式
|
|
|
|
*/
|
|
|
|
CSS(".css", "text/css"),
|
|
|
|
/**
|
|
|
|
* js 格式
|
|
|
|
*/
|
|
|
|
JS(".js", "text/javascript"),
|
|
|
|
/**
|
|
|
|
* svg 格式
|
|
|
|
*/
|
|
|
|
SVG(".svg", "image/svg+xml"),
|
|
|
|
/**
|
|
|
|
* png 格式
|
|
|
|
*/
|
|
|
|
PNG(".png", "image/png"),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jpg 格式
|
|
|
|
*/
|
|
|
|
JPG(".jpg", "image/jpeg"),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* jpeg 格式
|
|
|
|
*/
|
|
|
|
JPEG(".jpeg", "image/jpeg"),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* gif 格式
|
|
|
|
*/
|
|
|
|
GIF(".gif", "image/gif"),
|
|
|
|
/**
|
|
|
|
* woff 字体格式
|
|
|
|
*/
|
|
|
|
WOFF(".woff", "font/woff"),
|
|
|
|
/**
|
|
|
|
* ttf 字体格式
|
|
|
|
*/
|
|
|
|
TTF(".ttf", "truetype"),
|
|
|
|
|
|
|
|
/**
|
|
|
|
* MS 嵌入式开放字体
|
|
|
|
*/
|
|
|
|
EOT(".eot", "embedded-opentype");
|
|
|
|
|
|
|
|
private final String suffix;
|
|
|
|
private final String mimeType;
|
|
|
|
|
|
|
|
MimeType(String suffix, String mimeType) {
|
|
|
|
this.suffix = suffix;
|
|
|
|
this.mimeType = mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
public String getMimeType() {
|
|
|
|
return mimeType;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 获取指定路径对应的 mimetype,优先匹配常量中的类型
|
|
|
|
* 如果没有,尝试使用 Files.probeContentType 检测
|
|
|
|
* 如果没有,默认返回 text/html
|
|
|
|
*
|
|
|
|
* @param url url路径
|
|
|
|
* @return MimeType
|
|
|
|
*/
|
|
|
|
public static String parseMimeType(String url) {
|
|
|
|
if (StringUtils.isBlank(url)) {
|
|
|
|
return HTML.mimeType;
|
|
|
|
}
|
|
|
|
String finalPath = url.split("\\?")[0];
|
|
|
|
Optional<MimeType> mimeType = Arrays.stream(values())
|
|
|
|
.filter(type -> finalPath.endsWith(type.suffix))
|
|
|
|
.findFirst();
|
|
|
|
if (mimeType.isPresent()) {
|
|
|
|
return mimeType.get().mimeType;
|
|
|
|
} else {
|
|
|
|
return getFileMimeType(finalPath);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static String getFileMimeType(String finalPath) {
|
|
|
|
Path file = new File(finalPath).toPath();
|
|
|
|
try {
|
|
|
|
String s = Files.probeContentType(file);
|
|
|
|
return StringUtils.isEmpty(s) ? HTML.mimeType : s;
|
|
|
|
} catch (IOException e) {
|
|
|
|
return HTML.mimeType;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|