Skip to content

Commit

Permalink
fix:修复大文件上传失败问题
Browse files Browse the repository at this point in the history
  • Loading branch information
guicaiyue committed Apr 29, 2023
1 parent a0b6d22 commit 52cdf00
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 14 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=1.0.0-SNAPSHOT
version=1.0.1-SNAPSHOT
29 changes: 16 additions & 13 deletions src/main/java/run/halo/oss/GitHubAttachmentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -160,12 +155,12 @@ Mono<ObjectDetail> upload(UploadContext uploadContext, GithubOssProperties prope

public Mono<ObjectDetail> upload(FilePart filePart, FileNameHolder fileNameHolder) {
GithubOssProperties properties = fileNameHolder.properties;
Mono<DataBuffer> 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)
Expand All @@ -189,8 +184,16 @@ public Mono<ObjectDetail> 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.
});
});
}
Expand Down

0 comments on commit 52cdf00

Please sign in to comment.