|
|
@@ -1,8 +1,11 @@
|
|
|
package com.kmall.common.fileserver.minio;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.kmall.common.utils.SpringContextUtils;
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
import org.slf4j.Logger;
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.core.env.Environment;
|
|
|
import org.springframework.core.io.ByteArrayResource;
|
|
|
import org.springframework.core.io.Resource;
|
|
|
import org.springframework.http.HttpEntity;
|
|
|
@@ -13,25 +16,64 @@ import org.springframework.util.LinkedMultiValueMap;
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
import org.springframework.web.client.RestTemplate;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
-import org.springframework.web.multipart.commons.CommonsFileUploadSupport;
|
|
|
-import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Arrays;
|
|
|
|
|
|
public class MinIOUtils {
|
|
|
|
|
|
+ private static volatile String host = null;
|
|
|
+
|
|
|
+ private static volatile String baseApi = null;
|
|
|
+
|
|
|
+ private static volatile boolean inited = false;
|
|
|
+
|
|
|
private static final Logger log = LoggerFactory.getLogger(MinIOUtils.class);
|
|
|
|
|
|
private static final RestTemplate restTemplate = new RestTemplate();
|
|
|
|
|
|
+ private static String buildUrl(String uri) {
|
|
|
+ return getBaseUrl() + uri;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String getBaseUrl() {
|
|
|
+ if (!inited) {
|
|
|
+ synchronized (MinIOUtils.class) {
|
|
|
+ if (!inited) {
|
|
|
+ Environment env = SpringContextUtils.getBean(Environment.class);
|
|
|
+ try {
|
|
|
+ String profiles = env.getActiveProfiles()[0];
|
|
|
+ if ("dev".equals(profiles)) {
|
|
|
+ host = "http://127.0.0.1:9393";
|
|
|
+ baseApi = "/dev-api";
|
|
|
+ } else if ("test".equals(profiles)) {
|
|
|
+ host = "http://3f352e8584.qicp.vip:9393";
|
|
|
+ baseApi = "/test-api";
|
|
|
+ } else if ("prod".equals(profiles)) {
|
|
|
+ host = "http://3f352e8584.qicp.vip:9393";
|
|
|
+ baseApi = "/prod-api";
|
|
|
+ } else {
|
|
|
+ host = "http://3f352e8584.qicp.vip:9393";
|
|
|
+ baseApi = "/prod-api";
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ host = "http://3f352e8584.qicp.vip:9393";
|
|
|
+ baseApi = "/prod-api";
|
|
|
+ }
|
|
|
+ inited = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return host + baseApi;
|
|
|
+ }
|
|
|
+
|
|
|
public static FileInfo upload(MultipartFile file) throws IOException {
|
|
|
- String url = EOSSConfiguration.uploadUrl;
|
|
|
+
|
|
|
+ String url = buildUrl(EOSSProperties.uploadUri);
|
|
|
MediaType mediaType = MediaType.MULTIPART_FORM_DATA;
|
|
|
HttpHeaders headers = new HttpHeaders();
|
|
|
headers.setContentType(mediaType);
|
|
|
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
|
|
- Resource resource = new ByteArrayResource(file.getBytes(),file.getName()) {
|
|
|
+ Resource resource = new ByteArrayResource(file.getBytes(), file.getName()) {
|
|
|
@Override
|
|
|
public String getFilename() {
|
|
|
return file.getOriginalFilename();
|
|
|
@@ -44,7 +86,7 @@ public class MinIOUtils {
|
|
|
JSONObject result = JSONObject.parseObject(responseBody);
|
|
|
log.info("upload file result {}", result);
|
|
|
JSONObject dataJsonObject = result.getJSONObject("data");
|
|
|
- FileInfo data = dataJsonObject.getJSONArray("rows").getObject(0,FileInfo.class);
|
|
|
+ FileInfo data = dataJsonObject.getJSONArray("rows").getObject(0, FileInfo.class);
|
|
|
return data;
|
|
|
}
|
|
|
}
|