From 7757729efde47b07074d1d9beb28665ccd945c48 Mon Sep 17 00:00:00 2001 From: kl Date: Mon, 13 Apr 2026 20:54:27 +0800 Subject: [PATCH 1/2] Refine archive preview and PDF defaults --- .gitignore | 1 + server/src/main/config/application.properties | 6 +- .../cn/keking/service/CompressFileReader.java | 35 +- .../service/impl/OfficeFilePreviewImpl.java | 2 +- server/src/main/resources/web/compress.ftl | 1021 ++++++++++++++++- server/src/main/resources/web/pdf.ftl | 2 +- server/src/main/resources/web/picture.ftl | 8 +- .../keking/PdfViewerCompatibilityTests.java | 28 + 8 files changed, 1019 insertions(+), 84 deletions(-) diff --git a/.gitignore b/.gitignore index 7f9ea881..8cef58ee 100644 --- a/.gitignore +++ b/.gitignore @@ -26,6 +26,7 @@ nbdist/ ### VS Code ### .vscode/ .DS_Store +.artifacts/ server/src/main/cache/ server/src/main/file/ diff --git a/server/src/main/config/application.properties b/server/src/main/config/application.properties index 70990374..0423d607 100644 --- a/server/src/main/config/application.properties +++ b/server/src/main/config/application.properties @@ -97,11 +97,11 @@ office.type.web = ${KK_OFFICE_TYPE_WEB:web} # Office文档预览类型 # 支持动态配置,可选值:image/pdf -office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:image} +office.preview.type = ${KK_OFFICE_PREVIEW_TYPE:pdf} # 是否关闭Office预览模式切换开关,默认为false(允许切换) # 设置为true时,用户无法在图片和PDF模式间切换 -office.preview.switch.disabled = ${KK_OFFICE_PREVIEW_SWITCH_DISABLED:false} +office.preview.switch.disabled = ${KK_OFFICE_PREVIEW_SWITCH_DISABLED:true} ############################################################################### @@ -475,4 +475,4 @@ kk.scriptjs = true ############################################################################### # 纯文本文件类型,直接显示 -simText = ${KK_SIMTEXT:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd} \ No newline at end of file +simText = ${KK_SIMTEXT:txt,html,htm,asp,jsp,xml,json,properties,md,gitignore,log,java,py,c,cpp,sql,sh,bat,m,bas,prg,cmd} diff --git a/server/src/main/java/cn/keking/service/CompressFileReader.java b/server/src/main/java/cn/keking/service/CompressFileReader.java index 8fb4dd46..b3e15f9e 100644 --- a/server/src/main/java/cn/keking/service/CompressFileReader.java +++ b/server/src/main/java/cn/keking/service/CompressFileReader.java @@ -59,21 +59,26 @@ public class CompressFileReader { for (final ISimpleInArchiveItem item : simpleInArchive.getArchiveItems()) { if (!item.isFolder()) { final Path filePathInsideArchive = getFilePathInsideArchive(item, folderPath); - ExtractOperationResult result = item.extractSlow(data -> { - try (OutputStream out = new BufferedOutputStream(new FileOutputStream(filePathInsideArchive.toFile(), true))) { - out.write(data); - } catch (IOException e) { - throw new RuntimeException(e); - } - return data.length; - }, filePassword); - if (result != ExtractOperationResult.OK) { - ExtractOperationResult result1 = ExtractOperationResult.valueOf("WRONG_PASSWORD"); - if (result1.equals(result)) { - throw new Exception("Password"); - }else { - throw new Exception("Failed to extract RAR file."); + Files.deleteIfExists(filePathInsideArchive); + try (OutputStream out = new BufferedOutputStream(new FileOutputStream(filePathInsideArchive.toFile(), false))) { + ExtractOperationResult result = item.extractSlow(data -> { + try { + out.write(data); + } catch (IOException e) { + throw new RuntimeException(e); + } + return data.length; + }, filePassword); + if (result != ExtractOperationResult.OK) { + ExtractOperationResult result1 = ExtractOperationResult.valueOf("WRONG_PASSWORD"); + if (result1.equals(result)) { + throw new Exception("Password"); + } else { + throw new Exception("Failed to extract RAR file."); + } } + } catch (IOException e) { + throw new RuntimeException(e); } FileType type = FileType.typeFromUrl(filePathInsideArchive.toString()); @@ -110,4 +115,4 @@ public class CompressFileReader { } -} \ No newline at end of file +} diff --git a/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java b/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java index 306ad484..00051a44 100644 --- a/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java +++ b/server/src/main/java/cn/keking/service/impl/OfficeFilePreviewImpl.java @@ -348,4 +348,4 @@ public class OfficeFilePreviewImpl implements FilePreview { } return null; } -} \ No newline at end of file +} diff --git a/server/src/main/resources/web/compress.ftl b/server/src/main/resources/web/compress.ftl index 4013a15f..b57018eb 100644 --- a/server/src/main/resources/web/compress.ftl +++ b/server/src/main/resources/web/compress.ftl @@ -1,88 +1,985 @@ - + + ${file.name}压缩包预览 - - <#include "*/commonHeader.ftl"> - - - - - -
-

kkFileView

- + +
+
+ + +
+
+
+
+

kkFileView

+

未选择文件

+
+
+ 等待选择文件 +
+ +
+
+
+

选择文件后可在此预览

+

支持在当前页面预览压缩包内的文本、图片、PDF、Office 等文件。

+
+ 文本预览 + 图片预览 + Office 预览 +
+
+
+ + +
+
+
+ - \ No newline at end of file + diff --git a/server/src/main/resources/web/pdf.ftl b/server/src/main/resources/web/pdf.ftl index c697d6c9..fb2e44c5 100644 --- a/server/src/main/resources/web/pdf.ftl +++ b/server/src/main/resources/web/pdf.ftl @@ -27,7 +27,7 @@ if (kkagent === 'true' || !url.startsWith(baseUrl)) { url = baseUrl + 'getCorsFile?urlPath=' + encodeURIComponent(Base64.encode(url))+ "&key=${kkkey}"; } - document.getElementsByTagName('iframe')[0].src = "${baseUrl}pdfjs/web/viewer.html?file=" + encodeURIComponent(url) + "&disablepresentationmode=${pdfPresentationModeDisable}&disableopenfile=${pdfOpenFileDisable}&disableprint=${pdfPrintDisable}&disabledownload=${pdfDownloadDisable}&disablebookmark=${pdfBookmarkDisable}&disableediting=${pdfDisableEditing}"; + document.getElementsByTagName('iframe')[0].src = "${baseUrl}pdfjs/web/viewer.html?file=" + encodeURIComponent(url) + "&disablepresentationmode=${pdfPresentationModeDisable}&disableopenfile=${pdfOpenFileDisable}&disableprint=${pdfPrintDisable}&disabledownload=${pdfDownloadDisable}&disablebookmark=${pdfBookmarkDisable}&disableediting=${pdfDisableEditing}#page=1&pagemode=thumbs"; document.getElementsByTagName('iframe')[0].height = document.documentElement.clientHeight - 10; /** * 页面变化调整高度 diff --git a/server/src/main/resources/web/picture.ftl b/server/src/main/resources/web/picture.ftl index b0c19d1f..4017b5a6 100644 --- a/server/src/main/resources/web/picture.ftl +++ b/server/src/main/resources/web/picture.ftl @@ -9,7 +9,11 @@