|
|
@@ -35,6 +35,7 @@ import com.yhlxj.service.common.EnvCommonService;
|
|
|
import com.yhlxj.service.midjourney.MidjourneyAccountService;
|
|
|
import com.yhlxj.service.midjourney.MidjourneyService;
|
|
|
import com.yhlxj.service.midjourney.MidjourneyUserSettingsService;
|
|
|
+import com.yhlxj.util.COSUtil;
|
|
|
import com.yhlxj.web.wss.WssSession;
|
|
|
import lombok.Data;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
@@ -51,6 +52,7 @@ import javax.imageio.stream.ImageInputStream;
|
|
|
import java.awt.image.BufferedImage;
|
|
|
import java.io.File;
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
import java.net.MalformedURLException;
|
|
|
import java.net.URL;
|
|
|
import java.nio.charset.Charset;
|
|
|
@@ -193,6 +195,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
|
|
|
public MidjourneyUserConversation saveConversation(Long userId,Integer mode,SubmitResult result, String action,String prompt, String botType, Long apiChannelId) throws Exception {
|
|
|
+ if (result.getCode() == 23) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
Long taskId = Long.parseLong(result.getResult());
|
|
|
Map<String, Object> properties = result.getProperties();
|
|
|
MidjourneyUserConversation conversation = null;
|
|
|
@@ -393,6 +398,7 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
* 恢复次数
|
|
|
*/
|
|
|
public void recoverUserLimit(Long id,Integer mode,Long num){
|
|
|
+ log.info("恢复次数 id:{},mode:{},num:{}",id,mode,num);
|
|
|
if (mode == 1){
|
|
|
redisService.incr(RedisService.key.MIDJOURNEY_FAST_LIMIT.getName() + id, 1L);
|
|
|
midjourneyUserMapper.incrFastNum(id, 1);
|
|
|
@@ -544,6 +550,9 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
}
|
|
|
SubmitResult submitResult = submit2MjProxy(url, token, mode, action, param);
|
|
|
int code = submitResult.getCode();
|
|
|
+ if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
+ submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
+ }
|
|
|
if (code == 23){
|
|
|
recoverUserLimit(user.getId(), mode,1L);
|
|
|
return submitResult;
|
|
|
@@ -551,9 +560,6 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
if (code != 1 && code != 21 && code != 22) {
|
|
|
submitResult = checkResult(user, mode, action, instanceId, param, code, accountWithMinUsage, submitResult);
|
|
|
}
|
|
|
- if (StringUtils.isNotBlank(accountWithMinUsage) && mode == 2) {
|
|
|
- submitResult.setInstanceId(Long.valueOf(accountWithMinUsage));
|
|
|
- }
|
|
|
return submitResult;
|
|
|
}
|
|
|
|
|
|
@@ -834,7 +840,11 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
conversationMapper.updateById(conversation);
|
|
|
TASK_EXECUTOR.execute(() -> {
|
|
|
try {
|
|
|
- uploadPicToLocal(conversation,"conversation"+dbConversation.getId());
|
|
|
+ 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);
|
|
|
}
|
|
|
@@ -874,18 +884,21 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
|
|
|
|
|
|
private static String uploadPic(String url, String prefix) throws IOException {
|
|
|
+ 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);
|
|
|
}
|
|
|
- Map<String, Object> paramMap = new HashMap<>();
|
|
|
- paramMap.put("file", file);
|
|
|
- String json = HttpUtil.post("https://files.liuliangbang.vip/pic/ups", paramMap);
|
|
|
- log.info("上传图片结果:url:{},json:{}",url, json);
|
|
|
+ InputStream inputStream = FileUtil.getInputStream(file);
|
|
|
+ String contentType = Files.probeContentType(tempFile);
|
|
|
+ String originalFilename = file.getName();
|
|
|
+ String ext = originalFilename.substring(originalFilename.lastIndexOf("."));
|
|
|
+ String result = COSUtil.upLoad(inputStream, ext, contentType);
|
|
|
+ log.info("上传图片结果:url:{},json:{}",url, result);
|
|
|
FileUtil.del(file);
|
|
|
- return new JSONObject(json).getJSONObject("value").getJSONArray("saved").getJSONObject(0).getJSONObject("info").getStr("cdnUrl");
|
|
|
+ return result;
|
|
|
}
|
|
|
|
|
|
private void uploadPicToLocal(MidjourneyUserConversation conversation, String prefix) throws IOException {
|
|
|
@@ -1286,6 +1299,17 @@ public class MidjourneyServiceImpl implements MidjourneyService {
|
|
|
url = new URL(url.getProtocol(), "cdn.mj.liuliangbang.vip", url.getPort(), url.getFile());
|
|
|
conversation.setImageUrl(url.toString());
|
|
|
conversationMapper.insert(conversation);
|
|
|
+ TASK_EXECUTOR.execute(() -> {
|
|
|
+ try {
|
|
|
+ String imageUrl = conversation.getImageUrl();
|
|
|
+ if (StringUtil.isNotBlank(imageUrl)) {
|
|
|
+ conversation.setImageUrl(uploadPic(imageUrl, "conversation"+conversation.getId()));
|
|
|
+ conversationMapper.updateById(conversation);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ log.error("上传图片失败",e);
|
|
|
+ }
|
|
|
+ });
|
|
|
return conversation;
|
|
|
}
|
|
|
|