From 713edcfb8d4b69f91e955d5a96af5c70c9ac2986 Mon Sep 17 00:00:00 2001 From: YiwenXia <2741163438@qq.com> Date: Tue, 19 Sep 2023 23:32:45 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=80=E4=B8=AA=E9=80=9A=E7=94=A8=E7=9A=84?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9C=8D=E5=8A=A1=E5=99=A8=E8=AE=A4=E8=AF=81?= =?UTF-8?q?=E8=AE=BF=E9=97=AE=E7=9A=84=E8=AE=BE=E8=AE=A1=EF=BC=8C=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=20https://www.gitlink.org.cn/kk-open/kkFileView/issue?= =?UTF-8?q?s/3=20=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/cn/keking/model/FileAttribute.java | 14 +++++++++ .../cn/keking/service/FileHandlerService.java | 3 ++ .../java/cn/keking/utils/DownloadUtils.java | 29 +++++++++++++++++-- 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/server/src/main/java/cn/keking/model/FileAttribute.java b/server/src/main/java/cn/keking/model/FileAttribute.java index f07ba2f6..24694ab6 100644 --- a/server/src/main/java/cn/keking/model/FileAttribute.java +++ b/server/src/main/java/cn/keking/model/FileAttribute.java @@ -20,6 +20,13 @@ public class FileAttribute { private Boolean skipDownLoad = false; private Boolean forceUpdatedCache = false; + /** + * 代理请求到文件服务器的认证请求头,格式如下: + * {“username”:"test","password":"test"} + * 请求文件服务器时,会将 json 直接塞到请求头里 + */ + private String kkProxyAuthorization; + public FileAttribute() { } @@ -124,4 +131,11 @@ public class FileAttribute { this.forceUpdatedCache = forceUpdatedCache; } + public String getKkProxyAuthorization() { + return kkProxyAuthorization; + } + + public void setKkProxyAuthorization(String kkProxyAuthorization) { + this.kkProxyAuthorization = kkProxyAuthorization; + } } diff --git a/server/src/main/java/cn/keking/service/FileHandlerService.java b/server/src/main/java/cn/keking/service/FileHandlerService.java index 8f2fe5e7..5ac991cd 100644 --- a/server/src/main/java/cn/keking/service/FileHandlerService.java +++ b/server/src/main/java/cn/keking/service/FileHandlerService.java @@ -469,6 +469,9 @@ public class FileHandlerService implements InitializingBean { if (StringUtils.hasText(userToken)) { attribute.setUserToken(userToken); } + String kkProxyAuthorization = req.getParameter( "kk-proxy-authorization"); + attribute.setKkProxyAuthorization(kkProxyAuthorization); + } return attribute; diff --git a/server/src/main/java/cn/keking/utils/DownloadUtils.java b/server/src/main/java/cn/keking/utils/DownloadUtils.java index a5d93f75..9bd93fcd 100644 --- a/server/src/main/java/cn/keking/utils/DownloadUtils.java +++ b/server/src/main/java/cn/keking/utils/DownloadUtils.java @@ -3,6 +3,7 @@ package cn.keking.utils; import cn.keking.config.ConfigConstants; import cn.keking.model.FileAttribute; import cn.keking.model.ReturnResponse; +import com.fasterxml.jackson.databind.ObjectMapper; import io.mola.galimatias.GalimatiasParseException; import org.apache.commons.io.FileUtils; import org.slf4j.Logger; @@ -12,11 +13,19 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; import java.net.URL; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Map; import java.util.UUID; import static cn.keking.utils.KkFileUtils.isFtpUrl; import static cn.keking.utils.KkFileUtils.isHttpUrl; - +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.util.StringUtils; +import org.springframework.web.client.RequestCallback; +import org.springframework.web.client.RestTemplate; /** * @author yudian-it */ @@ -27,6 +36,9 @@ public class DownloadUtils { private static final String URL_PARAM_FTP_USERNAME = "ftp.username"; 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 RestTemplate restTemplate = new RestTemplate(); + private static final ObjectMapper mapper = new ObjectMapper(); + /** * @param fileAttribute fileAttribute @@ -75,7 +87,20 @@ public class DownloadUtils { if (!fileAttribute.getSkipDownLoad()) { if (isHttpUrl(url)) { File realFile = new File(realPath); - FileUtils.copyURLToFile(url, realFile); + RequestCallback requestCallback = request -> { + request.getHeaders() + .setAccept(Arrays.asList(MediaType.APPLICATION_OCTET_STREAM, MediaType.ALL)); + String proxyAuthorization = fileAttribute.getKkProxyAuthorization(); + if(StringUtils.hasText(proxyAuthorization)){ + Map proxyAuthorizationMap = mapper.readValue(proxyAuthorization, Map.class); + proxyAuthorizationMap.entrySet().forEach(entry-> request.getHeaders().set(entry.getKey(), entry.getValue())); + } + }; + urlStr = URLDecoder.decode(urlStr, StandardCharsets.UTF_8.name()); + restTemplate.execute(urlStr, HttpMethod.GET, requestCallback, fileResponse -> { + FileUtils.copyToFile(fileResponse.getBody(), realFile); + return null; + }); } else if (isFtpUrl(url)) { String ftpUsername = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_USERNAME); String ftpPassword = WebUtils.getUrlParameterReg(fileAttribute.getUrl(), URL_PARAM_FTP_PASSWORD);