Explorar el Código

v4场景下载

dengsixing hace 2 años
padre
commit
ea8744da0b

+ 31 - 56
src/main/java/com/fdkankan/download/service/impl/SceneDownloadHandlerServiceImpl.java

@@ -8,11 +8,9 @@ import cn.hutool.core.exceptions.ExceptionUtil;
 import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.StrUtil;
 import cn.hutool.core.util.ZipUtil;
-import cn.hutool.http.HttpDownloader;
 import cn.hutool.http.HttpUtil;
 import cn.hutool.json.JSONObject;
 import cn.hutool.json.JSONUtil;
-import com.alibaba.fastjson.JSON;
 import com.fdkankan.common.constant.ErrorCode;
 import com.fdkankan.common.constant.SceneDownloadProgressStatus;
 import com.fdkankan.common.constant.SceneFrom;
@@ -36,7 +34,6 @@ import com.fdkankan.fyun.face.FYunFileServiceInterface;
 import com.fdkankan.model.constants.UploadFilePath;
 import com.fdkankan.redis.constant.RedisKey;
 import com.fdkankan.redis.util.RedisUtil;
-import com.fdkankan.web.response.ResultData;
 import com.google.common.collect.Lists;
 import java.io.File;
 import java.io.FileInputStream;
@@ -58,7 +55,6 @@ import java.util.stream.Collectors;
 import javax.annotation.Resource;
 import lombok.extern.slf4j.Slf4j;
 import lombok.var;
