|
|
@@ -1,8 +1,12 @@
|
|
|
package cn.reghao.oss.sdk;
|
|
|
|
|
|
+import cn.reghao.jutil.jdk.http.UploadParam;
|
|
|
+import cn.reghao.jutil.jdk.http.WebRequest;
|
|
|
+import cn.reghao.jutil.jdk.http.WebResponse;
|
|
|
import cn.reghao.jutil.jdk.result.WebResult;
|
|
|
import cn.reghao.jutil.jdk.security.DigestUtil;
|
|
|
import cn.reghao.jutil.jdk.serializer.JsonConverter;
|
|
|
+import cn.reghao.jutil.tool.http.DefaultWebRequest;
|
|
|
import cn.reghao.oss.api.rest.UploadFileRet;
|
|
|
import com.google.gson.reflect.TypeToken;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
@@ -17,6 +21,7 @@ import java.net.http.HttpRequest;
|
|
|
import java.net.http.HttpResponse;
|
|
|
import java.nio.file.Path;
|
|
|
import java.nio.file.Paths;
|
|
|
+import java.util.HashMap;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
@@ -28,6 +33,7 @@ import java.util.function.Supplier;
|
|
|
public class ObjectUploadService {
|
|
|
private final String endpoint;
|
|
|
private final HttpClient httpClient = HttpClient.newBuilder().build();
|
|
|
+ private final DefaultWebRequest webRequest = new DefaultWebRequest();
|
|
|
|
|
|
public ObjectUploadService(String endpoint) {
|
|
|
this.endpoint = endpoint;
|
|
|
@@ -74,19 +80,20 @@ public class ObjectUploadService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
- public UploadFileRet postObject(File file, int channelId, long userId) {
|
|
|
+ public UploadFileRet postObject1(File file, int channelId, long userId) {
|
|
|
try {
|
|
|
String sha256sum = DigestUtil.sha256sum(file.getAbsolutePath());
|
|
|
MultiPartBodyPublisher publisher = new MultiPartBodyPublisher()
|
|
|
.addPart("file", Paths.get(file.getAbsolutePath()))
|
|
|
- .addPart("sha256sum", sha256sum)
|
|
|
- .addPart("channelId", channelId+"");
|
|
|
+ .addPart("channelId", ""+channelId)
|
|
|
+ .addPart("client", "client");
|
|
|
+ //.addPart("sha256sum", sha256sum)
|
|
|
|
|
|
String api = String.format("%s/", endpoint);
|
|
|
HttpRequest httpRequest = HttpRequest.newBuilder(new URI(api))
|
|
|
.version(HttpClient.Version.HTTP_1_1)
|
|
|
.header("x-user-id", userId+"")
|
|
|
- .header("authorization", "Bearer 0123456789")
|
|
|
+ //.header("authorization", "Bearer 0123456789")
|
|
|
.header("content-type", "multipart/form-data; boundary=" + publisher.getBoundary())
|
|
|
.POST(publisher.build())
|
|
|
.build();
|
|
|
@@ -100,6 +107,31 @@ public class ObjectUploadService {
|
|
|
return null;
|
|
|
}
|
|
|
|
|
|
+ public UploadFileRet postObject(File file, int channelId, long userId) {
|
|
|
+ Map<String, String> map = new HashMap<>();
|
|
|
+ map.put("channelId", ""+channelId);
|
|
|
+ map.put("client", "client");
|
|
|
+
|
|
|
+ UploadParam uploadParam = new UploadParam(file, map);
|
|
|
+ String api = String.format("%s/", endpoint);
|
|
|
+ WebResponse webResponse = webRequest.upload1(api, uploadParam, userId);
|
|
|
+ int statusCode = webResponse.getStatusCode();
|
|
|
+ String body = webResponse.getBody();
|
|
|
+ if (statusCode != 200) {
|
|
|
+ log.error("{} -> {}", statusCode, body);
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ Type type = new TypeToken<WebResult<UploadFileRet>>(){}.getType();
|
|
|
+ WebResult<UploadFileRet> webResult = JsonConverter.jsonToObject(body, type);
|
|
|
+ if (webResult.getCode() != 0) {
|
|
|
+ log.error("{}", webResult.getMsg());
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ return webResult.getData();
|
|
|
+ }
|
|
|
+
|
|
|
private UploadFileRet getResult(HttpResponse<String> httpResponse) {
|
|
|
int statusCode = httpResponse.statusCode();
|
|
|
String body = httpResponse.body();
|