|
|
@@ -14,6 +14,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
+import com.cyksj.common.util.IoKit;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
import com.cyksj.common.util.QueryWrapperUtils;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
@@ -38,6 +39,7 @@ import com.yhlxj.web.wss.WssSession;
|
|
|
import lombok.Data;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
+import net.coobird.thumbnailator.Thumbnails;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
@@ -51,6 +53,7 @@ import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
+import java.nio.charset.Charset;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.util.*;
|
|
|
@@ -159,7 +162,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (CollectionUtil.isNotEmpty(base64Array)) {
|
|
|
imagineParam.put("base64Array", base64Array);
|
|
|
}
|
|
|
- SubmitResult result = submit(user,mode,"imagine", null,imagineParam, null);
|
|
|
+ SubmitResult result = submit(user,mode,"imagine", null,imagineParam, null,false);
|
|
|
if (mode == 2) {
|
|
|
redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
}
|
|
|
@@ -234,7 +237,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("state", user.getId().toString())
|
|
|
.put("base64", base64)
|
|
|
.build();
|
|
|
- SubmitResult result = submit(user,mode,"describe", null, param, null);
|
|
|
+ SubmitResult result = submit(user,mode,"describe", null, param, null,false);
|
|
|
if (mode == 2) {
|
|
|
redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
}
|
|
|
@@ -251,7 +254,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (dimensions != null) {
|
|
|
param.put("dimensions", dimensions);
|
|
|
}
|
|
|
- SubmitResult result = submit(user,mode,"blend", null, param, null);
|
|
|
+ SubmitResult result = submit(user,mode,"blend", null, param, null,false);
|
|
|
if (mode == 2) {
|
|
|
redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
}
|
|
|
@@ -285,7 +288,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
param.put("maskBase64", maskBase64);
|
|
|
}
|
|
|
Long apiChannelId = midjourneyUserConversation == null ? null : midjourneyUserConversation.getApiChannelId();
|
|
|
- SubmitResult result = submit(user,mode,"modal", null, param, apiChannelId);
|
|
|
+ SubmitResult result = submit(user,mode,"modal", null, param, apiChannelId,false);
|
|
|
saveConversation(user.getId(), mode,result,"MODAL", StringUtils.EMPTY, StringUtils.EMPTY,apiChannelId);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
String modeStr;
|
|
|
@@ -320,7 +323,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("prompt", prompt)
|
|
|
.put("state", user.getId().toString())
|
|
|
.build();
|
|
|
- SubmitResult result = submit(user,mode,"shorten", null, param, null);
|
|
|
+ SubmitResult result = submit(user,mode,"shorten", null, param, null,false);
|
|
|
if (mode == 2) {
|
|
|
redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
}
|
|
|
@@ -444,7 +447,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("state", user.getId().toString())
|
|
|
.put("customId", customId)
|
|
|
.build();
|
|
|
- SubmitResult result = submit(user,mode,"action", conversation.getInstanceId(),param, apiChannelId);
|
|
|
+ SubmitResult result = submit(user,mode,"action", conversation.getInstanceId(),param, apiChannelId,false);
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
String modeStr;
|
|
|
String mjVersionStatusKey;
|
|
|
@@ -491,7 +494,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- public SubmitResult submit(MidjourneyUser user, Integer mode, String action, Long instanceId, Map<String, Object> param, Long apiChannelId) throws Exception {
|
|
|
+ public SubmitResult submit(MidjourneyUser user, Integer mode, String action, Long instanceId, Map<String, Object> param, Long apiChannelId,Boolean again) throws Exception {
|
|
|
|
|
|
String accountWithMinUsage = "";
|
|
|
String url = "";
|
|
|
@@ -515,7 +518,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
} else if (mode == 2) {
|
|
|
synchronized (user.getId()){
|
|
|
- userSubmitLimit(user,action,param.get("customId"));
|
|
|
+ userSubmitLimit(user,action,param.get("customId"),again);
|
|
|
}
|
|
|
url = RELAX_HOST;
|
|
|
token = RELAX_TOKEN;
|
|
|
@@ -551,7 +554,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
midjourneyAccountService.updateStatus(account.getId());
|
|
|
});
|
|
|
if(RETRY_ACTION.contains(action)){
|
|
|
- submit(user,mode, action, instanceId, param, null);
|
|
|
+ submit(user,mode, action, instanceId, param, null,true);
|
|
|
}
|
|
|
} else if(mode == 1) {
|
|
|
// 重试逻辑,切换供应商域名
|
|
|
@@ -599,10 +602,15 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
url = url + getActionUrl(action);
|
|
|
String host = midjourneyHost;
|
|
|
- if (mode == 1) {
|
|
|
- host = "https://mj.galaxydvd.com";
|
|
|
+ if (EnvCommonService.active_prd.equals(envCommonService.getEnv())){
|
|
|
+ host = host + "/8082/api/applets/midjourney/notifyHook";
|
|
|
+ if (mode == 2) {
|
|
|
+ host = "http://172.19.0.15:8082/applets/midjourney/notifyHook";
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ host = host + "/8083/api/applets/midjourney/notifyHook";
|
|
|
}
|
|
|
- param.put("notifyHook",EnvCommonService.active.equals(envCommonService.getEnv()) ? host + "/8083/api/applets/midjourney/notifyHook" : host + ":8082/applets/midjourney/notifyHook");
|
|
|
+ param.put("notifyHook",host);
|
|
|
HttpRequest request = HttpRequest.post(url).body(Jsons.toJson(param));
|
|
|
request = mode == 1 ? request.header("Authorization", token) : request.header("mj-api-secret", token);
|
|
|
String body = request.execute().body();
|
|
|
@@ -620,7 +628,10 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
/**
|
|
|
* 用户生图次数限制
|
|
|
*/
|
|
|
- public void userSubmitLimit(MidjourneyUser user,String action,Object customId) {
|
|
|
+ public void userSubmitLimit(MidjourneyUser user,String action,Object customId,Boolean again) {
|
|
|
+ if (again) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
if (StringUtils.equals("modal", action)) {
|
|
|
return;
|
|
|
}
|
|
|
@@ -809,17 +820,13 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
conversation.setId(dbConversation.getId());
|
|
|
conversationMapper.updateById(conversation);
|
|
|
- //TASK_EXECUTOR.execute(() -> {
|
|
|
- // try {
|
|
|
- // String imageUrl = conversation.getImageUrl();
|
|
|
- // if (StringUtil.isNotBlank(imageUrl)) {
|
|
|
- // conversation.setImageUrl(uploadPic(imageUrl, "conversation"+dbConversation.getId()));
|
|
|
- // conversationMapper.updateById(conversation);
|
|
|
- // }
|
|
|
- // } catch (IOException e) {
|
|
|
- // log.error("上传图片失败",e);
|
|
|
- // }
|
|
|
- //});
|
|
|
+ TASK_EXECUTOR.execute(() -> {
|
|
|
+ try {
|
|
|
+ uploadPicToLocal(conversation,"conversation"+dbConversation.getId());
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("上传图片失败",e);
|
|
|
+ }
|
|
|
+ });
|
|
|
//失败返还次数
|
|
|
if ("FAILURE".equals(conversation.getStatus()) && !dbConversation.getStatus().equals("MODEL")){
|
|
|
if (StringUtils.equals("未知频道",conversation.getFailReason())) {
|
|
|
@@ -869,6 +876,35 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
return new JSONObject(json).getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
}
|
|
|
|
|
|
+ private void uploadPicToLocal(MidjourneyUserConversation conversation, String prefix) throws IOException {
|
|
|
+ if (StringUtils.isBlank(conversation.getImageUrl())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ if (!"SUCCESS".equals(conversation.getStatus())){
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String url = conversation.getImageUrl().replace("cdn.mj.liuliangbang.vip", "cdn.discordapp.com");
|
|
|
+ // 上传图片
|
|
|
+ byte[] body = HttpUtil.downloadBytes(url);
|
|
|
+ File originalFile = new File("/mnt/picture", prefix + ".png");
|
|
|
+ try (FileImageOutputStream imageOutput = new FileImageOutputStream(originalFile)) {
|
|
|
+ imageOutput.write(body, 0, body.length);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 生成缩略图
|
|
|
+ File thumbnailFile = new File("/mnt/picture/thumbnail", prefix + ".png");
|
|
|
+ Thumbnails.of(originalFile)
|
|
|
+ .scale(0.3)
|
|
|
+ .toFile(thumbnailFile);
|
|
|
+
|
|
|
+ String originalFileUrl = midjourneyHost +"/picture/"+ originalFile.getName();
|
|
|
+ String thumbnailFileUrl = midjourneyHost +"/picture/thumbnail/"+ thumbnailFile.getName();
|
|
|
+ log.info("上传图片结果:url:{},thumbnailUrl:{}", originalFileUrl, thumbnailFileUrl);
|
|
|
+ conversation.setOriginalImageUrl(originalFileUrl);
|
|
|
+ conversation.setThumbnailImageUrl(thumbnailFileUrl);
|
|
|
+ conversationMapper.updateById(conversation);
|
|
|
+ }
|
|
|
+
|
|
|
private static List<String> uploadBase64Pic(List<String> base64Array) throws IOException {
|
|
|
List<String> list = new ArrayList<>();
|
|
|
for (String base64 : base64Array) {
|
|
|
@@ -958,7 +994,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if(StringUtils.isNotBlank(content)){
|
|
|
jsonObject.set("messageContent","**" + conversation.getPrompt() + "** - <@mj>");
|
|
|
}
|
|
|
- if ("SUBMITTED".equals(conversation.getStatus())){
|
|
|
+ if ("SUBMITTED".equals(conversation.getStatus()) || conversation.getProgressNum() == 0){
|
|
|
MidjourneyUserConversation midjourneyUserConversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, conversation.getTaskId()).last("limit 1"));
|
|
|
String mjVersionStatusKey;
|
|
|
if (midjourneyUserConversation != null){
|