浏览代码

完成转存重构

tsurumure 1 月之前
父节点
当前提交
46fae4180f

+ 0 - 107
src/main/java/com/backendsys/config/WebSocket/WebSocketConfig.java

@@ -1,107 +0,0 @@
-//package com.backendsys.config.WebSocket;
-//
-//import cn.hutool.core.util.StrUtil;
-//import com.backendsys.modules.common.config.security.utils.JwtUtil;
-//
-//import io.jsonwebtoken.Claims;
-//import lombok.RequiredArgsConstructor;
-//import org.springframework.beans.factory.annotation.Autowired;
-//import org.springframework.context.annotation.Configuration;
-//import org.springframework.http.HttpHeaders;
-//import org.springframework.messaging.Message;
-//import org.springframework.messaging.MessageChannel;
-//import org.springframework.messaging.simp.config.ChannelRegistration;
-//import org.springframework.messaging.simp.config.MessageBrokerRegistry;
-//import org.springframework.messaging.simp.stomp.StompCommand;
-//import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
-//import org.springframework.messaging.support.ChannelInterceptor;
-//import org.springframework.messaging.support.MessageHeaderAccessor;
-//import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
-//import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
-//import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
-//
-//@Configuration
-//@EnableWebSocketMessageBroker
-//@RequiredArgsConstructor
-//public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
-//
-//
-//    @Autowired
-//    private JwtUtil jwtUtil;
-//
-//    /**
-//     * 注册一个端点,客户端通过这个端点进行连接
-//     */
-//    @Override
-//    public void registerStompEndpoints(StompEndpointRegistry registry) {
-//        registry
-//                .addEndpoint("/ws")   // 注册了一个 /ws 的端点
-//                .setAllowedOriginPatterns("*") // 允许跨域的 WebSocket 连接
-//                .withSockJS();  // 启用 SockJS (浏览器不支持WebSocket,SockJS 将会提供兼容性支持)
-//    }
-//
-//    /**
-//     * 配置消息代理
-//     */
-//    @Override
-//    public void configureMessageBroker(MessageBrokerRegistry registry) {
-//        // 客户端发送消息的请求前缀
-//        registry.setApplicationDestinationPrefixes("/app");
-//        // 客户端订阅消息的请求前缀,topic一般用于广播推送,queue用于点对点推送
-//        registry.enableSimpleBroker("/topic", "/queue");
-//        // 服务端通知客户端的前缀,可以不设置,默认为user
-//        registry.setUserDestinationPrefix("/user");
-//    }
-//
-//    /**
-//     * 配置客户端入站通道拦截器
-//     * <p>
-//     * 添加 ChannelInterceptor 拦截器,用于在消息发送前,从请求头中获取 token 并解析出用户信息(username),用于点对点发送消息给指定用户
-//     *
-//     * @param registration 通道注册器
-//     */
-//    @Override
-//    public void configureClientInboundChannel(ChannelRegistration registration) {
-//        registration.interceptors(new ChannelInterceptor() {
-//            @Override
-//            public Message<?> preSend(Message<?> message, MessageChannel channel) {
-//
-//                StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, StompHeaderAccessor.class);
-//
-//                // (改) 如果是连接请求(CONNECT 命令),从请求头中取出 token 并设置到认证信息中
-//                if (accessor != null && StompCommand.CONNECT.equals(accessor.getCommand())) {
-//
-//                   // 从连接头中提取授权令牌
-//                   String bearerToken = accessor.getFirstNativeHeader(HttpHeaders.AUTHORIZATION);
-//
-//                   // 验证令牌格式并提取用户信息
-//                   if (StrUtil.isNotBlank(bearerToken) && bearerToken.startsWith("Bearer ")) {
-//                       try {
-//                           // 移除 "Bearer " 前缀,从令牌中提取用户信息(username), 并设置到认证信息中
-//                           String tokenWithoutPrefix = bearerToken.substring(7);
-//
-//                           Claims tokenInfo = jwtUtil.extractAllClaims(tokenWithoutPrefix);
-//                           String username = (String) tokenInfo.get("username");
-//
-//                           if (StrUtil.isNotBlank(username)) {
-//                               accessor.setUser(() -> username);
-//                               return message;
-//                           }
-//
-//                       } catch (Exception e) {
-//                           throw new RuntimeException("Failed to process authentication token.");
-//                       }
-//                   }
-//
-//
-//                }
-//                // 不是连接请求,直接放行
-//
-//
-//
-//                return ChannelInterceptor.super.preSend(message, channel);
-//            }
-//        });
-//    }
-//
-//}

