|
|
@@ -52,6 +52,7 @@ import javax.imageio.stream.FileImageOutputStream;
|
|
|
import javax.imageio.stream.ImageInputStream;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
+import java.io.FileOutputStream;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
import java.net.MalformedURLException;
|
|
|
@@ -82,7 +83,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
private static final String RELAX_TOKEN = "5a9a3e9b-5a5e-4b6b-9a5a-9a5a5a5a5a5a";
|
|
|
|
|
|
- private static final Map<Long, ImgSize> TASK_IMG_SIZE_MAP = new ConcurrentHashMap<>(64);
|
|
|
+ private static final Map<Long, ImgSize> TASK_IMG_SIZE_MAP = new ConcurrentHashMap<>();
|
|
|
|
|
|
private final MidjourneyUserConversationMapper conversationMapper;
|
|
|
|
|
|
@@ -842,17 +843,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
conversation.setId(dbConversation.getId());
|
|
|
conversationMapper.updateById(conversation);
|
|
|
- TASK_EXECUTOR.execute(() -> {
|
|
|
- try {
|
|
|
- String imageUrl = conversation.getImageUrl();
|
|
|
- if (StringUtil.isNotBlank(imageUrl) && "SUCCESS".equals(conversation.getStatus())) {
|
|
|
- conversation.setImageUrl(uploadPic(imageUrl, "conversation"+dbConversation.getId()));
|
|
|
- conversationMapper.updateById(conversation);
|
|
|
- }
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("上传图片失败",e);
|
|
|
- }
|
|
|
- });
|
|
|
//失败返还次数
|
|
|
if ("FAILURE".equals(conversation.getStatus()) && !dbConversation.getStatus().equals("MODEL")){
|
|
|
if (StringUtils.equals("未知频道",conversation.getFailReason())) {
|
|
|
@@ -888,25 +878,35 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
|
private static String uploadPic(String url, String prefix) throws IOException {
|
|
|
+ // 替换 URL 并下载图像字节
|
|
|
url = url.replace("cdn.mj.liuliangbang.vip", "cdn.discordapp.com");
|
|
|
byte[] body = HttpUtil.downloadBytes(url);
|
|
|
+
|
|
|
+ // 创建临时文件并将图像字节写入文件
|
|
|
Path tempFile = Files.createTempFile(prefix, ".png");
|
|
|
- File file = tempFile.toFile();
|
|
|
- try (FileImageOutputStream imageOutput = new FileImageOutputStream(file)) {
|
|
|
- imageOutput.write(body, 0, body.length);
|
|
|
+ try (FileOutputStream fos = new FileOutputStream(tempFile.toFile())) {
|
|
|
+ fos.write(body);
|
|
|
}
|
|
|
+
|
|
|
String result = url;
|
|
|
- try (InputStream inputStream = FileUtil.getInputStream(file)) {
|
|
|
+
|
|
|
+ // 上传图像并处理潜在的异常
|
|
|
+ try (InputStream inputStream = Files.newInputStream(tempFile)) {
|
|
|
String contentType = Files.probeContentType(tempFile);
|
|
|
- String originalFilename = file.getName();
|
|
|
+ String originalFilename = tempFile.getFileName().toString();
|
|
|
String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
|
result = COSUtil.upLoad(inputStream, ext, contentType);
|
|
|
} catch (Exception e) {
|
|
|
log.error("上传图片异常:{}", StringUtil.getErrorText(e));
|
|
|
} finally {
|
|
|
- FileUtil.del(file);
|
|
|
+ try {
|
|
|
+ Files.deleteIfExists(tempFile);
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("删除临时文件异常:{}", StringUtil.getErrorText(e));
|
|
|
+ }
|
|
|
}
|
|
|
- log.info("上传图片结果:url:{},json:{}",url, result);
|
|
|
+
|
|
|
+ log.info("上传图片结果:url:{},json:{}", url, result);
|
|
|
return result;
|
|
|
}
|
|
|
|
|
|
@@ -1297,7 +1297,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
List<MidjourneyUserConversation> imgConversations = conversationMapper.selectList(Wrappers.lambdaQuery(MidjourneyUserConversation.class)
|
|
|
.eq(MidjourneyUserConversation::getStatus, "SUCCESS").notLike(MidjourneyUserConversation::getImageUrl, "cdn.mj.galaxydvd.com")
|
|
|
.ge(MidjourneyUserConversation::getCreatedTime, DateUtil.offsetMinute(new Date(), -60 * 24)).orderByAsc(MidjourneyUserConversation::getId).last(" limit 10"));
|
|
|
- TASK_EXECUTOR.execute(() -> {
|
|
|
for (MidjourneyUserConversation conversation : imgConversations) {
|
|
|
try {
|
|
|
String imageUrl = conversation.getImageUrl();
|
|
|
@@ -1310,7 +1309,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
}
|
|
|
- });
|
|
|
}
|
|
|
|
|
|
@Override
|