瀏覽代碼

离线解析语音文件逻辑调整

王苗苗 3 月之前
父節點
當前提交
d056e64f46

+ 2 - 2
slibra-admin/src/main/resources/application-local.yml

@@ -7,10 +7,10 @@ spring:
             # 主库数据源
             master:
 #                url: jdbc:mysql://localhost:3306/big_model?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
-                url: jdbc:mysql://192.168.40.21:3306/intelligent_voice_customer_service?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
+                url: jdbc:mysql://192.168.100.159:3306/libra_bot?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
                 username: root
 #                password: 1234qwer
-                password: Hongshan2024@longjiang
+                password: EKoAe3H8xybQKrFPApXM
             # 从库数据源
             slave:
                 # 从数据源开关/默认关闭

+ 1 - 0
slibra-admin/src/main/resources/application.yml

@@ -249,4 +249,5 @@ WS_URL: wss://nls-gateway-cn-shanghai.aliyuncs.com/ws/v1
 DOMAIN: https://pbx.fuxicarbon.com/voice/
 URL_DISK_PREFIX: /root/aibot/dm/voice/
 SIP_SUFFIX: '@pbx.fuxicarbon.com:5060'
+FILE_URL_PREFIX: /home/hongshan
 

+ 1 - 1
slibra-common/src/main/java/com/slibra/common/constant/MyConstants.java

