diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java
index 272a7a3c0..9562c4cf5 100644
--- a/server/src/main/java/cn/keking/service/FileHandlerService.java
+++ b/server/src/main/java/cn/keking/service/FileHandlerService.java
@@ -14,6 +14,7 @@
import org.apache.pdfbox.rendering.ImageType;
import org.apache.pdfbox.rendering.PDFRenderer;
import org.apache.pdfbox.tools.imageio.ImageIOUtil;
+import org.artofsolving.jodconverter.util.PlatformUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
@@ -276,6 +277,9 @@ public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
attribute.setType(type);
attribute.setName(fileName);
attribute.setSuffix(suffix);
+ if (PlatformUtils.isWindows()) {
+ url = WebUtils.enrichFileSchemeUrl(url);
+ }
url = WebUtils.encodeUrlFileName(url);
attribute.setUrl(url);
if (req != null) {
diff --git a/server/src/main/java/cn/keking/utils/WebUtils.java b/server/src/main/java/cn/keking/utils/WebUtils.java
index 38472eaf0..47a1acc10 100644
--- a/server/src/main/java/cn/keking/utils/WebUtils.java
+++ b/server/src/main/java/cn/keking/utils/WebUtils.java
@@ -11,6 +11,7 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
+import java.util.Locale;
import java.util.Map;
/**
@@ -18,6 +19,8 @@
* create : 2020-12-27 1:30 上午
**/
public class WebUtils {
+ public static final String FILE_SCHEME = "file:";
+ public static final String FILE_SCHEME_FULL = "file:///";
/**
* 获取标准的URL
@@ -185,4 +188,19 @@ public static String getHost(String urlStr) {
}
return null;
}
+
+ /**
+ * 将文件链接中的 file: 替换为 file:///
+ * 因为在 Windows 系统中,io.mola.galimatias.URL.parse() 方法在解析 file: 时会出错
+ * 详细问题描述
+ *
+ * @param fileUrl 文件 url
+ * @return 处理后的 url
+ */
+ public static String enrichFileSchemeUrl(String fileUrl) {
+ if (fileUrl.toLowerCase(Locale.ROOT).startsWith(FILE_SCHEME) && !fileUrl.toLowerCase(Locale.ROOT).startsWith(FILE_SCHEME + "/")) {
+ return FILE_SCHEME_FULL + fileUrl.substring(FILE_SCHEME.length());
+ }
+ return fileUrl;
+ }
}
diff --git a/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java b/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java
index 0855dca05..705f48e1f 100644
--- a/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java
+++ b/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java
@@ -30,6 +30,7 @@
import java.util.List;
import static cn.keking.service.FilePreview.PICTURE_FILE_PREVIEW_PAGE;
+import static cn.keking.utils.WebUtils.FILE_SCHEME;
/**
* @author yudian-it
@@ -111,8 +112,8 @@ public void getCorsFile(String urlPath, HttpServletResponse response) {
logger.error(String.format(BASE64_DECODE_ERROR_MSG, urlPath),ex);
return;
}
- if (urlPath.toLowerCase().startsWith("file:") || urlPath.toLowerCase().startsWith("file%3")
- || !urlPath.toLowerCase().startsWith("http")) {
+ if (urlPath.toLowerCase().startsWith(FILE_SCHEME) || urlPath.toLowerCase().startsWith("file%3")
+ || !urlPath.toLowerCase().startsWith("http")) {
logger.info("读取跨域文件异常,可能存在非法访问,urlPath:{}", urlPath);
return;
}
diff --git a/server/src/main/java/cn/keking/web/filter/TrustDirFilter.java b/server/src/main/java/cn/keking/web/filter/TrustDirFilter.java
index ec0cee402..cae41da3b 100644
--- a/server/src/main/java/cn/keking/web/filter/TrustDirFilter.java
+++ b/server/src/main/java/cn/keking/web/filter/TrustDirFilter.java
@@ -56,6 +56,9 @@ public void destroy() {
private boolean allowPreview(String urlPath) {
try {
+ if (PlatformUtils.isWindows()) {
+ urlPath = WebUtils.enrichFileSchemeUrl(urlPath);
+ }
URL url = WebUtils.normalizedURL(urlPath);
if ("file".equals(url.getProtocol().toLowerCase(Locale.ROOT))) {
String filePath = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name());