+ 2 - 1
src/main/java/com/backendsys/modules/ai/media/service/impl/MediaTtvServiceImpl.java

@@ -12,6 +12,7 @@ import com.backendsys.modules.ai.media.entity.MediaTtv;
 import com.backendsys.modules.ai.media.entity.MediaTtvConfig;
 import com.backendsys.modules.ai.media.entity.MediaTtvSource;
 import com.backendsys.modules.ai.media.service.MediaTtvService;
+import com.backendsys.modules.common.config.security.utils.SecurityUtil;
 import com.backendsys.modules.sdk.baidu.bce.entity.BaiduBceMediaJob;
 import com.backendsys.modules.sdk.baidu.bce.entity.BaiduBceMediaJobResult;
 import com.backendsys.modules.sdk.baidu.bce.service.BaiduBceMediaService;
@@ -128,7 +129,7 @@ public class MediaTtvServiceImpl implements MediaTtvService {
                 job_status = 1;
 
                 // URL转存文件
-                SysFileResult sysFileResult = sysFileService.urlToUploadFile(response.getVideo_origin_url());
+                SysFileResult sysFileResult = sysFileService.urlToUploadFile(response.getVideo_origin_url(), SecurityUtil.getUserId());
                 String object_key = sysFileResult.getKey();
                 String video_url = sysFileResult.getDomain() + "/" + sysFileResult.getKey();
 

+ 1 - 0
src/main/java/com/backendsys/modules/common/config/security/utils/SecurityUtil.java

@@ -66,6 +66,7 @@ public class SecurityUtil {
      * 获得当前登录 Token
      */
     public static String getToken() {
+        // 如果是在 websocket 周期中,是获取不到 context 的
         Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
         return Convert.toStr(authentication.getDetails());
     }

+ 4 - 1
src/main/java/com/backendsys/modules/crt/service/impl/CrtGenerateServiceImpl.java

@@ -93,6 +93,7 @@ public class CrtGenerateServiceImpl implements CrtGenerateService {
         Integer param_batch_size = storyboardDetail.getParam_batch_size();                        // 生成图片数量
         Float param_prompt_flux_guidance = storyboardDetail.getParam_prompt_flux_guidance();      // 提示词引导系数
         String param_sampler = SamplerEnums.getValueByKey(storyboardDetail.getParam_sampler());   // 采样器
+        Integer param_step = storyboardDetail.getParam_step();
 
         // 随机种子 (默认值:1,范围:(1:随机, 2:自定义))
         Integer param_seed = storyboardDetail.getParam_seed();
@@ -109,6 +110,8 @@ public class CrtGenerateServiceImpl implements CrtGenerateService {
 
         }
 
+
+
         // [db] 获取基础模型 (从项目配置)
         Long model_id = settings_image.getModel_id();
         CrtModel modelDetail = crtModelDao.selectById(model_id);
@@ -460,7 +463,7 @@ public class CrtGenerateServiceImpl implements CrtGenerateService {
                 "  \"150\": {"+
                 "    \"inputs\": {"+
                 "      \"scheduler\": \"normal\","+
-                "      \"steps\": 25,"+
+                "      \"steps\": " + param_step + ","+
                 "      \"denoise\": 1,"+
                 "      \"model\": ["+
                 "        \"152\","+

+ 9 - 8
src/main/java/com/backendsys/modules/sdk/comfyui/controller/ComfyUIDemoController.java

@@ -2,6 +2,7 @@ package com.backendsys.modules.sdk.comfyui.controller;
 
 import com.backendsys.exception.CustException;
 import com.backendsys.modules.common.config.security.annotations.Anonymous;
+import com.backendsys.modules.common.config.security.utils.SecurityUtil;
 import com.backendsys.modules.sdk.comfyui.service.ComfyUIService;
 import com.backendsys.modules.sdk.comfyui.service.ComfyUISocketService;
 import com.backendsys.modules.sdk.tencentcloud.cos.service.TencentCosService;
@@ -38,7 +39,7 @@ public class ComfyUIDemoController {
     @PostMapping("/api/comfyui/ws/connect")
     public String connect(String clientId) {
         comfyUISocketService.connect(clientId, 8000).subscribe();
-        return "Connection initiated for: " + clientId;
+        return "Connection initiated for client_id: " + clientId;
     }
 
     /**
@@ -51,12 +52,12 @@ public class ComfyUIDemoController {
         return "Disconnected: " + clientId;
     }
 
-//    /**
-//     * 转存测试
-//     */
-//    @GetMapping("/api/comfyui/testToCos")
-//    public SysFileResult testToCos(String url) {
-//        return tencentCosService.urlToCOS(url, "png");
-//    }
+    /**
+     * 转存测试
+     */
+    @GetMapping("/api/comfyui/testToCos")
+    public SysFileResult testToCos(String url) {
+        return tencentCosService.urlToCOS(url, "png");
+    }
 
 }

+ 6 - 16
src/main/java/com/backendsys/modules/sdk/comfyui/service/impl/ComfyUISocketServiceImpl.java

@@ -17,6 +17,7 @@ import com.backendsys.modules.sse.entity.SseResponseEnum;
 import com.backendsys.modules.sse.utils.SseUtil;
 import com.backendsys.modules.system.service.SysCommonService;
 import com.backendsys.modules.upload.entity.SysFileResult;
+import com.backendsys.modules.upload.service.SysFileService;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
@@ -43,11 +44,7 @@ public class ComfyUISocketServiceImpl implements ComfyUISocketService {
     @Autowired
     private SseUtil sseUtil;
     @Autowired
-    private SysCommonService sysCommonService;
-    @Autowired
-    private TencentCosService tencentCosService;
-    @Autowired
-    private DouyinTosService douyinTosService;
+    private SysFileService sysFileService;
     @Autowired
     private CrtGenerateImageDao crtGenerateImageDao;
 
@@ -120,6 +117,7 @@ public class ComfyUISocketServiceImpl implements ComfyUISocketService {
 
         CrtGenerateImage entity = new CrtGenerateImage();
 
+        // Websocket 获取不到上下文信息,所以 user_id 需要从外部传递
         Long user_id = SecurityUtil.getUserId();
         entity.setUser_id(user_id);
 
@@ -176,17 +174,8 @@ public class ComfyUISocketServiceImpl implements ComfyUISocketService {
                                             String filepath = "http://o.daogu.ai/" + port + "/" + filename;
 
                                             // -- [图片转存储存桶] -------------------------------------
-                                            SysFileResult result = new SysFileResult();
-                                            Integer UPLOAD_TARGET = Convert.toInt(sysCommonService.getCommonByTag("UPLOAD_TARGET"));
-
-                                            // target: 上传目标 (-1:本地, 1:腾讯云, 2:阿里云, 3.抖音云)
-                                            if (UPLOAD_TARGET == 1) {
-                                                result = tencentCosService.urlToCOS(filepath, "png");
-                                            }
-                                            if (UPLOAD_TARGET == 3) {
-                                                result = douyinTosService.urlToTOS(filepath, "png");
-                                            }
-                                            System.out.println("result = " + result);
+                                            SysFileResult result = sysFileService.urlToUploadFile(filepath, user_id);
+                                            System.out.println("urlToUploadFile (result) = " + result);
 
                                             // -- [记录到生成图片记录表] --------------------------------
                                             if (params != null) {
@@ -200,6 +189,7 @@ public class ComfyUISocketServiceImpl implements ComfyUISocketService {
                                                         entity.setDrama_project_storyboard_id(drama_project_storyboard_id);
                                                         entity.setPrompt_id(prompt_id);
                                                         entity.setUrl_origin(filepath);
+                                                        entity.setUrl(result.getUrl());
                                                         crtGenerateImageDao.insert(entity);
                                                     }
 

+ 1 - 0
src/main/java/com/backendsys/modules/sdk/douyincloud/tos/service/DouyinTosService.java

@@ -38,5 +38,6 @@ public interface DouyinTosService {
     // [抖音云TOS] URL 转存
     SysFileResult urlToTOS(String origin_url);
     SysFileResult urlToTOS(String origin_url, String input_suffix);
+    SysFileResult urlToTOS(String origin_url, String input_suffix, Long user_id);
 
 }

+ 11 - 4
src/main/java/com/backendsys/modules/sdk/douyincloud/tos/service/impl/DouyinTosServiceImpl.java

@@ -60,7 +60,7 @@ public class DouyinTosServiceImpl implements DouyinTosService {
 
 
     // [抖音云COS] 获取进度函数
-    private DataTransferListener getDataTransferListener(String filename) {
+    private DataTransferListener getDataTransferListener(String filename, Long input_user_id) {
 
         return new DataTransferListener() {
             // 自定义实现 DataTransferListener 的 dataTransferStatusChange 接口
@@ -97,7 +97,10 @@ public class DouyinTosServiceImpl implements DouyinTosService {
                 progress.setPercent(percentage);
                 progress.setState(state);
                 String dataStr = (new SseResponse(SseResponseEnum.UPLOAD, progress)).toJsonStr();
-                sseUtil.send(SecurityUtil.getUserId(), dataStr);
+
+                Long user_id = input_user_id == null ? SecurityUtil.getUserId() : input_user_id;
+
+                sseUtil.send(user_id, dataStr);
 
             }
         };
@@ -141,7 +144,7 @@ public class DouyinTosServiceImpl implements DouyinTosService {
             putObjectInput.setOptions(options);
 
             // 自定义实现 DataTransferListener,实现进度条功能
-            DataTransferListener listener = getDataTransferListener(filename);
+            DataTransferListener listener = getDataTransferListener(filename, null);
             putObjectInput.setDataTransferListener(listener);
 
             // 上传对象
@@ -362,6 +365,10 @@ public class DouyinTosServiceImpl implements DouyinTosService {
     }
     @Override
     public SysFileResult urlToTOS(String origin_url, String input_suffix) {
+        return urlToTOS(origin_url, input_suffix);
+    }
+    @Override
+    public SysFileResult urlToTOS(String origin_url, String input_suffix, Long user_id) {
 
         if (StrUtil.isEmpty(origin_url)) throw new CustException("url 不能为空");
 
@@ -396,7 +403,7 @@ public class DouyinTosServiceImpl implements DouyinTosService {
                 putObjectInput.setOptions(options);
 
                 // 自定义实现 DataTransferListener,实现进度条功能
-                DataTransferListener listener = getDataTransferListener(object_filename);
+                DataTransferListener listener = getDataTransferListener(object_filename, user_id);
                 putObjectInput.setDataTransferListener(listener);
 
                 // 上传对象

+ 1 - 0
src/main/java/com/backendsys/modules/sdk/tencentcloud/cos/service/TencentCosService.java

@@ -43,4 +43,5 @@ public interface TencentCosService {
     // [腾讯云COS] URL 转存
     SysFileResult urlToCOS(String url);
     SysFileResult urlToCOS(String url, String input_suffix);
+    SysFileResult urlToCOS(String url, String input_suffix, Long user_id);
 }

+ 10 - 26
src/main/java/com/backendsys/modules/sdk/tencentcloud/cos/service/impl/TencentCosServiceImpl.java

@@ -3,8 +3,6 @@ package com.backendsys.modules.sdk.tencentcloud.cos.service.impl;
 import cn.hutool.core.convert.Convert;
 import cn.hutool.core.util.NumberUtil;
 import cn.hutool.core.util.StrUtil;
-import cn.hutool.crypto.digest.DigestUtil;
-import cn.hutool.http.HttpUtil;
 import com.backendsys.exception.CustException;
 import com.backendsys.modules.common.config.security.utils.SecurityUtil;
 import com.backendsys.modules.common.utils.CommonUtil;
@@ -32,18 +30,14 @@ import org.joda.time.DateTime;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
-import org.springframework.util.StringUtils;
 import org.springframework.web.multipart.MultipartFile;
 
 import java.io.*;
 import java.math.BigDecimal;
 import java.net.URL;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.util.Date;
 import java.util.LinkedList;
 import java.util.List;
-import java.util.Map;
 
 // 图片处理概述
 // https://cloud.tencent.com/document/product/436/42215
@@ -83,24 +77,10 @@ public class TencentCosServiceImpl implements TencentCosService {
     }
 
     // [腾讯云COS][高级接口] 获取进度函数
-    private void showTransferProgress(String filename, Transfer transfer) {
-
-
-
-
-
-
-
-        // 为什么 urlToCos 这里拿不到 Token?
-
-        Long user_id = SecurityUtil.getUserId();
-
-
-
-
-
-
+    private void showTransferProgress(String filename, Transfer transfer, Long input_user_id) {
 
+        Long user_id = input_user_id == null ? SecurityUtil.getUserId() : input_user_id;
+        System.out.println("showTransferProgress (user_id): " + user_id);
 
         // [SSE] 进度回传
         Progress progress = new Progress();
@@ -195,7 +175,7 @@ public class TencentCosServiceImpl implements TencentCosService {
             // 高级上传
             TransferManager transferManager = TencentCosUtil.createTransferManager(cosClient);
             Upload upload = transferManager.upload(putObjectRequest);   // 返回一个异步结果Upload
-            showTransferProgress(filename, upload);                     // 查询上传进度,直到上传结束
+            showTransferProgress(filename, upload, null);    // 查询上传进度,直到上传结束
             UploadResult uploadResult = upload.waitForUploadResult();   // 捕获可能出现的异常
 
             // 自定义返回结果实体
@@ -380,9 +360,12 @@ public class TencentCosServiceImpl implements TencentCosService {
     public SysFileResult urlToCOS(String url) {
         return urlToCOS(url, null);
     }
-
     @Override
     public SysFileResult urlToCOS(String url, String input_suffix) {
+        return urlToCOS(url, input_suffix);
+    }
+    @Override
+    public SysFileResult urlToCOS(String url, String input_suffix, Long user_id) {
 
         if (StrUtil.isEmpty(url)) throw new CustException("url 不能为空");
 
@@ -408,7 +391,7 @@ public class TencentCosServiceImpl implements TencentCosService {
             // 高级上传
             TransferManager transferManager = TencentCosUtil.createTransferManager(cosClient);
             Upload upload = transferManager.upload(putObjectRequest);   // 返回一个异步结果Upload
-            showTransferProgress(object_filename, upload);              // 查询上传进度,直到上传结束
+            showTransferProgress(object_filename, upload, user_id);     // 查询上传进度,直到上传结束
             UploadResult uploadResult = upload.waitForUploadResult();   // 捕获可能出现的异常
 
             SysFileResult result = new SysFileResult();
@@ -428,4 +411,5 @@ public class TencentCosServiceImpl implements TencentCosService {
             if (cosClient != null) cosClient.shutdown();
         }
     }
+
 }

+ 1 - 1
src/main/java/com/backendsys/modules/upload/service/SysFileService.java

@@ -32,7 +32,7 @@ public interface SysFileService extends IService<SysFile> {
     Map<String, Object> updateUploadFileBatch(SysFile sysFile);
 
     // URL转存
-    SysFileResult urlToUploadFile(String origin_url);
+    SysFileResult urlToUploadFile(String origin_url, Long user_id);
 
     // 根据 MD5 获取文件列表 (我的)
     List<Map<String, Object>> getUploadFileListByMd5(SysFile sysFile);

+ 7 - 5
src/main/java/com/backendsys/modules/upload/service/impl/SysFileServiceImpl.java

@@ -444,28 +444,30 @@ public class SysFileServiceImpl extends ServiceImpl<SysFileDao, SysFile> impleme
         return Map.of("ids", ids);
     }
 
-    // URL转存
+    // URL转存 (因为会用到 高级上传 -> 进度监听,需要从外部传入 user_id
     @Override
-    public SysFileResult urlToUploadFile(String origin_url) {
+    public SysFileResult urlToUploadFile(String origin_url, Long input_user_id) {
 
         Integer UPLOAD_TARGET = Convert.toInt(sysCommonService.getCommonByTag("UPLOAD_TARGET"));
 
         SysFileResult sysFileResult = null;
 
+        Long user_id = input_user_id == null ? SecurityUtil.getUserId() : input_user_id;
+
         // target: 上传目标 (-1:本地, 1:腾讯云, 2:阿里云, 3.抖音云)
         if (UPLOAD_TARGET == 1) {
-            sysFileResult = tencentCosService.urlToCOS(origin_url);
+            sysFileResult = tencentCosService.urlToCOS(origin_url, null, user_id);
         }
         // 3: 抖音云
         if (UPLOAD_TARGET == 3) {
-            sysFileResult = douyinTosService.urlToTOS(origin_url);
+            sysFileResult = douyinTosService.urlToTOS(origin_url, null, user_id);
         }
         if (sysFileResult == null) throw new CustException("上传失败");
 
         // [DB] 创建文件
         SysFile sysFileEntity = new SysFile();
         sysFileEntity.setRequest_id(sysFileEntity.getRequest_id());
-        sysFileEntity.setUser_id(SecurityUtil.getUserId());
+        sysFileEntity.setUser_id(user_id);
 
         try {
             URL parsed_url = new URL(origin_url);