@@ -6,7 +6,7 @@ import java.math.BigDecimal;
 
 public class MyConstants {
 
-    public static final String QI_NIU_YUN_UPLOAD_URL = "userupload/";
+    public static final String QI_NIU_YUN_UPLOAD_URL = "freeswitch/jms/";
     public static final String SUCCESS = "操作成功";
 
     public static final String PERCENT_SYMBOL = "%";

+ 174 - 0
slibra-system/src/main/java/com/slibra/business/service/impl/QiNiuYunServiceImpl.java

@@ -0,0 +1,174 @@
+package com.slibra.business.service.impl;
+
+import com.alibaba.fastjson2.JSON;
+import com.qiniu.common.QiniuException;
+import com.qiniu.common.Zone;
+import com.qiniu.http.Response;
+import com.qiniu.storage.BucketManager;
+import com.qiniu.storage.Configuration;
+import com.qiniu.storage.UploadManager;
+import com.qiniu.util.Auth;
+import com.slibra.common.config.QiNiuYunProperties;
+import com.slibra.common.constant.MyConstants;
+import com.slibra.common.core.redis.RedisCache;
+import com.slibra.common.utils.StringUtils;
+import com.slibra.common.utils.third.StringUtil;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Service;
+import org.springframework.web.multipart.MultipartFile;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.concurrent.TimeUnit;
+
+import static com.slibra.common.constant.CacheConstants.QINIUYUN_UPLOAD_KEY;
+import static com.slibra.common.constant.CacheConstants.QINIUYUN_UPLOAD_KEY_TIME;
+
+
+@Service
+@Slf4j
+@Component("qiNiuYunServiceNew")
+//@DependsOn("redisCache")
+public class QiNiuYunServiceImpl {
+
+    @Autowired
+    private QiNiuYunProperties qiNiuYunProperties;
+
+    @Autowired
+    private RedisCache redisCache;
+
+    // 七牛文件上传管理器
+    private UploadManager uploadManager;
+    //上传的token
+    private String token;
+    // 七牛认证管理
+    private Auth auth;
+
+    private BucketManager bucketManager;
+
+    public QiNiuYunServiceImpl(QiNiuYunProperties qiNiuYunConfig) {
+        log.info("七牛云上传服务层初始化开始~~~");
+        this.qiNiuYunProperties = qiNiuYunConfig;
+        init();
+    }
+
+    private void init() {
+        // 构造一个带指定Zone对象的配置类, 河北
+        uploadManager = new UploadManager(new Configuration(Zone.zone1()));
+        auth = Auth.create(qiNiuYunProperties.getAccessKey(), qiNiuYunProperties.getSecretKey());
+        // 根据命名空间生成的上传token
+        bucketManager = new BucketManager(auth, new Configuration(Zone.zone1()));
+//        token = auth.uploadToken(qiNiuYunProperties.getBucketName());
+//        redisCache.setCacheObject(QINIUYUN_UPLOAD_KEY, token, QINIUYUN_UPLOAD_KEY_TIME, TimeUnit.SECONDS);
+    }
+
+    /*
+     * 上传文件
+     * @Param [file, key]
+     * @return java.lang.String
+     **/
+    public String uploadQNImg(File file) {
+        FileInputStream inputStream = null;
+        try {
+            // 获取文件的名称
+            String fileName = file.getName();
+            log.info("获取到的文件的名字是{}", fileName);
+            // 使用工具类根据上传文件生成唯一图片名称
+            String imgName = MyConstants.QI_NIU_YUN_UPLOAD_URL + StringUtil.getRandomImgName(fileName);
+//            log.info("处理以后到的文件的名字是{}", imgName);
+            inputStream = new FileInputStream(file);
+            // 上传图片文件
+            //token默认是3600s Redis缓存处理
+            token = redisCache.getCacheObject(QINIUYUN_UPLOAD_KEY);
+            if(StringUtils.isBlank(token)){
+                log.info("七牛云缓存的token过期了或者是第一次初始化");
+                token = auth.uploadToken(qiNiuYunProperties.getBucketName());
+                redisCache.setCacheObject(QINIUYUN_UPLOAD_KEY, token, QINIUYUN_UPLOAD_KEY_TIME, TimeUnit.SECONDS);
+            }
+            Response res = uploadManager.put(inputStream, imgName, token, null, null);
+//            log.info("上传接口返回的结果是{}", JSON.toJSONString(res));
+            if (!res.isOK()) {
+                throw new RuntimeException("上传七牛出错,返回结果是:" + JSON.toJSONString(res));
+            }
+            // 解析上传成功的结果
+//            DefaultPutRet putRet = new Gson().fromJson(res.bodyString(), DefaultPutRet.class);
+
+            // 直接返回外链地址
+            return getPublicName(imgName);
+        } catch (QiniuException e) {
+            //这里有个bug  e.printStackTrace() 不打印异常信息,假如遇到token过期了,但是不会抛出异常
+            log.error("上次异常,异常信息为{}", e.getMessage());
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            log.error("上次异常,异常信息为{}", e.getMessage());
+            throw new RuntimeException(e);
+        } finally {
+            try {
+                inputStream.close();
+            } catch (IOException e) {
+                throw new RuntimeException(e);
+            }
+        }
+    }
+
+    private String getPublicName(String imgName) {
+        return String.format("%s/%s", this.qiNiuYunProperties.getUrl(), imgName);
+    }
+
+    /**
+     * 获取私有空间文件
+     *
+     * @param fileKey
+     * @return
+     * 2023年12月8日13:50:33 做为公共链接返回
+     */
+    public String getPrivateFile(String fileKey) {
+        String encodedFileName = null;
+//        String finalUrl = null;
+        try {
+            encodedFileName = URLEncoder.encode(fileKey, "utf-8").replace("+", "%20");
+            return String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
+//            String publicUrl = String.format("%s/%s", this.qiNiuYunProperties.getUrl(), encodedFileName);
+//            long expireInSeconds = 3600;//1小时,可以自定义链接过期时间
+//            finalUrl = auth.privateDownloadUrl(publicUrl, expireInSeconds);
+        } catch (UnsupportedEncodingException e) {
+            e.printStackTrace();
+        }
+//        return finalUrl;
+        return "";
+    }
+
+    /*
+     * 根据空间名、文件名删除文件
+     * @Param [bucketName, fileKey]
+     * @return boolean
+     **/
+    public boolean removeFile(String bucketName, String fileKey) {
+        try {
+            bucketManager.delete(bucketName, fileKey);
+        } catch (QiniuException e) {
+            e.printStackTrace();
+        }
+        return true;
+    }
+
+
+    public static void main(String[] args) throws UnsupportedEncodingException {
+        String str = URLEncoder.encode("abc/ddd", "utf-8").replace("+", "%20");
+        System.out.println(String.format("%s/%s", "https://static.fuxicarbon.com", str));
+        System.out.println(String.format("%s/%s", "https://static.fuxicarbon.com", "abc/ddd"));
+
+        try {
+            int i = 5/0;
+        } catch (Exception e) {
+//            throw new RuntimeException(e);
+            e.printStackTrace();
+        }
+    }
+
+}

+ 22 - 0
slibra-system/src/main/java/com/slibra/business/service/impl/TCallRecordServiceImpl.java

@@ -1,5 +1,6 @@
 package com.slibra.business.service.impl;
 
+import java.io.File;
 import java.util.*;
 
 import com.alibaba.fastjson2.JSON;
@@ -16,6 +17,7 @@ import com.slibra.common.utils.SecurityUtils;
 import com.slibra.common.utils.StringUtils;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 import com.slibra.business.mapper.TCallRecordMapper;
@@ -62,6 +64,13 @@ public class TCallRecordServiceImpl implements ITCallRecordService
 
     @Value("${URL_DISK_PREFIX}")
     private String urlDiskPrefix;
+
+    @Value("${FILE_URL_PREFIX}")
+    private String fileUrlPrefix;
+
+    @Autowired
+    @Qualifier("qiNiuYunServiceNew")
+    QiNiuYunServiceImpl qiNiuYunService;
     /**
      * 查询通话记录
      * 
@@ -188,11 +197,24 @@ public class TCallRecordServiceImpl implements ITCallRecordService
         return this.tCallRecordMapper.getAgentList();
     }
 
+    /**
+     * 2024年12月09日13:41:48 逻辑调整:获取通话记录,找到服务器上的录音文件,再上传到七牛云上(目前是存储了一天的时间),然后再调用离线版本的语音解析,再返回给前端
+     * @param id
+     * @return
+     */
     @Override
     public String updateFile2TextById(Long id) {
         //查询数据 校验数据
         TCallRecord tCallRecord = this.tCallRecordMapper.selectTCallRecordById(id);
         String url = checkParamsAndGetUrl(tCallRecord);
+        log.info("服务区上存储录音文件是{}", url);
+        url = fileUrlPrefix + url;
+        log.info("拼接完前缀以后,文件的URL是{}", url);
+//        url = "/Users/wangmiaomiao/Downloads/C1865941831922094080_0.wav";//本地测试
+        //上传到七牛云
+        File file = new File(url);
+        url = this.qiNiuYunService.uploadQNImg(file);
+        log.info("上传到七牛云以后的文件地址为{}", url);
         /*final String accessKeyId = "LTAI5tQ2HmiHCygZkt5BYrYR";
         final String accessKeySecret = "KhmxTd14SUcXafpFk5yofA43FoeM99";
         final String appKey = "OKt6jogp6fRjHQVp";*/