|
@@ -9,6 +9,7 @@ import com.backendsys.modules.crt.dao.CrtGenerateImageDao;
|
|
|
import com.backendsys.modules.crt.entity.CrtGenerateImage;
|
|
|
import com.backendsys.modules.sdk.comfyui.enums.TaskStatusEnums;
|
|
|
import com.backendsys.modules.sdk.comfyui.service.ComfyuiSocketService;
|
|
|
+import com.backendsys.modules.sdk.comfyui.utils.ComfyUtil;
|
|
|
import com.backendsys.modules.sse.entity.SseResponse;
|
|
|
import com.backendsys.modules.sse.entity.SseResponseEnum;
|
|
|
import com.backendsys.modules.sse.utils.SseUtil;
|
|
@@ -38,6 +39,9 @@ public class ComfyuiSocketServiceImpl implements ComfyuiSocketService {
|
|
|
|
|
|
@Autowired
|
|
|
private SseUtil sseUtil;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private ComfyUtil comfyUtil;
|
|
|
@Autowired
|
|
|
private SysFileService sysFileService;
|
|
|
@Autowired
|
|
@@ -142,32 +146,50 @@ public class ComfyuiSocketServiceImpl implements ComfyuiSocketService {
|
|
|
|
|
|
JSONObject data = JSONUtil.parseObj(message);
|
|
|
|
|
|
- if (is_save) {
|
|
|
- // == [任务执行完成] =======================================================
|
|
|
- // { "type": "executed", .. }
|
|
|
- String type = Convert.toStr(data.get("type"));
|
|
|
- if (TaskStatusEnums.EXECUTED.getValue().equals(type)) {
|
|
|
-
|
|
|
- JSONObject dataChildren = JSONUtil.parseObj(data.get("data"));
|
|
|
- JSONObject output = JSONUtil.parseObj(dataChildren.get("output"));
|
|
|
-
|
|
|
- // -- [生成图片 -> 转存图片 -> 新增记录] ------------------------------------------------------
|
|
|
- // 由于图片地址不是公开的,需要加 Token 访问,因此不能公开返回原始图片地址,比如:
|
|
|
- // http://43.128.1.201:8000/api/view?filename=fenjing_1_00012_.png&token=$2b$12$.MR4qGaFetN1FPQzbfyIrehsyjnPJ12xAZhR/l7KZpLkUPQTCG4gy
|
|
|
- // 因此,直接在 ComfyUI 的物理目录上构建 nginx 静态资源访问目录
|
|
|
- Object imagesObj = output.get("images");
|
|
|
- if (imagesObj != null) {
|
|
|
- String prompt_id = Convert.toStr(dataChildren.get("prompt_id"));
|
|
|
- Long drama_project_storyboard_id = Convert.toLong(params.get("drama_project_storyboard_id"));
|
|
|
- List<String> images_path = imagesToStoryboard(user_id, prompt_id, port, imagesObj, drama_project_storyboard_id);
|
|
|
- output.put("images_path", images_path);
|
|
|
- dataChildren.put("output", output);
|
|
|
- data.put("data", dataChildren);
|
|
|
+
|
|
|
+ // == [任务执行完成] =======================================================
|
|
|
+ // { "type": "executed", .. }
|
|
|
+ String type = Convert.toStr(data.get("type"));
|
|
|
+ if (TaskStatusEnums.EXECUTED.getValue().equals(type)) {
|
|
|
+
|
|
|
+ JSONObject dataChildren = JSONUtil.parseObj(data.get("data"));
|
|
|
+ JSONObject output = JSONUtil.parseObj(dataChildren.get("output"));
|
|
|
+
|
|
|
+ // -- [生成图片 -> 转存图片 -> 新增记录] ------------------------------------------------------
|
|
|
+ // 由于图片地址不是公开的,需要加 Token 访问,因此不能公开返回原始图片地址,比如:
|
|
|
+ // http://43.128.1.201:8000/api/view?filename=fenjing_1_00012_.png&token=$2b$12$.MR4qGaFetN1FPQzbfyIrehsyjnPJ12xAZhR/l7KZpLkUPQTCG4gy
|
|
|
+ // 因此,直接在 ComfyUI 的物理目录上构建 nginx 静态资源访问目录
|
|
|
+ Object imagesObj = output.get("images");
|
|
|
+ if (imagesObj != null) {
|
|
|
+ String prompt_id = Convert.toStr(dataChildren.get("prompt_id"));
|
|
|
+ Long drama_project_storyboard_id = Convert.toLong(params.get("drama_project_storyboard_id"));
|
|
|
+
|
|
|
+ // 临时图片相对路径
|
|
|
+ JSONArray images = JSONUtil.parseArray(imagesObj);
|
|
|
+ // 返回完整的临时图片路径
|
|
|
+ List<String> images_temp_path_full = imagesToFullpath(images, port);
|
|
|
+ System.out.println("images_temp_path_full = " + images_temp_path_full);
|
|
|
+ output.put("images_temp_path", images_temp_path_full);
|
|
|
+
|
|
|
+ if (is_save) {
|
|
|
+ // 创建一个 CompletableFuture 来执行异步任务
|
|
|
+ CompletableFuture.runAsync(() -> {
|
|
|
+ // 返回转存后的图片路径
|
|
|
+ List<String> images_path = imagesToStoryboard(user_id, prompt_id, images_temp_path_full, drama_project_storyboard_id);
|
|
|
+ output.put("images_path", images_path);
|
|
|
+ });
|
|
|
}
|
|
|
|
|
|
+ dataChildren.put("output", output);
|
|
|
+ data.put("data", dataChildren);
|
|
|
+
|
|
|
+ // [db] 新增任务记录
|
|
|
+ comfyUtil.asyncUpdateTaskRecord(prompt_id, JSONUtil.toJsonStr(dataChildren));
|
|
|
+
|
|
|
}
|
|
|
- // ========================================================================
|
|
|
+
|
|
|
}
|
|
|
+ // ========================================================================
|
|
|
|
|
|
sseUtil.send(user_id, new SseResponse(SseResponseEnum.COMFYUI, data).toJsonStr());
|
|
|
})
|
|
@@ -218,41 +240,51 @@ public class ComfyuiSocketServiceImpl implements ComfyuiSocketService {
|
|
|
return Flux.fromIterable(sessions.keySet());
|
|
|
}
|
|
|
|
|
|
- // 转存图片
|
|
|
- private List<String> imagesToStoryboard(Long user_id, String prompt_id, Integer port, Object imagesObj, Long drama_project_storyboard_id) {
|
|
|
- JSONArray images = JSONUtil.parseArray(imagesObj);
|
|
|
+ // 返回临时图片完整路径
|
|
|
+ private List<String> imagesToFullpath(JSONArray images, Integer port) {
|
|
|
List<String> images_path = new ArrayList<>();
|
|
|
if (images.size() > 0) {
|
|
|
for (int i = 0; i < images.size(); i++) {
|
|
|
JSONObject image = images.getJSONObject(i);
|
|
|
String filename = image.getStr("filename");
|
|
|
- String filepath = "http://o.daogu.ai/" + port + "/" + filename; // 生成图片临时目录
|
|
|
+ String filepath = "http://o.daogu.ai/" + port + "/" + filename;
|
|
|
+ images_path.add(filepath);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return images_path;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 转存图片
|
|
|
+ private List<String> imagesToStoryboard(Long user_id, String prompt_id, List<String> images_temp_path_full, Long drama_project_storyboard_id) {
|
|
|
+
|
|
|
+ List<String> images_path = new ArrayList<>();
|
|
|
+ if (images_temp_path_full.size() > 0) {
|
|
|
+
|
|
|
+ images_temp_path_full.stream().forEach(temp_image -> {
|
|
|
|
|
|
// -- [图片转存储存桶] -------------------------------------
|
|
|
- SysFileResult result = sysFileService.urlToUploadFile(filepath, user_id);
|
|
|
+ SysFileResult result = sysFileService.urlToUploadFile(temp_image, user_id);
|
|
|
System.out.println("urlToUploadFile (result) = " + result);
|
|
|
|
|
|
+ String image = result.getUrl();
|
|
|
+ images_path.add(image);
|
|
|
+
|
|
|
// -- [记录到生成图片记录表] --------------------------------
|
|
|
- // 创建一个 CompletableFuture 来执行异步任务
|
|
|
- CompletableFuture.runAsync(() -> {
|
|
|
-
|
|
|
- if (drama_project_storyboard_id != null) {
|
|
|
- CrtGenerateImage entity = new CrtGenerateImage();
|
|
|
- entity.setUser_id(user_id);
|
|
|
- entity.setDrama_project_storyboard_id(drama_project_storyboard_id);
|
|
|
- entity.setPrompt_id(prompt_id);
|
|
|
- entity.setUrl_origin(filepath);
|
|
|
- entity.setUrl(result.getUrl());
|
|
|
- entity.setTarget(result.getTarget());
|
|
|
- crtGenerateImageDao.insert(entity);
|
|
|
- }
|
|
|
-
|
|
|
- });
|
|
|
+ if (drama_project_storyboard_id != null) {
|
|
|
+ CrtGenerateImage entity = new CrtGenerateImage();
|
|
|
+ entity.setUser_id(user_id);
|
|
|
+ entity.setDrama_project_storyboard_id(drama_project_storyboard_id);
|
|
|
+ entity.setPrompt_id(prompt_id);
|
|
|
+ entity.setUrl_origin(temp_image);
|
|
|
+ entity.setUrl(image);
|
|
|
+ entity.setTarget(result.getTarget());
|
|
|
+ entity.setObject_key(result.getKey());
|
|
|
+ crtGenerateImageDao.insert(entity);
|
|
|
+ }
|
|
|
// ------------------------------------------------------
|
|
|
|
|
|
- images_path.add(filepath);
|
|
|
+ });
|
|
|
|
|
|
- }
|
|
|
}
|
|
|
return images_path;
|
|
|
}
|