Skip to content

Commit

Permalink
!299 修复压缩获取路径错误,图片合集路径错误,水印问题等BUG
Browse files Browse the repository at this point in the history
Merge pull request !299 from 高雄/yashuoba
  • Loading branch information
gitchenjh authored and gitee-org committed Jul 2, 2024
2 parents 7abfb67 + c8dc638 commit 77f5adb
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 25 deletions.
16 changes: 10 additions & 6 deletions server/src/main/java/cn/keking/service/CompressFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.io.OutputStream;
import java.io.RandomAccessFile;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand Down Expand Up @@ -48,7 +49,6 @@ public String unRar(String filePath, String filePassword, String fileName, FileA
if (fileAttribute.isCompressFile()) {
folderName = "_decompression" + folderName;
}

Path folderPath = Paths.get(fileDir, folderName + packagePath);
Files.createDirectories(folderPath);

Expand All @@ -67,14 +67,18 @@ public String unRar(String filePath, String filePassword, String fileName, FileA
}
return data.length;
}, filePassword);

if (result != ExtractOperationResult.OK) {
throw new Exception("Failed to extract RAR file.");
ExtractOperationResult result1 = ExtractOperationResult.valueOf("WRONG_PASSWORD");
if (result1.equals(result)) {
throw new Exception("Password");
}else {
throw new Exception("Failed to extract RAR file.");
}
}

FileType type = FileType.typeFromUrl(filePathInsideArchive.toString());
if (type.equals(FileType.PICTURE)) {
imgUrls.add(baseUrl + folderPath.relativize(filePathInsideArchive).toString().replace("\\", "/"));
if (type.equals(FileType.PICTURE)) { //图片缓存到集合,为了特殊符号需要进行编码
imgUrls.add(baseUrl + URLEncoder.encode(fileName + packagePath+"/"+ folderPath.relativize(filePathInsideArchive).toString().replace("\\", "/"), "UTF-8"));
}
}
}
Expand Down Expand Up @@ -106,4 +110,4 @@ private Path getFilePathInsideArchive(ISimpleInArchiveItem item, Path folderPath
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -474,11 +474,7 @@ public FileAttribute getFileAttribute(String url, HttpServletRequest req) {
boolean isCompressFile = !ObjectUtils.isEmpty(compressFileKey);
if (isCompressFile) { //判断是否使用特定压缩包符号
try {
// http://127.0.0.1:8012/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_
// http://127.0.0.1:8012/preview/各类型文件1 - 副本.zip_/各类型文件/正常预览/PPT转的PDF.pdf?kkCompressfileKey=各类型文件1 - 副本.zip_ 获取路径就会错误 需要下面的方法
String urlStrr = getSubString(compressFilePath, compressFileKey); //反代情况下添加前缀,只获取有压缩包字符的路径
originFileName = compressFileKey + urlStrr.trim(); //拼接完整路径
originFileName = URLDecoder.decode(originFileName, uriEncoding); //压缩包文件中文编码问题
originFileName = URLDecoder.decode(originFileName, uriEncoding); //转义的文件名 解下出原始文件名
attribute.setSkipDownLoad(true);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,11 @@ public String filePreviewHandle(String url, Model model, FileAttribute fileAttri
try {
fileTree = compressFileReader.unRar(filePath, filePassword, fileName, fileAttribute);
} catch (Exception e) {
logger.error("Error processing RAR file: " + e.getMessage(), e);
Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
for (Throwable throwable : throwableArray) {
if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
if (e.getMessage().toLowerCase().contains(Rar_PASSWORD_MSG)) {
model.addAttribute("needFilePassword", true);
return EXEL_FILE_PREVIEW_PAGE;
}
}
if (e.getMessage().toLowerCase().contains(Rar_PASSWORD_MSG)) {
model.addAttribute("needFilePassword", true);
return EXEL_FILE_PREVIEW_PAGE;
}else {
logger.error("Error processing RAR file: " + e.getMessage(), e);
}
}
if (!ObjectUtils.isEmpty(fileTree)) {
Expand Down
2 changes: 1 addition & 1 deletion server/src/main/java/cn/keking/utils/RarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public static boolean judge(char c){
}
public static String specialSymbols(String str) {
//去除压缩包文件字符串中特殊符号
Pattern p = Pattern.compile("\\s|\t|\r|\n|\\+|#|&|=|\\p{P}");
Pattern p = Pattern.compile("\\s|\t|\r|\n|\\+|#|&|=|�|\\p{P}");
// Pattern p = Pattern.compile("\\s|\\+|#|&|=|\\p{P}");
Matcher m = p.matcher(str);
return m.replaceAll("");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ public Object directory(String urls) {
String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url");
return ReturnResponse.failure(errorMsg);
}
fileUrl = fileUrl.replaceAll("http://", "");
if (KkFileUtils.isIllegalFileName(fileUrl)) {
return ReturnResponse.failure("不允许访问的路径:");
}
Expand Down
25 changes: 21 additions & 4 deletions server/src/main/resources/web/compress.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html>
<head>
<meta charset="utf-8"/>
<title>压缩包预览</title>
<title>${file.name}压缩包预览</title>
<script src="js/jquery-3.6.1.min.js"></script>
<#include "*/commonHeader.ftl">
<script src="js/base64.min.js" type="text/javascript"></script>
Expand Down Expand Up @@ -49,14 +49,28 @@
onClick: chooseNode,
}
};

function isNotEmpty(value) {
return value !== null && value !== undefined && value !== '' && value !== 0 && !(value instanceof Array && value.length === 0) && !isNaN(value);
}
function getQueryParam(url, param) {
var urlObj = new URL(url);
return urlObj.searchParams.get(param);
}
var currentUrl = window.location.href;
var keyword = getQueryParam(currentUrl, 'watermarkTxt');
function chooseNode(event, treeId, treeNode) {
if (!treeNode.isParent) {
var path = '${baseUrl}' + treeNode.id + "?kkCompressfileKey=" + encodeURIComponent('${fileTree}')+"&kkCompressfilepath=" + encodeURIComponent(treeNode.id)+"&fullfilename="+encodeURIComponent(treeNode.name);
location.href = "${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode(path));
var path = '${baseUrl}'+treeNode.id+"?kkCompressfileKey="+'${fileTree}'+"&kkCompressfilepath="+encodeURIComponent(treeNode.id)+"&fullfilename="+encodeURIComponent(treeNode.name);
if (isNotEmpty(keyword)){
location.href = "${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode(path))+"&watermarkTxt="+keyword;
}else{
location.href = "${baseUrl}onlinePreview?url=" + encodeURIComponent(Base64.encode(path));}

}
}
$(document).ready(function () {
var url = '${fileTree}';
var url = "http://"+'${fileTree}'; //添加http协议方法
$.ajax({
type: "get",
url: "${baseUrl}directory?urls="+encodeURIComponent(Base64.encode(url)),
Expand All @@ -66,6 +80,9 @@
}
});
});
window.onload = function () {
initWaterMark();
}
</script>
</body>
</html>

0 comments on commit 77f5adb

Please sign in to comment.