Merge branch 'dev/dev_xp' into dev/dev
This commit is contained in:
@@ -3,13 +3,14 @@ package com.ai.da.common.utils;
|
||||
import com.ai.da.common.config.exception.BusinessException;
|
||||
import com.ai.da.mapper.primary.entity.ObjectItem;
|
||||
import io.minio.*;
|
||||
import io.minio.errors.MinioException;
|
||||
import io.minio.errors.*;
|
||||
import io.minio.http.Method;
|
||||
import io.minio.messages.DeleteError;
|
||||
import io.minio.messages.DeleteObject;
|
||||
import io.minio.messages.Item;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
@@ -617,6 +618,59 @@ public class MinioUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public String getImageAsBase64(String path) throws IOException {
|
||||
int index = path.indexOf("/");
|
||||
String bucketName = path.substring(0, index);
|
||||
String fileName = path.substring(index + 1);
|
||||
|
||||
// 检查桶是否存在
|
||||
boolean found = doesObjectExist(bucketName, fileName);
|
||||
if (!found) {
|
||||
throw new IOException("Bucket " + bucketName + " does not exist");
|
||||
}
|
||||
|
||||
try (InputStream stream = minioClient.getObject(
|
||||
GetObjectArgs.builder()
|
||||
.bucket(bucketName)
|
||||
.object(fileName)
|
||||
.build())) {
|
||||
|
||||
byte[] bytes = IOUtils.toByteArray(stream);
|
||||
return Base64.getEncoder().encodeToString(bytes);
|
||||
} catch (ServerException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InsufficientDataException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (ErrorResponseException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InvalidResponseException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (XmlParserException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (InternalException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public void uploadToMinio(byte[] data, String bucket, String objectName, String contentType) /*throws Exception*/ {
|
||||
try {
|
||||
minioClient.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucket)
|
||||
.object(objectName)
|
||||
.stream(new ByteArrayInputStream(data), data.length, -1)
|
||||
.contentType(contentType)
|
||||
.build());
|
||||
} catch (MinioException | IOException | NoSuchAlgorithmException | InvalidKeyException e){
|
||||
log.error("图片上传到minio出错,{}", e.getMessage());
|
||||
throw new BusinessException("file upload exception");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -498,6 +498,8 @@ public class RedisUtil {
|
||||
|
||||
public final static String STRIPE_EXCEPTION_LOG = "StripeException:";
|
||||
|
||||
public final static String ANIMATE_ANYONE_TEMPLATE_ID = "AnimateAnyoneTemplateId:";
|
||||
|
||||
public void batchDeleteKeysWithSamePrefix(String prefix){
|
||||
Set<String> keys = redisTemplate.keys(prefix + "*");
|
||||
assert keys != null;
|
||||
|
||||
92
src/main/java/com/ai/da/common/utils/SendRequestUtil.java
Normal file
92
src/main/java/com/ai/da/common/utils/SendRequestUtil.java
Normal file
@@ -0,0 +1,92 @@
|
||||
package com.ai.da.common.utils;
|
||||
|
||||
import cn.hutool.http.Header;
|
||||
import cn.hutool.http.HttpRequest;
|
||||
import cn.hutool.http.HttpResponse;
|
||||
import cn.hutool.json.JSONObject;
|
||||
import cn.hutool.json.JSONUtil;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
public class SendRequestUtil {
|
||||
|
||||
@Value("${ALIYUN_API_KEY}")
|
||||
private String AliYunAPIKey;
|
||||
@Value("${FREEPIK_API_KEY}")
|
||||
private String freepikAPIKey;
|
||||
|
||||
public String sendAliYunPostAsync(String apiUrl, String requestBody){
|
||||
// 发送POST请求 todo 异常处理
|
||||
HttpResponse execute = HttpRequest.post(apiUrl)
|
||||
.header(Header.AUTHORIZATION, "Bearer " + AliYunAPIKey)
|
||||
.header("X-DashScope-Async", "enable")
|
||||
.header(Header.CONTENT_TYPE, "application/json")
|
||||
.body(requestBody)
|
||||
.timeout(20000) // 设置超时时间20秒
|
||||
.execute();
|
||||
int status = execute.getStatus();
|
||||
if (status == 200){
|
||||
String body = execute.body();
|
||||
JSONObject bodyJson = JSONUtil.parseObj(body);
|
||||
return body;
|
||||
}
|
||||
log.warn("请求失败,状态码为 : {}", status);
|
||||
return null;
|
||||
}
|
||||
|
||||
public static final String FREE_PIK = "https://api.freepik.com/v1/ai/beta/text-to-image/reimagine-flux";
|
||||
public String sendFreepikPost( String requestBody){
|
||||
// 发送POST请求 todo 异常处理
|
||||
HttpResponse execute = HttpRequest.post(FREE_PIK)
|
||||
.header(Header.CONTENT_TYPE, "application/json")
|
||||
.header("x-freepik-api-key", freepikAPIKey)
|
||||
.body(requestBody)
|
||||
.timeout(20000) // 设置超时时间20秒
|
||||
.execute();
|
||||
int status = execute.getStatus();
|
||||
if (status == 200){
|
||||
return execute.body();
|
||||
}
|
||||
log.warn("请求失败,状态码为 : {}", status);
|
||||
return null;
|
||||
}
|
||||
|
||||
public String sendAliYunGet(String fullUrl){
|
||||
// 发送GET请求 todo 异常处理
|
||||
HttpResponse httpResponse = HttpRequest.get(fullUrl)
|
||||
.header(Header.AUTHORIZATION, "Bearer " + AliYunAPIKey)
|
||||
.timeout(20000) // 设置超时时间20秒
|
||||
.execute();
|
||||
int status = httpResponse.getStatus();
|
||||
if (status == 200){
|
||||
return httpResponse.body();
|
||||
}else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public String sendPost(String url, String requestBodyStr){
|
||||
int status;
|
||||
String body;
|
||||
try (HttpResponse execute = HttpRequest.post(url)
|
||||
.header("Content-Type", "application/json") // 必须设置 Content-Type
|
||||
.body(requestBodyStr) // Hutool 会自动处理 JSON 序列化
|
||||
.timeout(120000) // 设置超时(毫秒)
|
||||
.execute()) {
|
||||
|
||||
status = execute.getStatus();
|
||||
body = execute.body();
|
||||
if (status == 200) {
|
||||
return body;
|
||||
}
|
||||
}
|
||||
log.warn("请求失败,状态码为 : {}, body: {}", status, body);
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user