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

Conversation

gkchp
Copy link
Contributor

@gkchp gkchp commented Jul 28, 2022

调用在线预览,使用 file: 开头的 url,在 WebUtils.normalizedURL() 调用 io.mola.galimatias.URL.parse() 时无法解析协议,会抛出

GalimatiasParseException: Missing scheme

比如这样的 url:

file:D:\测试scheme.docx

完整调用如下:

http://127.0.0.1:8012/onlinePreview?url=ZmlsZTpEOlzmtYvor5VzY2hlbWUuZG9jeA==

根据 RFC8089 的定义,file: 也应该是可用的
这个问题主要出现在Windows下,将 file: 替换为 file:/// 可解决

@@ -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;
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants