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

fix 解决错误转义导致的压缩包内文件预览问题 #533

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
18 changes: 10 additions & 8 deletions server/src/main/java/cn/keking/service/FileHandlerService.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,14 @@ private String getPdf2jpgUrl(String pdfFilePath, int index) {
String baseUrl = BaseUrlFilter.getBaseUrl();
pdfFilePath = pdfFilePath.replace(fileDir, "");
String pdfFolder = pdfFilePath.substring(0, pdfFilePath.length() - 4);
String urlPrefix;
try {
urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20");
} catch (UnsupportedEncodingException e) {
logger.error("UnsupportedEncodingException", e);
urlPrefix = baseUrl + pdfFolder;
}
// 移除多余的转义(压缩包中的文件路径/会被转义,导致无法正常预览)
String urlPrefix = baseUrl + pdfFolder;
// try {
// urlPrefix = baseUrl + URLEncoder.encode(pdfFolder, uriEncoding).replaceAll("\\+", "%20");
// } catch (UnsupportedEncodingException e) {
// logger.error("UnsupportedEncodingException", e);
// urlPrefix = baseUrl + pdfFolder;
// }
return urlPrefix + "/" + index + PDF2JPG_IMAGE_FORMAT;
}

Expand Down Expand Up @@ -468,7 +469,8 @@ public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
url = WebUtils.encodeUrlFileName(url);
if (UrlEncoderUtils.hasUrlEncoded(originFileName)) { //判断文件名是否转义
try {
originFileName = URLDecoder.decode(originFileName, uriEncoding).replaceAll("\\+", "%20");
// 无需转义+号,否则文件名包含+号将无法正常下载资源
originFileName = URLDecoder.decode(originFileName, uriEncoding);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
Expand Down
10 changes: 4 additions & 6 deletions server/src/main/java/cn/keking/utils/RarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,12 @@ public static boolean judge(char c){
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z');
}
public static boolean isMessyCode(String strName) {
//去除字符串中的空格 制表符 换行 回车
Pattern p = Pattern.compile("\\s*|\t*|\r*|\n*");
//去除字符串中的空白字符 + # & = 标点符号
Pattern p = Pattern.compile("\\s|\\+|#|&|=|\\p{P}");
Matcher m = p.matcher(strName);
String after = m.replaceAll("").replaceAll("\\+", "").replaceAll("#", "").replaceAll("&", "");
//去除字符串中的标点符号
String temp = after.replaceAll("\\p{P}", "");
String after = m.replaceAll("");
//处理之后转换成字符数组
char[] ch = temp.trim().toCharArray();
char[] ch = after.trim().toCharArray();
for (char c : ch) {
//判断是否是数字或者英文字符
if (!judge(c)) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/main/resources/web/compress.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@

function chooseNode(event, treeId, treeNode) {
if (!treeNode.isParent) {
var path = '${baseUrl}' + treeNode.id + "?kkCompressfileKey=" + '${fileTree}';
// 转义特殊字符,解决压缩包内特殊文件名无法预览问题
var path = '${baseUrl}' + treeNode.id.split("/").map(name=>encodeURIComponent(name)).join("/") + "?kkCompressfileKey=" + encodeURIComponent('${fileTree}');
location.href = "${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode(path));
}
}
Expand Down