|
|
@@ -7,11 +7,17 @@ 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.Codec;
|
|
|
+import com.cyksj.common.util.J11HttpC;
|
|
|
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.entity.MidjourneyUser;
|
|
|
@@ -22,12 +28,21 @@ 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.ImageIO;
|
|
|
import javax.imageio.stream.FileImageOutputStream;
|
|
|
-import java.io.IOException;
|
|
|
+import java.awt.image.BufferedImage;
|
|
|
+import java.io.*;
|
|
|
+import java.net.URI;
|
|
|
+import java.net.URLEncoder;
|
|
|
+import java.net.http.HttpResponse;
|
|
|
+import java.nio.charset.StandardCharsets;
|
|
|
import java.nio.file.Files;
|
|
|
import java.nio.file.Path;
|
|
|
+import java.security.MessageDigest;
|
|
|
+import java.time.ZoneOffset;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
import java.util.stream.Collectors;
|
|
|
@@ -52,6 +67,13 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
private final RedisService redisService;
|
|
|
|
|
|
+ private final MidjourneyUserMapper midjourneyUserMapper;
|
|
|
+
|
|
|
+ @Value("${midjourney.url}")
|
|
|
+ private String midjourneyHost;
|
|
|
+
|
|
|
+ private final EnvCommonService envCommonService;
|
|
|
+
|
|
|
private static final GlobalThreadPoolTaskExecutor TASK_EXECUTOR = GlobalThreadPoolTaskExecutor.getInstance();
|
|
|
|
|
|
|
|
|
@@ -97,12 +119,12 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
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()));
|
|
|
- }
|
|
|
+// 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()));
|
|
|
+// }
|
|
|
return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),properties,"BLEND", StringUtils.EMPTY);
|
|
|
}
|
|
|
|
|
|
@@ -141,8 +163,27 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
+ /**
|
|
|
+ * 同步数据库
|
|
|
+ */
|
|
|
+ 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 MidjourneyUserConversation submitAction(MidjourneyUser user, Long taskId, String customId,Long num) throws Exception {
|
|
|
MidjourneyUserConversation conversation = conversationMapper.selectOne(Wrappers.lambdaQuery(MidjourneyUserConversation.class).eq(MidjourneyUserConversation::getTaskId, taskId).last("limit 1"));
|
|
|
if (conversation == null) {
|
|
|
throw BusinessRuntimeException.getInstance("关联任务不存在或已失效");
|
|
|
@@ -157,10 +198,10 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (button.getCustomId().equals(customId)) {
|
|
|
log.info("action customId:{}", customId);
|
|
|
button.setStyle(3);
|
|
|
- if (button.getLabel().contains("Vary") || button.getCustomId().contains("::pan_")
|
|
|
+ if (user.getMode() == 2 && (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::")) {
|
|
|
+ || button.getCustomId().contains("::CustomZoom::"))) {
|
|
|
flag.set(true);
|
|
|
}
|
|
|
}
|
|
|
@@ -178,6 +219,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (flag.get()) {
|
|
|
// 以上操作有弹窗确认,恢复次数
|
|
|
recoverUserLimit(user.getId(), user.getMode(),user.getMjRelaxNum());
|
|
|
+ }else {
|
|
|
+ syncUser(user.getId(), user.getMode(),num);
|
|
|
}
|
|
|
return saveConversation(user.getId(), user.getMode(), Long.parseLong(result.getResult()),result.getProperties(),"ACTION", StringUtils.EMPTY);
|
|
|
}
|
|
|
@@ -188,6 +231,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
param.put("mode", "FAST");
|
|
|
}
|
|
|
String url = (mode == 1 ? FAST_HOST : RELAX_HOST) + 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);
|
|
|
@@ -227,19 +271,8 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
@Override
|
|
|
public List<MidjourneyUserConversation> listConversationByIds(Integer mode, List<Long> ids) throws Exception {
|
|
|
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(() -> {
|
|
|
- try {
|
|
|
- sync(conversation);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new RuntimeException(e);
|
|
|
- }
|
|
|
- });
|
|
|
+ ids.forEach(id ->{
|
|
|
+ list.add((MidjourneyUserConversation) redisService.get(RedisService.key.MIDJOURNEY_CONVERSATION.getName() + id));
|
|
|
});
|
|
|
return list;
|
|
|
}
|
|
|
@@ -252,8 +285,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
.eq(MidjourneyUserConversation::getUserId, conversation.getUserId()).last("limit 1"));
|
|
|
if (dbConversation != null) {
|
|
|
try {
|
|
|
- if (StringUtils.isNotBlank(conversation.getImageUrl())) {
|
|
|
- conversation.setImageUrl(uploadPic(conversation.getImageUrl(), "conversation"+dbConversation.getId()));
|
|
|
+ String imageUrl = conversation.getImageUrl();
|
|
|
+ if (StringUtil.isNotBlank(imageUrl)) {
|
|
|
+ conversation.setImageUrl(uploadPic(imageUrl, "conversation"+dbConversation.getId()));
|
|
|
}
|
|
|
} catch (IOException e) {
|
|
|
log.error("上传图片失败",e);
|
|
|
@@ -274,8 +308,9 @@ 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));
|
|
|
- return result.getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
+ String json = HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap);
|
|
|
+ log.info("上传图片结果:url:{},json:{}",url, json);
|
|
|
+ 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 +328,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;
|
|
|
}
|
|
|
@@ -332,9 +367,16 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
@Override
|
|
|
- public void notifyHook(String json) throws Exception {
|
|
|
+ public void notifyHook(String json) {
|
|
|
log.info("notifyHook:{}", json);
|
|
|
- MidjourneyUserConversation conversation = Jsons.parseObject(json, MidjourneyUserConversation.class);
|
|
|
+ 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);
|