Commit 7e9a1599 by xushaohua

feat:部署

parent 04615de7
......@@ -18,6 +18,10 @@ bin/
*.iws
*.iml
*.ipr
*.jpg
*.png
*.jpg
*.webp
### NetBeans ###
/nbproject/private/
......
......@@ -111,6 +111,20 @@
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.13</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpmime</artifactId>
<version>4.5.13</version>
</dependency>
</dependencies>
<build>
......
......@@ -11,18 +11,9 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
@Api(tags = "任务查询")
@RestController
......
......@@ -47,7 +47,8 @@ public class InMemoryTaskStoreServiceImpl implements TaskStoreService {
TaskStatus status = task.getStatus();
if(TaskStatus.SUCCESS.equals(status) && StrUtil.isNotBlank(task.getImageUrl()) && task.getImageUrl().contains("cdn.discordapp.com")) {
// 将discord地址转换成华为云地址返回
task.setImageUrl(fileUtil.convertFile(task.getImageUrl()));
String imageUrl = task.getImageUrl();
task.setImageUrl(fileUtil.convertFile(imageUrl));
}
try {
if(ObjectUtil.isNotNull(task.getFinishTime())) {
......@@ -59,8 +60,10 @@ public class InMemoryTaskStoreServiceImpl implements TaskStoreService {
if(ObjectUtil.isNotNull(task.getStartTime())) {
task.setStartDate(DateUtil.formatDateTime(new Date(task.getStartTime())));
}
Long costTime = (task.getFinishTime() - task.getStartTime()) / 1000;
task.setCostTime(costTime);
if(ObjectUtil.isNotNull(task.getFinishTime()) && ObjectUtil.isNotNull(task.getStartTime())) {
Long costTime = (task.getFinishTime() - task.getStartTime()) / 1000;
task.setCostTime(costTime);
}
} catch (Exception e) {
log.error("获取任务详情,转换图片异常:{}", key, e);
}
......
package com.github.novicezk.midjourney.util;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
......@@ -7,15 +8,11 @@ import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import springfox.documentation.schema.Maps;
import javax.crypto.spec.OAEPParameterSpec;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.*;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@Slf4j
......@@ -27,48 +24,65 @@ public class FileUtil {
/**
* 将外网地址url转换成华为云地址
*
* @param discordUrl
* @return
*/
public String convertFile(String discordUrl) {
log.info("开始转换图片:{}", discordUrl);
try {
// 在线图片地址转换成base64
URL url = new URL(discordUrl);
InputStream inputStream = url.openStream();
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
// 将图片数据读取到字节数组输出流
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
File file = urlToFile(discordUrl);
if(ObjectUtil.isNull(file)) {
return "";
}
log.info("开始调用转换url接口:{}", discordUrl);
String apiUrl = domain + "/raysserve/v1.0/readArticle/obsUpload";//替换为您的接口URL
HttpRequest httpRequest = HttpUtil.createPost(apiUrl);
httpRequest.setReadTimeout(1000 * 60 * 3);
httpRequest.form("file", file);
HttpResponse execute = httpRequest.execute();
String body = execute.body();
log.info("调用转换url接口返回的数据:{}", body);
String res = Optional.ofNullable(body)
.map(JSONObject::new)
.map(x -> x.getJSONObject("data"))
.map(x -> x.getString("url"))
.orElse("");
return res;
}
// 将字节数组输出流中的图片数据进行 Base64 编码
byte[] imageBytes = outputStream.toByteArray();
String base64Data = Base64.getEncoder().encodeToString(imageBytes);
log.info("下载转换成了base64:{}", discordUrl);
String last = discordUrl.substring(discordUrl.lastIndexOf(".") + 1);
/**
* 在线图片地址转换成File
*
* @param urlStr
* @return
*/
public File urlToFile(String urlStr) {
log.info("开始在线图片地址转换成File:{}", urlStr);
URLConnection conn = null;
File file = null;
try {
URL url = new URL(urlStr);
conn = url.openConnection();
conn.setConnectTimeout(1000 * 60);
String endLast = urlStr.substring(urlStr.lastIndexOf("."));
file = new File("mj" + endLast);
JSONObject jsonObject = new JSONObject();
jsonObject.put("base64Image", base64Data);
jsonObject.put("extension", last);
HttpRequest request = HttpRequest.post(domain + "/raysserve/v1.0/readArticle/obsUploadbase64")
.setConnectionTimeout(1000 * 60 * 10)
.header("accept", "application/json")
.header("content-type", "application/json")
.body(jsonObject.toString(), "application/json;charset=utf-8");
HttpResponse httpResponse = request.execute();
log.info("调用远程上传接口返回:{}", httpResponse.body());
String uploadUrl = Optional.ofNullable(httpResponse.body())
.map(JSONObject::new)
.map(x -> x.getJSONObject("data"))
.map(x -> x.getString("url"))
.orElse("");
return uploadUrl;
} catch (Exception e) {
log.error("将在线图片url转换成base64异常:{}", e);
// try-with-resources语句块确保了InputStream和FileOutputStream在代码块结束时自动关闭,无需手动调用close()方法,
// 我们没有手动关闭URLConnection,但它的资源和连接会在InputStream和FileOutputStream被关闭时自动释放。
// 这是因为在下载过程中,URLConnection的输入流与远程服务器建立了连接,而输出流与本地文件建立了连接。
// 在使用try-with-resources的代码块中,当InputStream和FileOutputStream被关闭时,底层的URLConnection也会被关闭。
try (InputStream inputStream = conn.getInputStream();
FileOutputStream outputStream = new FileOutputStream(file)) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
log.info("在线url地址转换成file文件后的路径:{}", file.getAbsolutePath());
return file;
}
} catch (IOException e) {
log.info("在线url地址转换成file文件异常:{}", urlStr, e);
}
return "";
return null;
}
}
......@@ -33,7 +33,10 @@ spring:
name: midjourney-proxy
profiles:
active: dev
servlet:
multipart:
max-file-size: -1
max-request-size: -1
server:
port: 9090
servlet:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment