Commit 7e9a1599 by xushaohua

feat:部署

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