Explorar el Código

Merge branch 'feature-v4.3.0-20220825' into project-jp

dengsixing hace 3 años
padre
commit
39fa828ed6

+ 34 - 25
4dkankan-center-scene-download/src/main/java/com/fdkankan/download/service/impl/SceneDownloadHandlerServiceImpl.java

@@ -3,6 +3,7 @@ package com.fdkankan.download.service.impl;
 import cn.hutool.core.collection.CollUtil;
 import cn.hutool.core.collection.ConcurrentHashSet;
 import cn.hutool.core.exceptions.ExceptionUtil;
+import cn.hutool.core.io.IoUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
@@ -29,7 +30,12 @@ import com.fdkankan.scene.api.feign.SceneUserSceneClient;
 import com.google.common.collect.Lists;
 import java.io.File;
 import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
 import java.math.BigDecimal;
+import java.net.HttpURLConnection;
+import java.net.URL;
 import java.net.URLEncoder;
 import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
@@ -376,29 +382,6 @@ public class SceneDownloadHandlerServiceImpl {
         return map;
     }
 
-    private JSONObject zipGetInfoJson(ZipOutputStream out, String root, String num) throws Exception{
-
-        ResultData<SceneInfoDTO> sceneViewInfo = sceneUserSceneClient.getSceneViewInfo(num);
-        if(!sceneViewInfo.getSuccess()){
-            throw new Exception(ServerCode.FEIGN_REQUEST_FAILD.message());
-        }
-        SceneInfoDTO data = sceneViewInfo.getData();
-        JSONObject getInfoJson = null;
-        if(Objects.isNull(data)){
-            getInfoJson = new JSONObject();
-        }else {
-            getInfoJson = JSONUtil.parseObj(data);
-        }
-
-        getInfoJson.set("sceneScheme", 3);
-        getInfoJson.set("needKey", 0);
-        getInfoJson.set("sceneKey","");
-        //写入getInfo.json
-        String getInfoJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "getInfo.json";
-        this.zipBytes(out, getInfoJsonPath, getInfoJson.toString().getBytes());
-        return getInfoJson;
-    }
-
     private void zipSceneJson(ZipOutputStream out, String root, String num, JSONObject sceneJson) throws Exception{
 
         //访问密码置0
@@ -494,7 +477,7 @@ public class SceneDownloadHandlerServiceImpl {
 
             zipBytes(out, prefix + key, content.getBytes());
         }else{
-            zipBytes(out, prefix + key, FileUtils.getBytesFromUrl(url));
+            zipFromUrl(out, prefix + key, url);
         }
     }
 
@@ -535,7 +518,7 @@ public class SceneDownloadHandlerServiceImpl {
 
     }
 
-    public void zipInputStream(ZipOutputStream out, String key, FileInputStream in) throws Exception {
+    public synchronized void zipInputStream(ZipOutputStream out, String key, InputStream in) throws Exception {
         out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
         byte[] bytes = new byte[1024];
         int b = 0;
@@ -550,4 +533,30 @@ public class SceneDownloadHandlerServiceImpl {
     }
 
 
+
+    public void zipFromUrl(ZipOutputStream out, String key, String url) throws Exception {
+        try (InputStream is = this.getInputStreamFromUrl(url)){
+            this.zipInputStream(out, key, is);
+        }catch (Exception e){
+            log.error("追加压缩包失败,url="+url, e);
+            throw e;
+        }
+    }
+
+    public InputStream getInputStreamFromUrl(String urlStr){
+        InputStream inputStream = null;
+        try {
+            URL url = new URL(urlStr);
+            HttpURLConnection conn = (HttpURLConnection)url.openConnection();
+            conn.setConnectTimeout(3000);
+            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
+            inputStream = conn.getInputStream();
+            return inputStream;
+        } catch (FileNotFoundException e) {
+            log.error("获取流失败,url = " + urlStr, e);
+        } catch (IOException e) {
+            log.error("获取流失败,url = " + urlStr, e);
+        }
+        return null;
+    }
 }

+ 72 - 24
4dkankan-center-scene/src/main/java/com/fdkankan/scene/service/impl/SceneEditInfoServiceImpl.java

@@ -494,6 +494,8 @@ public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper,
         sceneInfoVO.setVideos(scenePlusExt.getVideos());
         sceneInfoVO.setMosaicList(this.getMosaicList(num));
 
+        this.SortBoxVideos(sceneInfoVO);
+
         // TODO: 2022/4/24 v3版本停机要切换---------------------------start
 //        this.setExtData(sceneInfoVO, scenePlus.getCameraId());
         this.setExtDataFromV3(sceneInfoVO, scenePlus.getCameraId());
@@ -502,6 +504,35 @@ public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper,
         return sceneInfoVO;
     }
 
