Browse Source

Pull request #12560: REPORT-102243 修复图表富文本编辑器自定义样式图标异常

Merge in DESIGN/design from ~VITO/c-design:final/11.0 to final/11.0

* commit '3a7fce0b20dcd86a7679fe898ff03b368905fa67':
  无jira任务 单元测试
  REPORT-102243 修复图表富文本编辑器自定义样式图标异常
new-design
vito-刘恒霖 1 year ago
parent
commit
c4b2994637
  1. 11
      designer-base/src/main/java/com/fr/design/jxbrowser/MimeType.java
  2. 14
      designer-base/src/main/java/com/fr/design/jxbrowser/NxInterceptRequestCallback.java
  3. 9
      designer-base/src/test/java/com/fr/design/jxbrowser/MimeTypeTest.java

11
designer-base/src/main/java/com/fr/design/jxbrowser/MimeType.java

@ -83,21 +83,20 @@ public enum MimeType {
* 如果没有尝试使用 Files.probeContentType 检测 * 如果没有尝试使用 Files.probeContentType 检测
* 如果没有默认返回 text/html * 如果没有默认返回 text/html
* *
* @param url url路径 * @param resourcePath 资源路径
* @return MimeType * @return MimeType
*/ */
public static String parseMimeType(String url) { public static String parseMimeType(String resourcePath) {
if (StringUtils.isBlank(url)) { if (StringUtils.isBlank(resourcePath)) {
return HTML.mimeType; return HTML.mimeType;
} }
String finalPath = url.split("\\?")[0];
Optional<MimeType> mimeType = Arrays.stream(values()) Optional<MimeType> mimeType = Arrays.stream(values())
.filter(type -> finalPath.endsWith(type.suffix)) .filter(type -> resourcePath.endsWith(type.suffix))
.findFirst(); .findFirst();
if (mimeType.isPresent()) { if (mimeType.isPresent()) {
return mimeType.get().mimeType; return mimeType.get().mimeType;
} else { } else {
return getFileMimeType(finalPath); return getFileMimeType(resourcePath);
} }
} }

14
designer-base/src/main/java/com/fr/design/jxbrowser/NxInterceptRequestCallback.java

@ -20,6 +20,7 @@ import com.teamdev.jxbrowser.net.callback.InterceptUrlRequestCallback;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.io.InputStream; import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder; import java.net.URLDecoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Map; import java.util.Map;
@ -80,8 +81,9 @@ public class NxInterceptRequestCallback implements InterceptUrlRequestCallback {
protected Optional<UrlRequestJob> generateFileProtocolUrlRequestJob(Params params, String path) { protected Optional<UrlRequestJob> generateFileProtocolUrlRequestJob(Params params, String path) {
try { try {
InputStream inputStream = getResourceStream(path); String resourcePath = getResourcePath(path);
String mimeType = MimeType.parseMimeType(path); InputStream inputStream = getResourceStream(resourcePath);
String mimeType = MimeType.parseMimeType(resourcePath);
byte[] bytes; byte[] bytes;
if (isHtml(mimeType)) { if (isHtml(mimeType)) {
String text = IOUtils.inputStream2String(inputStream, EncodeConstants.ENCODING_UTF_8); String text = IOUtils.inputStream2String(inputStream, EncodeConstants.ENCODING_UTF_8);
@ -104,7 +106,11 @@ public class NxInterceptRequestCallback implements InterceptUrlRequestCallback {
* @return 输入流 * @return 输入流
* @throws Exception IO异常 * @throws Exception IO异常
*/ */
private InputStream getResourceStream(String path) throws Exception { private InputStream getResourceStream(String path) {
return IOUtils.readResource(path);
}
private static String getResourcePath(String path) throws UnsupportedEncodingException {
int index = path.indexOf("="); int index = path.indexOf("=");
if (index > 0) { if (index > 0) {
path = path.substring(index + 1); path = path.substring(index + 1);
@ -115,7 +121,7 @@ public class NxInterceptRequestCallback implements InterceptUrlRequestCallback {
// 通过自定义协议之后的url会自动encode一些中文字符,这里做一个decode,否则会导致路径访问失败 // 通过自定义协议之后的url会自动encode一些中文字符,这里做一个decode,否则会导致路径访问失败
path = URLDecoder.decode(path, StandardCharsets.UTF_8.name()); path = URLDecoder.decode(path, StandardCharsets.UTF_8.name());
} }
return IOUtils.readResource(path); return path;
} }
private boolean isHtml(String mimeType) { private boolean isHtml(String mimeType) {

9
designer-base/src/test/java/com/fr/design/jxbrowser/MimeTypeTest.java

@ -13,9 +13,10 @@ public class MimeTypeTest {
@Test @Test
public void getMimeType() { public void getMimeType() {
Assert.assertEquals("text/html", MimeType.parseMimeType("http://a.html")); Assert.assertEquals("text/html", MimeType.parseMimeType("http://a.html"));
Assert.assertEquals("text/html", MimeType.parseMimeType("http://a.html?a=ji")); Assert.assertEquals("truetype",
Assert.assertEquals("text/html", MimeType.parseMimeType("http://a.xml?a=ji")); MimeType.parseMimeType("emb://com/fr/web/ui/resources?path=/com/fr/web/ui/font/iconfont.ttf"));
Assert.assertEquals("image/jpeg", MimeType.parseMimeType("http://a.jpg?a=ji")); Assert.assertEquals("font/woff",
Assert.assertEquals("image/jpeg", MimeType.parseMimeType("http://a.jpeg?a=ji")); MimeType.parseMimeType("http://a.html?path=com/fr/ui/a.woff"));
// 对资源来说不存在http://a.jpg?a=ji这种情况,之前多虑了
} }
} }
Loading…
Cancel
Save