From 52cdf00244e2f54fba57615dfa48b4c747a17a98 Mon Sep 17 00:00:00 2001 From: guicaiyue <2941382431@qq.com> Date: Sat, 29 Apr 2023 10:28:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=E4=BF=AE=E5=A4=8D=E5=A4=A7=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E4=B8=8A=E4=BC=A0=E5=A4=B1=E8=B4=A5=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gradle.properties | 2 +- .../run/halo/oss/GitHubAttachmentHandler.java | 29 ++++++++++--------- 2 files changed, 17 insertions(+), 14 deletions(-) diff --git a/gradle.properties b/gradle.properties index 8d0c7be..6845d83 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1 +1 @@ -version=1.0.0-SNAPSHOT +version=1.0.1-SNAPSHOT diff --git a/src/main/java/run/halo/oss/GitHubAttachmentHandler.java b/src/main/java/run/halo/oss/GitHubAttachmentHandler.java index d3cfc94..437cb9e 100644 --- a/src/main/java/run/halo/oss/GitHubAttachmentHandler.java +++ b/src/main/java/run/halo/oss/GitHubAttachmentHandler.java @@ -3,9 +3,6 @@ import cn.hutool.core.util.StrUtil; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; -import io.netty.channel.ChannelOption; -import io.netty.handler.timeout.ReadTimeoutHandler; -import io.netty.handler.timeout.WriteTimeoutHandler; import lombok.Data; import lombok.extern.slf4j.Slf4j; import org.jsoup.internal.StringUtil; @@ -35,6 +32,7 @@ import java.nio.charset.StandardCharsets; import java.nio.file.FileAlreadyExistsException; +import java.time.Duration; import java.util.Base64; import java.util.List; import java.util.Map; @@ -62,10 +60,7 @@ public class GitHubAttachmentHandler implements AttachmentHandler { private final ReactiveExtensionClient extensionClient; public GitHubAttachmentHandler(ReactiveExtensionClient extensionClient) { - HttpClient httpClient = HttpClient.create() - .tcpConfiguration(tcpClient -> tcpClient.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, 5000) - .doOnConnected(conn -> conn.addHandlerLast(new ReadTimeoutHandler(10)) - .addHandlerLast(new WriteTimeoutHandler(10)))); + HttpClient httpClient = HttpClient.create().responseTimeout(Duration.ofMillis(10000)); webClient = WebClient.builder() .clientConnector(new ReactorClientHttpConnector(httpClient)) @@ -160,12 +155,12 @@ Mono upload(UploadContext uploadContext, GithubOssProperties prope public Mono upload(FilePart filePart, FileNameHolder fileNameHolder) { GithubOssProperties properties = fileNameHolder.properties; - Mono dataBufferMono = filePart.content().reduce(DataBuffer::write); - DataBuffer dataBuffer = dataBufferMono.block(); - byte[] bytes = dataBuffer.toByteBuffer().array(); - return getConfigMap(BasicConfig.NAME, BasicConfig.GROUP).flatMap(baseConfig -> { + return Mono.zip(filePart.content().reduce(DataBuffer::write), + getConfigMap(BasicConfig.NAME, BasicConfig.GROUP)).flatMap(tuple -> { + var dataBuffer = tuple.getT1(); + var baseConfig = tuple.getT2(); debug("配置信息", baseConfig); - String base64Content = Base64.getEncoder().encodeToString(bytes); + String base64Content = Base64.getEncoder().encodeToString(dataBuffer.toByteBuffer().array()); JSONObject jsonObject = new JSONObject(); jsonObject.putOpt("committer", new JSONObject() .putOpt("email", baseConfig.email) @@ -189,8 +184,16 @@ public Mono upload(FilePart filePart, FileNameHolder fileNameHolde return new GitHubAttachmentHandler.ObjectDetail(fileNameHolder.objectKey, githubVo, fileNameHolder.fileName, fileNameHolder.fileType); }); } else { - return Mono.error(new RuntimeException("Failed to upload file")); + return clientResponse.bodyToMono(String.class).flatMap(body -> { + log.error("上传文件失败:{}", body); + return Mono.error(new RuntimeException("Failed to upload file")); + }); } + }) + .onErrorContinue((e, i) -> { + e.printStackTrace(); + log.info(JSONUtil.toJsonStr(i)); + // Log the error here. }); }); }