|
|
@@ -1,31 +1,40 @@
|
|
|
package com.cyksj.service.midjourney.impl;
|
|
|
|
|
|
import cn.hutool.core.collection.CollectionUtil;
|
|
|
+import cn.hutool.core.io.FileUtil;
|
|
|
import cn.hutool.core.map.MapUtil;
|
|
|
import cn.hutool.http.HttpRequest;
|
|
|
import cn.hutool.http.HttpUtil;
|
|
|
import cn.hutool.json.JSONArray;
|
|
|
import cn.hutool.json.JSONObject;
|
|
|
import cn.hutool.json.JSONUtil;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
|
|
+import com.cyksj.common.EnvCommonService;
|
|
|
import com.cyksj.common.exception.BusinessRuntimeException;
|
|
|
import com.cyksj.common.task.GlobalThreadPoolTaskExecutor;
|
|
|
import com.cyksj.common.util.Jsons;
|
|
|
+import com.cyksj.common.util.StringUtil;
|
|
|
import com.cyksj.mapper.MidjourneyUserConversationMapper;
|
|
|
+import com.cyksj.mapper.MidjourneyUserMapper;
|
|
|
import com.cyksj.model.dto.BlendDimensions;
|
|
|
import com.cyksj.model.dto.MessageButton;
|
|
|
+import com.cyksj.model.dto.SubmitModalDTO;
|
|
|
+import com.cyksj.model.entity.MidjourneyAccount;
|
|
|
import com.cyksj.model.entity.MidjourneyUser;
|
|
|
import com.cyksj.model.entity.MidjourneyUserConversation;
|
|
|
import com.cyksj.model.response.SubmitResult;
|
|
|
import com.cyksj.redis.RedisService;
|
|
|
+import com.cyksj.service.midjourney.MidjourneyAccountService;
|
|
|
import com.cyksj.service.midjourney.MidjourneyService;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Service;
|
|
|
|
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.io.*;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
import java.util.*;
|
|
|
@@ -52,97 +61,153 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
private final RedisService redisService;
|
|
|
|
|
|
+ private final MidjourneyUserMapper midjourneyUserMapper;
|
|
|
+
|
|
|
+ @Value("${midjourney.url}")
|
|
|
+ private String midjourneyHost;
|
|
|
+ @Value("${midjourney.cdn}")
|
|
|
+ private String midjourneyCdnHost;
|
|
|
+
|
|
|
+ private final EnvCommonService envCommonService;
|
|
|
+
|
|
|
private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
|
+ private final MidjourneyAccountService midjourneyAccountService;
|
|
|
+
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitImagine(MidjourneyUser user, String prompt, List<String> base64Array) throws Exception {
|
|
|
+ public MidjourneyUserConversation submitImagine(MidjourneyUser user, String prompt,String botType, List<String> base64Array) throws Exception {
|
|
|
Map<String, Object> imagineParam = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("prompt", prompt).put("state", user.getId()).build();
|
|
|
+ .put("prompt", prompt)
|
|
|
+ .put("state", user.getId())
|
|
|
+ .build();
|
|
|
if (CollectionUtil.isNotEmpty(base64Array)) {
|
|
|
imagineParam.put("base64Array", base64Array);
|
|
|
}
|
|
|
- SubmitResult result = submit(user.getMode(),"imagine", imagineParam);
|
|
|
- return saveConversation(user.getId(), user.getMode(),Long.parseLong(result.getResult()),result.getProperties(),"IMAGINE", StringUtils.EMPTY);
|
|
|
+ SubmitResult result = submit(user.getMode(),"imagine", null,imagineParam);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
+ return saveConversation(user.getId(), user.getMode(),result,"IMAGINE", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
- public MidjourneyUserConversation saveConversation(Long userId,Integer mode,Long taskId,Map<String,Object> properties, String action,String prompt) throws Exception {
|
|
|
+ public MidjourneyUserConversation saveConversation(Long userId,Integer mode,SubmitResult result, String action,String prompt, String botType) throws Exception {
|
|
|
+ Long taskId = Long.parseLong(result.getResult());
|
|
|
+ Map<String, Object> properties = result.getProperties();
|
|
|
MidjourneyUserConversation conversation = new MidjourneyUserConversation()
|
|
|
.setUserId(userId).setTaskId(taskId).setAction(action).setPrompt(prompt)
|
|
|
.setMode(mode).setStartTime(System.currentTimeMillis()).setProgress("0%")
|
|
|
- .setStatus("IN_PROGRESS").setTaskId(taskId);
|
|
|
+ .setStatus(result.getCode() == 21 ? "NOT_START" : "IN_PROGRESS").setTaskId(taskId);
|
|
|
if (MapUtil.isNotEmpty(properties)) {
|
|
|
conversation.setProperties(Jsons.toJson(properties));
|
|
|
conversation.setChannelId(properties.get("discordChannelId") == null ? null : Long.parseLong(properties.get("discordChannelId").toString()))
|
|
|
.setInstanceId(properties.get("discordInstanceId") == null ? null : Long.parseLong(properties.get("discordInstanceId").toString()));
|
|
|
}
|
|
|
+ if (conversation.getInstanceId() == null) {
|
|
|
+ conversation.setInstanceId(result.getInstanceId());
|
|
|
+ }
|
|
|
conversationMapper.insert(conversation);
|
|
|
return conversation;
|
|
|
}
|
|
|
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitDescribe(MidjourneyUser user, String base64) throws Exception {
|
|
|
- Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>()).put("state", user.getId()).put("base64", base64).build();
|
|
|
- SubmitResult result = submit(user.getMode(),"describe", param);
|
|
|
- return saveConversation(user.getId(), user.getMode(),Long.parseLong(result.getResult()),result.getProperties(),"DESCRIBE", StringUtils.EMPTY);
|
|
|
+ public MidjourneyUserConversation submitDescribe(MidjourneyUser user, String botType, String base64) throws Exception {
|
|
|
+ Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
+ .put("state", user.getId())
|
|
|
+ .put("base64", base64)
|
|
|
+ .build();
|
|
|
+ SubmitResult result = submit(user.getMode(),"describe", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
+ return saveConversation(user.getId(), user.getMode(),result,"DESCRIBE", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitBlend(MidjourneyUser user, BlendDimensions dimensions, List<String> base64Array) throws Exception {
|
|
|
+ public MidjourneyUserConversation submitBlend(MidjourneyUser user, BlendDimensions dimensions, String botType, List<String> base64Array) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("base64Array", base64Array).put("state", user.getId()).build();
|
|
|
+ .put("base64Array", base64Array)
|
|
|
+ .put("state", user.getId())
|
|
|
+ .build();
|
|
|
if (dimensions != null) {
|
|
|
param.put("dimensions", dimensions);
|
|
|
}
|
|
|
- SubmitResult result = submit(user.getMode(),"blend", param);
|
|
|
- Map<String, Object> properties = result.getProperties();
|
|
|
- if (user.getMode() == 1) {
|
|
|
- String finalPrompt = "%s --ar %s --style raw --s 250";
|
|
|
- List<String> picList = uploadBase64Pic(base64Array);
|
|
|
- String pics = picList.stream().map(pic -> "<" + pic + ">").collect(Collectors.joining(" "));
|
|
|
- properties.put("finalPrompt", String.format(finalPrompt, pics,dimensions.getValue()));
|
|
|
+ SubmitResult result = submit(user.getMode(),"blend", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
}
|
|
|
- return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),properties,"BLEND", StringUtils.EMPTY);
|
|
|
+ return saveConversation(user.getId(), user.getMode(),result,"BLEND", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
public MidjourneyUserConversation submitModal(MidjourneyUser user, Long taskId, String prompt, String maskBase64) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("taskId", taskId).put("state", user.getId()).build();
|
|
|
+ .put("taskId", taskId)
|
|
|
+ .put("state", user.getId())
|
|
|
+ .build();
|
|
|
if (StringUtils.isNotBlank(prompt)) {
|
|
|
param.put("prompt", prompt);
|
|
|
}
|
|
|
if (StringUtils.isNotBlank(maskBase64)) {
|
|
|
param.put("maskBase64", maskBase64);
|
|
|
}
|
|
|
- SubmitResult result = submit(user.getMode(),"modal", param);
|
|
|
- return saveConversation(user.getId(), user.getMode(),Long.parseLong(result.getResult()),result.getProperties(),"MODAL", StringUtils.EMPTY);
|
|
|
+ SubmitResult result = submit(user.getMode(),"modal", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
+ return saveConversation(user.getId(), user.getMode(),result,"MODAL", StringUtils.EMPTY, StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitShorten(MidjourneyUser user, String prompt) throws Exception {
|
|
|
+ public MidjourneyUserConversation submitShorten(MidjourneyUser user, String botType, String prompt) throws Exception {
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("prompt", prompt).put("state", user.getId()).build();
|
|
|
- SubmitResult result = submit(user.getMode(),"shorten", param);
|
|
|
- return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),result.getProperties(),"SHORTEN", StringUtils.EMPTY);
|
|
|
+ .put("prompt", prompt)
|
|
|
+ .put("state", user.getId())
|
|
|
+ .build();
|
|
|
+ SubmitResult result = submit(user.getMode(),"shorten", null, param);
|
|
|
+ if (user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
+ return saveConversation(user.getId(), user.getMode(), result,"SHORTEN", StringUtils.EMPTY, botType);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 恢复次数
|
|
|
*/
|
|
|
- public void recoverUserLimit(Long id,Integer mode,Integer num){
|
|
|
+ public void recoverUserLimit(Long id,Integer mode,Long num){
|
|
|
if (mode == 1){
|
|
|
redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + id, 1L);
|
|
|
+ midjourneyUserMapper.incrFastNum(id, 1);
|
|
|
}
|
|
|
if (mode == 2){
|
|
|
- if (num != null) {
|
|
|
+ if (num != null){
|
|
|
redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + id, 1L);
|
|
|
+ midjourneyUserMapper.incrRelaxNum(id, 1);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+// /**
|
|
|
+// * 同步数据库
|
|
|
+// */
|
|
|
+// public void syncUser(Long id,Integer mode,Long num){
|
|
|
+// log.info("同步次数 id:{},mode:{},num:{}",id,mode,num);
|
|
|
+// if (num == null){
|
|
|
+// return;
|
|
|
+// }
|
|
|
+// LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
+// .eq(MidjourneyUser::getId, id);
|
|
|
+// if (mode == 1){
|
|
|
+// wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
+// }
|
|
|
+// if (mode == 2){
|
|
|
+// wrapper.set(MidjourneyUser::getMjRelaxNum, num);
|
|
|
+// }
|
|
|
+// midjourneyUserMapper.update(null, wrapper);
|
|
|
+// }
|
|
|
+
|
|
|
@Override
|
|
|
- public MidjourneyUserConversation submitAction(MidjourneyUser user, Long taskId, String customId) throws Exception {
|
|
|
+ public Object submitAction(MidjourneyUser user, Long taskId, String customId, Long num, String botType) throws Exception {
|
|
|
MidjourneyUserConversation conversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, taskId).last("limit 1"));
|
|
|
if (conversation == null) {
|
|
|
throw BusinessRuntimeException.getInstance("关联任务不存在或已失效");
|
|
|
@@ -150,63 +215,129 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (!user.getMode().equals(conversation.getMode())) {
|
|
|
throw BusinessRuntimeException.getInstance("当前出图模式与关联任务出图模式不符");
|
|
|
}
|
|
|
- AtomicBoolean flag = new AtomicBoolean(false);
|
|
|
+ AtomicBoolean modalFlag = new AtomicBoolean(false);
|
|
|
if (StringUtils.isNotBlank(conversation.getButtons())){
|
|
|
List<MessageButton> messageButtons = Jsons.parseList(conversation.getButtons(), MessageButton.class);
|
|
|
messageButtons.forEach(button -> {
|
|
|
if (button.getCustomId().equals(customId)) {
|
|
|
log.info("action customId:{}", customId);
|
|
|
button.setStyle(3);
|
|
|
- if (button.getLabel().contains("Vary") || button.getCustomId().contains("::pan_")
|
|
|
- || button.getEmoji().equals("🔄") || button.getCustomId().contains("PromptAnalyzer:")
|
|
|
- || button.getCustomId().contains("PicReader::") || button.getCustomId().contains("::variation::")
|
|
|
- || button.getCustomId().contains("::CustomZoom::")) {
|
|
|
- flag.set(true);
|
|
|
+ if (!"Vary (Region)".equals(button.getLabel()) && !"Custom Zoom".equals(button.getLabel())) {
|
|
|
+ modalFlag.set(true);
|
|
|
}
|
|
|
}
|
|
|
});
|
|
|
conversation.setButtons(Jsons.toJson(messageButtons));
|
|
|
}
|
|
|
- conversation.setBookmark(customId.contains("BOOKMARK"));
|
|
|
- conversationMapper.updateById(conversation);
|
|
|
- if (conversation.getBookmark()) {
|
|
|
- return conversation;
|
|
|
+ if (customId.contains("BOOKMARK")) {
|
|
|
+ conversation.setBookmark(true);
|
|
|
+ conversationMapper.updateById(conversation);
|
|
|
+ return null;
|
|
|
}
|
|
|
Map<String, Object> param = MapUtil.builder(new HashMap<String,Object>())
|
|
|
- .put("taskId", taskId).put("state", user.getId()).put("customId", customId).build();
|
|
|
- SubmitResult result = submit(user.getMode(),"action", param);
|
|
|
- if (flag.get()) {
|
|
|
- // 以上操作有弹窗确认,恢复次数
|
|
|
- recoverUserLimit(user.getId(), user.getMode(),user.getMjRelaxNum());
|
|
|
+ .put("taskId", taskId)
|
|
|
+ .put("state", user.getId())
|
|
|
+ .put("customId", customId)
|
|
|
+ .build();
|
|
|
+ SubmitResult result = submit(user.getMode(),"action", conversation.getInstanceId(),param);
|
|
|
+ if (result.getCode() == 21) {
|
|
|
+ if (user.getMode() == 2 && modalFlag.get()){
|
|
|
+ MidjourneyUserConversation modal = submitModal(user, Long.valueOf(result.getResult()), conversation.getPrompt() + " --v 6", null);
|
|
|
+ log.info("action-modal:{}", modal);
|
|
|
+ return modal;
|
|
|
+ }else {
|
|
|
+ // 以上操作有弹窗确认,恢复次数
|
|
|
+ recoverUserLimit(user.getId(), user.getMode(),num);
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ //syncUser(user.getId(), user.getMode(),num);
|
|
|
+ if (!customId.contains("upsample") && user.getMode() == 2) {
|
|
|
+ redisService.hincr(RedisService.key.MIDJOURNEY_ACCOUNT.getName(), result.getInstanceId().toString(), 1.0);
|
|
|
+ }
|
|
|
}
|
|
|
- return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),result.getProperties(),"ACTION", StringUtils.EMPTY);
|
|
|
+ saveConversation(user.getId(), user.getMode(), result,"ACTION", StringUtils.EMPTY, botType);
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
|
|
|
- public SubmitResult submit(Integer mode,String action, Map<String, Object> param) throws Exception {
|
|
|
+ public SubmitResult submit(Integer mode,String action,Long instanceId, Map<String, Object> param) throws Exception {
|
|
|
+ String url = "";
|
|
|
+ String accountWithMinUsage = "";
|
|
|
if (mode == 1) {
|
|
|
param.put("mode", "FAST");
|
|
|
+ url = FAST_HOST;
|
|
|
+ } else if (mode == 2) {
|
|
|
+ url = RELAX_HOST;
|
|
|
+ //慢速查询在使用次数最少的账号
|
|
|
+ accountWithMinUsage = getAccountWithMinUsage(instanceId);
|
|
|
+ param.put("accountFilter",MapUtil.builder(new HashMap<String,Object>())
|
|
|
+ .put("instanceId",accountWithMinUsage).build());
|
|
|
}
|
|
|
- String url = (mode == 1 ? FAST_HOST : RELAX_HOST) + getActionUrl(action);
|
|
|
+ url = url + getActionUrl(action);
|
|
|
+ param.put("notifyHook",midjourneyHost +(EnvCommonService.active.equals(envCommonService.getEnv()) ? "/8081":"/8082") + "/api/applets/midjourney/notifyHook");
|
|
|
String body = HttpRequest.post(url).body(Jsons.toJson(param)).header("Authorization", FAST_TOKEN).execute().body();
|
|
|
log.info("action 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("图片重复");
|
|
|
+ throw BusinessRuntimeException.getInstance(submitResult.getDescription());
|
|
|
}
|
|
|
if (code == 24) {
|
|
|
- throw BusinessRuntimeException.getInstance("prompt包含敏感词");
|
|
|
+ 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;
|
|
|
}
|
|
|
+
|
|
|
+ public String getAccountWithMinUsage(Long instanceId) {
|
|
|
+ String key = RedisService.key.MIDJOURNEY_ACCOUNT.getName();
|
|
|
+ try {
|
|
|
+ // 获取所有账号ID和使用次数
|
|
|
+ Map<Object, Object> accountsUsage = redisService.hmget(key);
|
|
|
+
|
|
|
+ if (accountsUsage == null || accountsUsage.isEmpty()) {
|
|
|
+ accountsUsage = midjourneyAccountService.list(Wrappers.lambdaQuery(MidjourneyAccount.class).eq(MidjourneyAccount::getStatus,Boolean.TRUE)).stream().collect(Collectors.toMap(k -> k.getInstanceId().toString(), v -> 0));
|
|
|
+ // 保存所有账号ID和使用次数
|
|
|
+ redisService.hmset(key, accountsUsage);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 找到使用次数最少的账号ID
|
|
|
+ String minAccountId = null;
|
|
|
+ if (instanceId != null) {
|
|
|
+ minAccountId = instanceId.toString();
|
|
|
+ }else {
|
|
|
+ int minUsage = Integer.MAX_VALUE;
|
|
|
+ for (Map.Entry<Object, Object> entry : accountsUsage.entrySet()) {
|
|
|
+ int usage = Integer.parseInt(entry.getValue().toString());
|
|
|
+ if (usage < minUsage) {
|
|
|
+ minUsage = usage;
|
|
|
+ minAccountId = entry.getKey().toString();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return minAccountId;
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw BusinessRuntimeException.getInstance("获取账号失败");
|
|
|
+ }
|
|
|
+ }
|
|
|
private String getActionUrl(String action) {
|
|
|
switch (action) {
|
|
|
case "imagine":
|
|
|
@@ -225,21 +356,36 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
}
|
|
|
@Override
|
|
|
- public List<MidjourneyUserConversation> listConversationByIds(Integer mode, List<Long> ids) throws Exception {
|
|
|
+ public List<MidjourneyUserConversation> listConversationByIds(Integer mode, List<Long> ids) {
|
|
|
List<MidjourneyUserConversation> list = new ArrayList<>();
|
|
|
- listByIds(mode,ids).forEach(json -> {
|
|
|
- JSONObject jsons = JSONUtil.parseObj(json);
|
|
|
- MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
|
|
|
- conversation.setUserId(jsons.getLong("state"));
|
|
|
- conversation.setTaskId(jsons.getLong("id"));
|
|
|
- list.add(conversation);
|
|
|
- TASK_EXECUTOR.execute(() -> {
|
|
|
+ ids.forEach(id ->{
|
|
|
+ String queryKey = RedisService.key.MIDJOURNEY_QUERY.getName();
|
|
|
+ Long count = redisService.incr(queryKey + id, 1L);
|
|
|
+ if (count%5 == 0) {
|
|
|
try {
|
|
|
- sync(conversation);
|
|
|
- } catch (IOException e) {
|
|
|
+ listByIds(mode,List.of(id)).forEach(json ->{
|
|
|
+ JSONObject jsons = JSONUtil.parseObj(json);
|
|
|
+ MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
|
|
|
+ if (StringUtil.isNotBlank(conversation.getImageUrl())) {
|
|
|
+ conversation.setImageUrl(conversation.getImageUrl().replace("cdn.discordapp.com", "mj.galaxydvd.com"));
|
|
|
+ }
|
|
|
+ conversation.setUserId(jsons.getLong("state"));
|
|
|
+ conversation.setTaskId(jsons.getLong("id"));
|
|
|
+ list.add(conversation);
|
|
|
+ TASK_EXECUTOR.execute(() -> {
|
|
|
+ try {
|
|
|
+ sync(conversation);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ });
|
|
|
+ } catch (Exception e) {
|
|
|
throw new RuntimeException(e);
|
|
|
}
|
|
|
- });
|
|
|
+ }else {
|
|
|
+ list.add((MidjourneyUserConversation) redisService.get(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + id));
|
|
|
+ }
|
|
|
});
|
|
|
return list;
|
|
|
}
|
|
|
@@ -248,34 +394,61 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
public void sync(MidjourneyUserConversation conversation) throws IOException {
|
|
|
if ((StringUtils.isNotBlank(conversation.getProgress()) && progress.contains(conversation.getProgress())) || status.contains(conversation.getStatus())) {
|
|
|
log.info("同步任务:{},进度:{}",conversation.getTaskId(),conversation.getProgress());
|
|
|
+ Long userId = conversation.getUserId();
|
|
|
MidjourneyUserConversation dbConversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, conversation.getTaskId())
|
|
|
- .eq(MidjourneyUserConversation::getUserId, conversation.getUserId()).last("limit 1"));
|
|
|
+ .eq(MidjourneyUserConversation::getUserId, userId).orderByDesc(MidjourneyUserConversation::getId).last("limit 1"));
|
|
|
if (dbConversation != null) {
|
|
|
- try {
|
|
|
- if (StringUtils.isNotBlank(conversation.getImageUrl())) {
|
|
|
- conversation.setImageUrl(uploadPic(conversation.getImageUrl(), "conversation"+dbConversation.getId()));
|
|
|
+ if ("SUCCESS".equals(dbConversation.getStatus()) || "FAILURE".equals(dbConversation.getStatus()) || "MODAL".equals(dbConversation.getStatus())) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ //失败返还次数
|
|
|
+ if ("FAILURE".equals(conversation.getStatus())){
|
|
|
+ Object num = redisService.get(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + userId);
|
|
|
+ LambdaUpdateWrapper<MidjourneyUser> wrapper = Wrappers.lambdaUpdate(MidjourneyUser.class)
|
|
|
+ .eq(MidjourneyUser::getId, userId);
|
|
|
+ Integer mode = dbConversation.getMode();
|
|
|
+ if (mode == 1){
|
|
|
+ num = redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + userId, 1L);
|
|
|
+ wrapper.set(MidjourneyUser::getMjFastNum, num);
|
|
|
+ }
|
|
|
+ if (mode == 2){
|
|
|
+ if (num != null) {
|
|
|
+ num = redisService.incr(RedisService.key.MIDJOURNEY_RELAX_LIMIT.getName() + userId, 1L);
|
|
|
+ wrapper.set(MidjourneyUser::getMjRelaxNum, num);
|
|
|
+ }
|
|
|
}
|
|
|
- } catch (IOException e) {
|
|
|
- log.error("上传图片失败",e);
|
|
|
- }finally {
|
|
|
- conversation.setId(dbConversation.getId());
|
|
|
- conversationMapper.updateById(conversation);
|
|
|
+ midjourneyUserMapper.update(null, wrapper);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
private static String uploadPic(String url, String prefix) throws IOException {
|
|
|
byte[] body = HttpUtil.downloadBytes(url);
|
|
|
Path tempFile = Files.createTempFile(prefix, ".png");
|
|
|
- try (FileImageOutputStream imageOutput = new FileImageOutputStream(tempFile.toFile())) {
|
|
|
+ File file = tempFile.toFile();
|
|
|
+ try (FileImageOutputStream imageOutput = new FileImageOutputStream(file)) {
|
|
|
imageOutput.write(body, 0, body.length);
|
|
|
}
|
|
|
Map<String, Object> paramMap = new HashMap<>();
|
|
|
- paramMap.put("file", tempFile.toFile());
|
|
|
- JSONObject result = JSONUtil.parseObj(HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap));
|
|
|
- return result.getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
+ paramMap.put("file", file);
|
|
|
+ String json = HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap);
|
|
|
+ log.info("上传图片结果:url:{},json:{}",url, json);
|
|
|
+ FileUtil.del(file);
|
|
|
+ return new JSONObject(json).getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
}
|
|
|
|
|
|
private static List<String> uploadBase64Pic(List<String> base64Array) throws IOException {
|
|
|
@@ -293,8 +466,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
Map<String, Object> paramMap = new HashMap<>();
|
|
|
paramMap.put("file", tempFile.toFile());
|
|
|
- JSONObject result = JSONUtil.parseObj(HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap));
|
|
|
- list.add(result.getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl"));
|
|
|
+ String json = HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap);
|
|
|
+ list.add(new JSONObject(json).getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl"));
|
|
|
}
|
|
|
return list;
|
|
|
}
|
|
|
@@ -331,4 +504,23 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ @Override
|
|
|
+ public void notifyHook(String json) {
|
|
|
+ log.info("notifyHook:{}", json);
|
|
|
+ JSONObject jsons = JSONUtil.parseObj(json);
|
|
|
+ MidjourneyUserConversation conversation = JSONUtil.toBean(jsons, MidjourneyUserConversation.class);
|
|
|
+ conversation.setUserId(jsons.getLong("state"));
|
|
|
+ conversation.setTaskId(jsons.getLong("id"));
|
|
|
+ if (StringUtil.isNotBlank(conversation.getImageUrl())) {
|
|
|
+ conversation.setImageUrl(conversation.getImageUrl().replace("cdn.discordapp.com", "mj.galaxydvd.com"));
|
|
|
+ }
|
|
|
+ redisService.set(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + conversation.getTaskId(), conversation,RedisService.key.MIDJOURNEY_CONVERSATION.getTimeout());
|
|
|
+ TASK_EXECUTOR.execute(() -> {
|
|
|
+ try {
|
|
|
+ sync(conversation);
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw new RuntimeException(e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
}
|