|
@@ -14,11 +14,14 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
import com.cyksj.common.util.Jsons;
|
|
|
|
|
+import com.cyksj.common.util.QueryWrapperUtils;
|
|
|
import com.cyksj.common.util.StringUtil;
|
|
import com.cyksj.common.util.StringUtil;
|
|
|
|
|
+import com.yhlxj.dao.mapper.GroupsRelationMapper;
|
|
|
import com.yhlxj.dao.mapper.midjourney.MidjourneyUserConversationMapper;
|
|
import com.yhlxj.dao.mapper.midjourney.MidjourneyUserConversationMapper;
|
|
|
import com.yhlxj.dao.mapper.midjourney.MidjourneyUserMapper;
|
|
import com.yhlxj.dao.mapper.midjourney.MidjourneyUserMapper;
|
|
|
import com.yhlxj.dao.model.dto.BlendDimensions;
|
|
import com.yhlxj.dao.model.dto.BlendDimensions;
|
|
|
import com.yhlxj.dao.model.dto.MessageButton;
|
|
import com.yhlxj.dao.model.dto.MessageButton;
|
|
|
|
|
+import com.yhlxj.dao.model.entity.GroupsRelation;
|
|
|
import com.yhlxj.dao.model.entity.MidjourneyAccount;
|
|
import com.yhlxj.dao.model.entity.MidjourneyAccount;
|
|
|
import com.yhlxj.dao.model.entity.MidjourneyUser;
|
|
import com.yhlxj.dao.model.entity.MidjourneyUser;
|
|
|
import com.yhlxj.dao.model.entity.MidjourneyUserConversation;
|
|
import com.yhlxj.dao.model.entity.MidjourneyUserConversation;
|
|
@@ -36,7 +39,8 @@ import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
|
-import java.io.*;
|
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
|
+import java.io.IOException;
|
|
|
import java.net.MalformedURLException;
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Files;
|
|
@@ -45,6 +49,8 @@ import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.stream.Collectors;
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
|
|
|
|
+import static com.yhlxj.web.wss.MidjourneyServerEndpoint.WSS_SESSION_MAP;
|
|
|
|
|
+
|
|
|
/**
|
|
/**
|
|
|
* @author zwhui
|
|
* @author zwhui
|
|
|
* @date 2024/4/23 15:44
|
|
* @date 2024/4/23 15:44
|
|
@@ -76,6 +82,28 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
private final MidjourneyAccountService midjourneyAccountService;
|
|
private final MidjourneyAccountService midjourneyAccountService;
|
|
|
|
|
|
|
|
|
|
+ private final GroupsRelationMapper groupsRelationMapper;
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public MidjourneyUser getUser(String userToken) {
|
|
|
|
|
+ MidjourneyUser midjourneyUser = midjourneyUserMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUser.class)
|
|
|
|
|
+ .eq(MidjourneyUser::getUserToken, userToken).last("limit 1"));
|
|
|
|
|
+ if (midjourneyUser == null){
|
|
|
|
|
+ throw new BusinessRuntimeException("账号不存在,请重新登录");
|
|
|
|
|
+ }
|
|
|
|
|
+ if(!redisService.hasKey(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId())){
|
|
|
|
|
+ redisService.set(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + midjourneyUser.getId(),midjourneyUser.getMjFastNum());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if(!redisService.hasKey(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId())){
|
|
|
|
|
+ redisService.set(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + midjourneyUser.getId(),midjourneyUser.getMjRelaxNum());
|
|
|
|
|
+ }
|
|
|
|
|
+ Long relationId = midjourneyUser.getRelationId();
|
|
|
|
|
+ GroupsRelation relation = groupsRelationMapper.selectById(relationId);
|
|
|
|
|
+ midjourneyUser.setAqType(relation.getAqType());
|
|
|
|
|
+
|
|
|
|
|
+ return midjourneyUser;
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public MidjourneyUserConversation submitImagine(MidjourneyUser user, String prompt, String botType, List<String> base64Array) throws Exception {
|
|
public MidjourneyUserConversation submitImagine(MidjourneyUser user, String prompt, String botType, List<String> base64Array) throws Exception {
|
|
@@ -176,49 +204,49 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
@Override
|
|
@Override
|
|
|
public MidjourneyUserConversation submitSeed(MidjourneyUser user, Long taskId, String botType) throws Exception {
|
|
public MidjourneyUserConversation submitSeed(MidjourneyUser user, Long taskId, String botType) throws Exception {
|
|
|
- SubmitResult result = seed(user.getMode(),"seed", taskId);
|
|
|
|
|
- return saveConversation(user.getId(), user.getMode(), result,"SEED", StringUtils.EMPTY, botType);
|
|
|
|
|
|
|
+ //SubmitResult result = seed(user.getMode(),"seed", taskId);
|
|
|
|
|
+ //return saveConversation(user.getId(), user.getMode(), result,"SEED", StringUtils.EMPTY, botType);
|
|
|
|
|
+ return null;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- private SubmitResult seed(Integer mode, String seed, Long taskId) {
|
|
|
|
|
- String url = "";
|
|
|
|
|
- if (mode == 1) {
|
|
|
|
|
- url = FAST_HOST;
|
|
|
|
|
- } else if (mode == 2) {
|
|
|
|
|
- url = RELAX_HOST;
|
|
|
|
|
- }
|
|
|
|
|
- url = url + String.format("/mj/task/%d/image-seed", taskId);
|
|
|
|
|
- String body = HttpRequest.get(url).header("Authorization", FAST_TOKEN).execute().body();
|
|
|
|
|
- log.info("seed body:{}", body);
|
|
|
|
|
- SubmitResult submitResult = Jsons.parseObject(body, SubmitResult.class);
|
|
|
|
|
- int code = submitResult.getCode();
|
|
|
|
|
- if (code != 1 && code != 21 && code != 22) {
|
|
|
|
|
- if (code == 3) {
|
|
|
|
|
- if(mode == 2 && StringUtils.isNotBlank(accountWithMinUsage)){
|
|
|
|
|
- redisService.hdel(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),accountWithMinUsage);
|
|
|
|
|
- String finalAccountWithMinUsage = accountWithMinUsage;
|
|
|
|
|
- TASK_EXECUTOR.execute(() -> {
|
|
|
|
|
- MidjourneyAccount account = midjourneyAccountService.getOne(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getInstanceId, finalAccountWithMinUsage).last("limit 1"));
|
|
|
|
|
- midjourneyAccountService.updateStatus(account.getId());
|
|
|
|
|
- });
|
|
|
|
|
- }
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("账号不存在");
|
|
|
|
|
- }
|
|
|
|
|
- if (code == 4) {
|
|
|
|
|
- throw BusinessRuntimeException.getInstance(submitResult.getDescription());
|
|
|
|
|
- }
|
|
|
|
|
- if (code == 24) {
|
|
|
|
|
- JSONObject jsonObject = JSONUtil.parseObj(submitResult.getResult());
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("prompt包含敏感词:"+jsonObject.getStr("bannedWord"));
|
|
|
|
|
- }
|
|
|
|
|
- log.error("action:" + action + " error message:" + submitResult.getDescription());
|
|
|
|
|
- throw BusinessRuntimeException.getInstance("队列已满,请稍后尝试");
|
|
|
|
|
- }
|
|
|
|
|
- if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
|
|
- submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
|
|
- }
|
|
|
|
|
- return submitResult;
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ //private SubmitResult seed(Integer mode, String seed, Long taskId) {
|
|
|
|
|
+ // String url = "";
|
|
|
|
|
+ // if (mode == 1) {
|
|
|
|
|
+ // url = FAST_HOST;
|
|
|
|
|
+ // } else if (mode == 2) {
|
|
|
|
|
+ // url = RELAX_HOST;
|
|
|
|
|
+ // }
|
|
|
|
|
+ // url = url + String.format("/mj/task/%d/image-seed", taskId);
|
|
|
|
|
+ // String body = HttpRequest.get(url).header("Authorization", FAST_TOKEN).execute().body();
|
|
|
|
|
+ // log.info("seed body:{}", body);
|
|
|
|
|
+ // SubmitResult submitResult = Jsons.parseObject(body, SubmitResult.class);
|
|
|
|
|
+ // int code = submitResult.getCode();
|
|
|
|
|
+ // if (code != 1 && code != 21 && code != 22) {
|
|
|
|
|
+ // if (code == 3) {
|
|
|
|
|
+ // if(mode == 2 && StringUtils.isNotBlank(accountWithMinUsage)){
|
|
|
|
|
+ // redisService.hdel(RedisService.key.MIDJOURNEY_ACCOUNT.getName(),accountWithMinUsage);
|
|
|
|
|
+ // String finalAccountWithMinUsage = accountWithMinUsage;
|
|
|
|
|
+ // TASK_EXECUTOR.execute(() -> {
|
|
|
|
|
+ // MidjourneyAccount account = midjourneyAccountService.getOne(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getInstanceId, finalAccountWithMinUsage).last("limit 1"));
|
|
|
|
|
+ // midjourneyAccountService.updateStatus(account.getId());
|
|
|
|
|
+ // });
|
|
|
|
|
+ // }
|
|
|
|
|
+ // throw BusinessRuntimeException.getInstance("账号不存在");
|
|
|
|
|
+ // }
|
|
|
|
|
+ // if (code == 4) {
|
|
|
|
|
+ // throw BusinessRuntimeException.getInstance(submitResult.getDescription());
|
|
|
|
|
+ // }
|
|
|
|
|
+ // if (code == 24) {
|
|
|
|
|
+ // JSONObject jsonObject = JSONUtil.parseObj(submitResult.getResult());
|
|
|
|
|
+ // throw BusinessRuntimeException.getInstance("prompt包含敏感词:"+jsonObject.getStr("bannedWord"));
|
|
|
|
|
+ // }
|
|
|
|
|
+ // throw BusinessRuntimeException.getInstance("队列已满,请稍后尝试");
|
|
|
|
|
+ // }
|
|
|
|
|
+ // if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
|
|
+ // submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
|
|
+ // }
|
|
|
|
|
+ // return submitResult;
|
|
|
|
|
+ //}
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
|
* 恢复次数
|
|
* 恢复次数
|
|
@@ -322,7 +350,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.put("instanceId",accountWithMinUsage).build());
|
|
.put("instanceId",accountWithMinUsage).build());
|
|
|
}
|
|
}
|
|
|
url = url + getActionUrl(action);
|
|
url = url + getActionUrl(action);
|
|
|
- param.put("notifyHook",midjourneyHost +(EnvCommonService.active.equals(envCommonService.getEnv()) ? "/8081":"/8082") + "/api/applets/midjourney/notifyHook");
|
|
|
|
|
|
|
+ param.put("notifyHook",midjourneyHost +(EnvCommonService.active.equals(envCommonService.getEnv()) ? "/8083":"/8082") + "/api/applets/midjourney/notifyHook");
|
|
|
String body = HttpRequest.post(url).body(Jsons.toJson(param)).header("Authorization", FAST_TOKEN).execute().body();
|
|
String body = HttpRequest.post(url).body(Jsons.toJson(param)).header("Authorization", FAST_TOKEN).execute().body();
|
|
|
log.info("action body:{}", body);
|
|
log.info("action body:{}", body);
|
|
|
SubmitResult submitResult = Jsons.parseObject(body, SubmitResult.class);
|
|
SubmitResult submitResult = Jsons.parseObject(body, SubmitResult.class);
|
|
@@ -442,10 +470,19 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
public MidjourneyUserConversation getConversationById(Long id){
|
|
public MidjourneyUserConversation getConversationById(Long id){
|
|
|
|
|
|
|
|
//从缓存中查询 该任务是否存在
|
|
//从缓存中查询 该任务是否存在
|
|
|
- MidjourneyUserConversation conversation = (MidjourneyUserConversation)redisService.get(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + id);
|
|
|
|
|
- if(conversation == null){
|
|
|
|
|
- conversation = conversationMapper.selectById(id);
|
|
|
|
|
|
|
+ MidjourneyUserConversation conversation;
|
|
|
|
|
+ Object obj = redisService.get(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + id);
|
|
|
|
|
+ if(obj == null){
|
|
|
|
|
+ conversation = conversationMapper.selectOne(QueryWrapperUtils.buildWrapper((wrapper)->{
|
|
|
|
|
+ wrapper.eq(MidjourneyUserConversation::getTaskId, id);
|
|
|
|
|
+ }));
|
|
|
redisService.set(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + id, conversation, RedisService.key.MIDJOURNEY_CONVERSATION.getTimeout());
|
|
redisService.set(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + id, conversation, RedisService.key.MIDJOURNEY_CONVERSATION.getTimeout());
|
|
|
|
|
+ }else {
|
|
|
|
|
+ try {
|
|
|
|
|
+ conversation = (MidjourneyUserConversation)obj;
|
|
|
|
|
+ }catch (Exception e){
|
|
|
|
|
+ conversation = JSONUtil.parseObj(obj).toBean(MidjourneyUserConversation.class);
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
return conversation;
|
|
return conversation;
|
|
@@ -584,9 +621,12 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
conversation.setTaskId(jsons.getLong("id"));
|
|
conversation.setTaskId(jsons.getLong("id"));
|
|
|
if (StringUtil.isNotBlank(conversation.getImageUrl())) {
|
|
if (StringUtil.isNotBlank(conversation.getImageUrl())) {
|
|
|
URL url = new URL(conversation.getImageUrl());
|
|
URL url = new URL(conversation.getImageUrl());
|
|
|
-
|
|
|
|
|
|
|
+ String scaling = "&quality=lossless&width=350&height=350";
|
|
|
|
|
+ if(url.getFile().contains("webp")){
|
|
|
|
|
+ scaling += "&format=webp";
|
|
|
|
|
+ }
|
|
|
// 使用 Hutool 提取各个部分并构建新 URL
|
|
// 使用 Hutool 提取各个部分并构建新 URL
|
|
|
- String newUrl = URLUtil.toURI(new URL(url.getProtocol(), "cdn.mj.liuliangbang.vip", url.getPort(), url.getFile()) + "?" + url.getQuery()).toString();
|
|
|
|
|
|
|
+ String newUrl = URLUtil.toURI(new URL(url.getProtocol(), "cdn.mj.liuliangbang.vip", url.getPort(), url.getFile() + scaling)).toString();
|
|
|
//
|
|
//
|
|
|
conversation.setImageUrl(newUrl);
|
|
conversation.setImageUrl(newUrl);
|
|
|
}
|
|
}
|
|
@@ -594,9 +634,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
TASK_EXECUTOR.execute(()->{
|
|
TASK_EXECUTOR.execute(()->{
|
|
|
//WebSocket 直接发送 任务状态
|
|
//WebSocket 直接发送 任务状态
|
|
|
- WssSession wssSession = (WssSession) redisService.hget(RedisService.key.MIDJOURNEY_WS_SESSION.getName(), conversation.getTaskId().toString());
|
|
|
|
|
|
|
+ WssSession wssSession = WSS_SESSION_MAP.get(conversation.getUserId());
|
|
|
if (wssSession != null) {
|
|
if (wssSession != null) {
|
|
|
- wssSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_INFO, conversation);
|
|
|
|
|
|
|
+ wssSession.sendMessage(WsMessageTypeEnum.SERVER_TASK_INFO, new JSONObject(conversation));
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|