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
Changes from 1 commit
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 @@ -11,6 +11,7 @@
import io.mola.galimatias.GalimatiasParseException;
import jodd.io.NetUtil;
import org.apache.commons.codec.binary.Base64;
import org.artofsolving.jodconverter.util.PlatformUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
Expand Down Expand Up @@ -38,6 +39,8 @@
public class OnlinePreviewController {

public static final String BASE64_DECODE_ERROR_MSG = "Base64解码失败,请检查你的 %s 是否采用 Base64 + urlEncode 双重编码了!";
public static final String FILE_SCHEME = "file:";
public static final String FILE_SCHEME_FULL = "file:///";
private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);

private final FilePreviewFactory previewFactory;
Expand All @@ -61,6 +64,11 @@ public String onlinePreview(String url, Model model, HttpServletRequest req) {
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
return otherFilePreview.notSupportedFile(model, errorMsg);
}
if (PlatformUtils.isWindows()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 入口方法尽量不要添加这类逻辑,在 getFileAttribute() 处理会更好吗?
  • 然后建议封装一个具有具体含义的方法出来,因为看起来 TrustDirFilter.java 里也有需要处理的代码,如:
    private boolean allowPreview(String urlPath) {
        try {
            URL url = WebUtils.normalizedURL(urlPath);
            if ("file".equals(url.getProtocol().toLowerCase(Locale.ROOT))) {
                String filePath = URLDecoder.decode(url.getPath(), StandardCharsets.UTF_8.name());
                if (PlatformUtils.isWindows()) {
                    filePath = filePath.replaceAll("/", "\\\\");
                }
                return filePath.startsWith(ConfigConstants.getFileDir()) || filePath.startsWith(ConfigConstants.getLocalPreviewDir());
            }
            return true;
        } catch (IOException | GalimatiasParseException e) {
            logger.error("解析URL异常,url:{}", urlPath, e);
            return false;
        }
    }

if (fileUrl.startsWith(FILE_SCHEME) && !fileUrl.startsWith(FILE_SCHEME + "/")) {
fileUrl = FILE_SCHEME_FULL + fileUrl.substring(FILE_SCHEME.length());
}
}
FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req);
model.addAttribute("file", fileAttribute);
FilePreview filePreview = previewFactory.get(fileAttribute);
Expand Down Expand Up @@ -111,8 +119,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