-import org.apache.tools.zip.ZipOutputStream;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.cloud.context.config.annotation.RefreshScope;
@@ -208,11 +204,12 @@ public class SceneDownloadHandlerServiceImpl {
         String num = downLoadTaskBean.getNum();
         //zip包路径
         String zipPath = null;
-        ZipOutputStream out = null;
-
         try {
             TimeInterval timer = DateUtil.timer();
 
+            //删除资源目录
+            FileUtil.del(String.format(this.sourceLocal, num, ""));
+
             ScenePlus scenePlus = scenePlusService.getByNum(num);
             if(Objects.isNull(scenePlus))
                 throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
@@ -228,13 +225,13 @@ public class SceneDownloadHandlerServiceImpl {
             //key总个数
             int total = ossFilePaths.size() + v4localFilePaths.size();
             AtomicInteger count = new AtomicInteger(0);
+
             //定义压缩包
             zipPath = String.format(this.zipLocalFormat, num);
             File zipFile = new File(zipPath);
             if(!zipFile.getParentFile().exists()){
                 zipFile.getParentFile().mkdirs();
             }
-            out = new ZipOutputStream(zipFile);
 
             String sceneJsonData = fYunFileService.getFileContent(bucket, String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json");
             JSONObject sceneJson = JSONUtil.parseObj(sceneJsonData);
@@ -256,20 +253,21 @@ public class SceneDownloadHandlerServiceImpl {
             }
 
             //固定文件写入
-            this.zipLocalFiles(out, v4localFilePaths, v4localPath, num, count, total, "v4");
+            this.zipLocalFiles(v4localFilePaths, v4localPath, num, count, total, "v4");
             log.info("打包固定文件耗时, num:{}, time:{}", num, timer.intervalRestart());
 
             //oss文件写入
-            this.zipOssFiles(out, ossFilePaths, num, count, total, resolution, imagesVersion, cacheKeys, "v4");
+            this.zipOssFiles(ossFilePaths, num, count, total, resolution, imagesVersion, cacheKeys, "v4");
             log.info("打包oss文件耗时, num:{}, time:{}", num, timer.intervalRestart());
 
             //重新写入scene.json(去掉密码访问设置)
-            this.zipSceneJson(out, this.wwwroot, num, sceneJson);
+            this.zipSceneJson(num, sceneJson);
 
             //写入启动命令
-            this.zipBat(out, num, "v4");
+            this.zipBat(num, "v4");
 
-            out.close();
+            //打压缩包
+            ZipUtil.zip(String.format(this.sourceLocal, num, ""), zipPath);
 
             //上传压缩包
             String uploadPath = String.format(this.zipOssFormat, num);
@@ -284,11 +282,8 @@ public class SceneDownloadHandlerServiceImpl {
             this.updateProgress( null, num, SceneDownloadProgressStatus.DOWNLOAD_FAILED.code(), null, "v4");
             throw e;
         }finally {
-            if(Objects.nonNull(out)){
-                out.close();
-                //删除本地zip包
-                FileUtils.deleteFile(zipPath);
-            }
+            FileUtil.del(zipPath);
+            FileUtil.del(String.format(this.sourceLocal, num, ""));
         }
     }
 
@@ -297,14 +292,13 @@ public class SceneDownloadHandlerServiceImpl {
         String num = downLoadTaskBean.getSceneNum();
         //zip包路径
         String zipPath = null;
-        ZipOutputStream out = null;
 
         try {
+            TimeInterval timer = DateUtil.timer();
 
+            //删除资源目录
             FileUtil.del(String.format(this.sourceLocal, num, ""));
 
-            TimeInterval timer = DateUtil.timer();
-
             ScenePro scenePro = sceneProService.getByNum(num);
             if(Objects.isNull(scenePro))
                 throw new BusinessException(ErrorCode.FAILURE_CODE_5005);
@@ -324,7 +318,6 @@ public class SceneDownloadHandlerServiceImpl {
             if(!zipFile.getParentFile().exists()){
                 zipFile.getParentFile().mkdirs();
             }
-//            out = new ZipOutputStream(zipFile);
 
             int imagesVersion =0;
             String resolution = "2k";
@@ -337,18 +330,18 @@ public class SceneDownloadHandlerServiceImpl {
 
             //固定文件写入
             timer.intervalRestart();
-            this.zipLocalFiles(out, v3localFilePaths, v3localPath, num, count, total, "v3");
+            this.zipLocalFiles(v3localFilePaths, v3localPath, num, count, total, "v3");
             log.info("打包固定文件耗时, num:{}, time:{}", num, timer.intervalRestart());
 
             //oss文件写入
-            this.zipOssFiles(out, ossFilePaths, num, count, total, resolution, imagesVersion, cacheKeys, "v3");
+            this.zipOssFiles(ossFilePaths, num, count, total, resolution, imagesVersion, cacheKeys, "v3");
             log.info("打包oss文件耗时, num:{}, time:{}", num, timer.intervalRestart());
 
             //重新写入scene.json(去掉密码访问设置)
-            this.zipGetInfoJson(out, num, getInfoJson);
+            this.zipGetInfoJson(num, getInfoJson);
 
             //写入启动命令
-            this.zipBat(out, num, "v3");
+            this.zipBat(num, "v3");
 
             //打压缩包
             ZipUtil.zip(String.format(this.sourceLocal, num, ""), zipPath);
@@ -390,7 +383,7 @@ public class SceneDownloadHandlerServiceImpl {
         return data;
     }
 
-    private void zipOssFiles(ZipOutputStream out, List<String> ossFilePaths, String num, AtomicInteger count,
+    private void zipOssFiles(List<String> ossFilePaths, String num, AtomicInteger count,
         int total, String resolution, int imagesVersion, Set<String> cacheKeys, String version) throws Exception{
         if(CollUtil.isEmpty(ossFilePaths)){
             return;
@@ -406,7 +399,7 @@ public class SceneDownloadHandlerServiceImpl {
             Callable<Boolean> call = new Callable() {
                 @Override
                 public Boolean call() throws Exception {
-                    zipOssFilesHandler(out, num, count, total, resolution,
+                    zipOssFilesHandler(num, count, total, resolution,
                         imagesVersion, cacheKeys,filePath, finalImageNumPath, version);
                     return true;
                 }
@@ -428,7 +421,7 @@ public class SceneDownloadHandlerServiceImpl {
         }
     }
 
-    private void zipOssFilesHandler(ZipOutputStream out, String num,
+    private void zipOssFilesHandler(String num,
         AtomicInteger count, int total, String resolution,
         int imagesVersion, Set<String> cacheKeys,
         String filePath, String imageNumPath, String version) throws Exception{
@@ -441,20 +434,20 @@ public class SceneDownloadHandlerServiceImpl {
         if(!"notNeadCut".equals(resolution)){
             if((filePath.contains(imageNumPath + "panorama/") && filePath.contains("tiles/" + resolution))
                 || filePath.contains(imageNumPath + "tiles/" + resolution + "/")) {
-                this.processImage(num, filePath, out, resolution, imagesVersion, cacheKeys);
+                this.processImage(num, filePath, resolution, imagesVersion, cacheKeys);
                 return;
             }
         }
 
         //其他文件打包
-        this.ProcessFiles(num, filePath, out, this.wwwroot, cacheKeys);
+        this.ProcessFiles(num, filePath, this.wwwroot, cacheKeys);
 
         //更新进度
         this.updateProgress(new BigDecimal(count.incrementAndGet()).divide(new BigDecimal(total), 6, BigDecimal.ROUND_HALF_UP),
             num, SceneDownloadProgressStatus.DOWNLOADING.code(), null, version);
     }
 
-    private void zipLocalFiles(ZipOutputStream out, List<String> localFilePaths, String v3localPath, String num, AtomicInteger count, int total, String version) throws Exception{
+    private void zipLocalFiles(List<String> localFilePaths, String v3localPath, String num, AtomicInteger count, int total, String version) throws Exception{
         String sourcePath = String.format(this.sourceLocal, num, "");
         String localPath = "v4".equals(version) ? this.v4localPath : this.v3localPath;
         for (String localFilePath : localFilePaths) {
@@ -474,7 +467,7 @@ public class SceneDownloadHandlerServiceImpl {
         FileUtil.writeUtf8String(num, String.format(sourceLocal, num, "code.txt"));
     }
 
-    private void zipBat(ZipOutputStream out, String num, String version) throws Exception{
+    private void zipBat(String num, String version) throws Exception{
         String batContent = String.format(this.exeContent, num);
         if("v3".equals(version)){
             batContent = String.format(this.exeContentV3, num);
@@ -549,25 +542,22 @@ public class SceneDownloadHandlerServiceImpl {
         return map;
     }
 
-    private void zipSceneJson(ZipOutputStream out, String root, String num, JSONObject sceneJson) throws Exception{
-
+    private void zipSceneJson(String num, JSONObject sceneJson) throws Exception{
         //访问密码置0
         JSONObject controls = sceneJson.getJSONObject("controls");
         controls.set("showLock", 0);
-
-        String sceneJsonPath = root + String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json";
-        this.zipBytes(out, sceneJsonPath, sceneJson.toString().getBytes());
+        String sceneJsonPath = String.format(UploadFilePath.DATA_VIEW_PATH, num) + "scene.json";
+        FileUtil.writeUtf8String(sceneJson.toString(), String.format(this.sourceLocal, num, this.wwwroot + sceneJsonPath));
     }
 
-    private void zipGetInfoJson(ZipOutputStream out, String num, JSONObject getInfo) throws Exception{
+    private void zipGetInfoJson(String num, JSONObject getInfo) throws Exception{
 
         //访问密码置0
         String getInfoKey = String.format("data/data%s/", num) + "getInfo.json";
         FileUtil.writeUtf8String(getInfo.toString(), String.format(this.sourceLocal, num, this.wwwroot + getInfoKey));
-//        this.zipBytes(out, getInfoKey, getInfo.toString().getBytes());
     }
 
-    private void processImage(String sceneNum, String key, ZipOutputStream out, String resolution, int imagesVersion, Set<String> imgKeys) throws Exception{
+    private void processImage(String sceneNum, String key, String resolution, int imagesVersion, Set<String> imgKeys) throws Exception{
 
         if(key.contains("x-oss-process") || key.endsWith("/")){
             return;
@@ -631,7 +621,7 @@ public class SceneDownloadHandlerServiceImpl {
 
     }
 
-    public void ProcessFiles(String num, String key, ZipOutputStream out, String prefix, Set<String> cacheKeys) throws Exception{
+    public void ProcessFiles(String num, String key, String prefix, Set<String> cacheKeys) throws Exception{
         if(cacheKeys.contains(key)){
             return;
         }
@@ -692,19 +682,4 @@ public class SceneDownloadHandlerServiceImpl {
 
     }
 
-    public void zipInputStream(ZipOutputStream out, String key, FileInputStream in) throws Exception {
-        out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
-        byte[] bytes = new byte[1024];
-        int b = 0;
-        while ((b = in.read(bytes)) != -1) {
-            out.write(bytes, 0, b);
-        }
-    }
-
-    public synchronized void zipBytes(ZipOutputStream out, String key, byte[] bytes) throws Exception {
-        out.putNextEntry(new org.apache.tools.zip.ZipEntry(key));
-        out.write(bytes);
-    }
-
-
 }