Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Windows 兼容 file:path 格式的url #374

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 18 additions & 0 deletions server/src/main/java/cn/keking/utils/WebUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* @author : kl
* 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
Expand Down Expand Up @@ -185,4 +188,19 @@ public static String getHost(String urlStr) {
}
return null;
}

/**
* 将文件链接中的 file: 替换为 file:///<br/>
* 因为在 Windows 系统中,io.mola.galimatias.URL.parse() 方法在解析 file: 时会出错<br/>
* <a href="https://github.com/kekingcn/kkFileView/pull/374">详细问题描述</a>
*
* @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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
3 changes: 3 additions & 0 deletions server/src/main/java/cn/keking/web/filter/TrustDirFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down