+    private void SortBoxVideos(SceneInfoVO sceneInfoVO){
+        String boxVideos = sceneInfoVO.getBoxVideos();
+        if(StrUtil.isEmpty(boxVideos)){
+            return;
+        }
+        JSONArray boxVideoArr = JSON.parseArray(boxVideos);
+        if(CollUtil.isEmpty(boxVideoArr)){
+            return;
+        }
+        List<TagBean> tagBeanList = boxVideoArr.stream().map(o -> {
+                        JSONObject item = (JSONObject) o;
+                        return TagBean.builder()
+                            .createTime(item.getLong("createTime"))
+                            .tag(item).build();
+                    }).collect(Collectors.toList());
+
+        //按创建时间倒叙排序
+        tagBeanList.sort(Comparator.comparingLong(TagBean::getCreateTime).reversed());
+
+        //移除createTime字段
+        List<JSONObject> boxVideoList = tagBeanList.stream().map(tagBean -> {
+            JSONObject tag = tagBean.getTag();
+            tag.remove("createTime");
+            return tag;
+        }).collect(Collectors.toList());
+
+        sceneInfoVO.setBoxVideos(JSON.toJSONString(boxVideoList));
+    }
+
     /**
      * <p>
      getInfo接口返回字段扩展
@@ -2398,10 +2429,24 @@ public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper,
             boxVideosJson = new JSONArray();
         }
 
+        if(boxVideosJson.size() > 0){
+            int i = 1;
+            long timeInMillis = Calendar.getInstance().getTimeInMillis();
+            for (Object o : boxVideosJson) {
+                JSONObject item = (JSONObject)o;
+                if(Objects.nonNull(item.getLong("createTime"))){
+                    continue;
+                }
+                item.put("createTime", timeInMillis - (i++));
+            }
+        }
+
+
         String result = null;
         //删除
         if(type == OperationType.DELETE.code()){
-            Set<String> deleteFile = new HashSet<>();
+            Set<String> deleteVidoeFile = new HashSet<>();
+            Set<String> deletePicFile = new HashSet<>();
             if(boxVideosJson.size() == 0)
                 return null;
             for(int i=0;i<boxVideosJson.size();++i){
@@ -2410,43 +2455,46 @@ public class SceneEditInfoServiceImpl extends ServiceImpl<ISceneEditInfoMapper,
 
                     String poster = ele.getString("poster");
                     if(StrUtil.isNotEmpty(poster))
-                        deleteFile.add(poster);
+                        deletePicFile.add(poster);
                     String url = ele.getString("url");
                     if(StrUtil.isNotEmpty(url)){
-                        deleteFile.add(url);
-                        deleteFile.add(url.replace(".mp4",".flv"));
+                        deleteVidoeFile.add(url);
+                        deleteVidoeFile.add(url.replace(".mp4",".flv"));
                     }
 
                     boxVideosJson.remove(i);
                 }
             }
 
-            //删除资源文件
-            if(CollUtil.isNotEmpty(deleteFile))
+            //删除视频文件
+            if(CollUtil.isNotEmpty(deleteVidoeFile))
                 sceneUploadService.delete(
                     DeleteFileParamVO.builder().num(num)
                         .bizType(FileBizType.BOX_VIDEO.code())
-                        .fileNames(new ArrayList<>(deleteFile)).build());
+                        .fileNames(new ArrayList<>(deleteVidoeFile)).build());
+            //删除封面图文件
+            if(CollUtil.isNotEmpty(deleteVidoeFile))
+                sceneUploadService.delete(
+                    DeleteFileParamVO.builder().num(num)
+                        .bizType(FileBizType.BOX_POSTER.code())
+                        .fileNames(new ArrayList<>(deletePicFile)).build());
         }else{
 
-            // TODO: 2022/3/1 目前需求只保留一个,所以这里先注释掉,sid不同时,不追加
-            //更新
-//            boolean exist = false;
-//            for(int i=0;i<boxVideosJson.size();++i){
-//                JSONObject ele = boxVideosJson.getJSONObject(i);
-//                if(ele.getString("sid").equals(sid)){
-//                    boxVideosJson.set(i, boxVideo);
-//                    exist = true;
-//                }
-//            }
-//        //新增
-//        if(!exist){
-//            boxVideosJson.add(boxVideo);
-//        }
-
-            boxVideosJson.clear();
-            boxVideosJson.add(boxVideo);
+            boxVideo.put("createTime", Calendar.getInstance().getTimeInMillis());
 
+            //更新
+            boolean exist = false;
+            for(int i=0;i<boxVideosJson.size();++i){
+                JSONObject ele = boxVideosJson.getJSONObject(i);
+                if(ele.getString("sid").equals(sid)){
+                    boxVideosJson.set(i, boxVideo);
+                    exist = true;
+                }
+            }
+            //新增
+            if(!exist){
+                boxVideosJson.add(boxVideo);
+            }
         }
         if(boxVideosJson.size() != 0){
             result = boxVideosJson.toJSONString();