From 6006e1d1b9442c00b85058053d76f22758092030 Mon Sep 17 00:00:00 2001 From: zwt13703 Date: Wed, 13 May 2026 20:00:04 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20onlinePreview=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E4=B8=AD=EF=BC=8CURL=20=E6=8C=87=E5=90=91=E7=9A=84=E5=85=AC?= =?UTF-8?q?=E7=BD=91=E6=96=87=E4=BB=B6=E8=BF=87=E5=A4=A7=E5=AF=BC=E8=87=B4?= =?UTF-8?q?=E4=B8=8B=E8=BD=BD=E7=BC=93=E6=85=A2=EF=BC=8C=E4=BD=86=E8=BF=99?= =?UTF-8?q?=E4=BA=9B=E6=96=87=E4=BB=B6=E5=AE=9E=E9=99=85=E5=B0=B1=E5=AD=98?= =?UTF-8?q?=E6=94=BE=E5=9C=A8=E5=BD=93=E5=89=8D=E6=9C=8D=E5=8A=A1=E5=99=A8?= =?UTF-8?q?=E7=9A=84=20/home/img=20=E7=9B=AE=E5=BD=95=E4=B8=8B=EF=BC=8C?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E4=BC=98=E5=85=88=E4=BD=BF=E7=94=A8=E6=9C=AC?= =?UTF-8?q?=E5=9C=B0=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/OnlinePreviewController.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java b/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java index 666e322..2b79eb8 100644 --- a/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java +++ b/server/src/main/java/cn/keking/web/controller/OnlinePreviewController.java @@ -25,9 +25,12 @@ import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.springframework.web.client.HttpClientErrorException; +import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URL; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.List; @@ -49,6 +52,7 @@ public class OnlinePreviewController { private static final String URL_PARAM_FTP_PASSWORD = "ftp.password"; private static final String URL_PARAM_FTP_CONTROL_ENCODING = "ftp.control.encoding"; private static final String URL_PARAM_FTP_PORT = "ftp.control.port"; + private static final String LOCAL_FILE_ROOT = "/home/img"; private final FilePreviewFactory previewFactory; private final CacheService cacheService; @@ -82,6 +86,8 @@ public class OnlinePreviewController { String errorMsg = String.format(BASE64_DECODE_ERROR_MSG, "url"); return otherFilePreview.notSupportedFile(model, errorMsg); } + // 优先检查本地文件,本地存在则直接使用,避免从公网下载大文件 + fileUrl = checkLocalFile(fileUrl); FileAttribute fileAttribute = fileHandlerService.getFileAttribute(fileUrl, req); highlightall= KkFileUtils.htmlEscape(highlightall); @@ -287,4 +293,31 @@ public class OnlinePreviewController { cacheService.addQueueTask(fileUrls); return "success"; } + + /** + * 检查本地是否已有该文件,若有则返回本地文件路径,避免从公网重复下载 + * + * @param fileUrl 解码后的文件URL + * @return 本地文件路径(如果存在),否则返回原始URL + */ + private String checkLocalFile(String fileUrl) { + if (fileUrl == null || (!fileUrl.startsWith("http://") && !fileUrl.startsWith("https://"))) { + return fileUrl; + } + try { + URL url = new URL(fileUrl); + String path = url.getPath(); + if (path != null && !path.isEmpty()) { + String decodedPath = URLDecoder.decode(path, StandardCharsets.UTF_8); + File localFile = new File(LOCAL_FILE_ROOT, decodedPath); + if (localFile.exists() && localFile.isFile()) { + logger.info("本地文件存在,使用本地文件:{}", localFile.getAbsolutePath()); + return "file://" + localFile.getAbsolutePath(); + } + } + } catch (Exception e) { + logger.warn("检查本地文件失败,url:{}", fileUrl, e); + } + return fileUrl; + } } \ No newline at end of file