wuweihao 3 лет назад
Родитель
Сommit
f5bc1300c3

+ 4 - 0
gis_application/src/main/resources/application.properties

@@ -12,6 +12,10 @@ spring.servlet.multipart.enabled=true
 spring.servlet.multipart.max-file-size=256MB
 spring.servlet.multipart.max-request-size=256MB
 
+# \u5141\u8BB8\u4E0A\u4F20\u6587\u4EF6\u540E\u7F00
+server.file.allow=.jpg,.gif,.png,.ico,.bmp,.jpeg,.zip,.zp,.rar,.mp3,.mp4,.avi,.mov,.flv,.3gp,.rmvb,.4dage,.wav,.wma,.m4a,.obj,.pdf,.audio,.ppt,.pptx,.xls,.xlsx,.doc,.docx,.txt
+
+
 
 
 

+ 9 - 0
gis_application/src/main/resources/static/zip.sh

@@ -0,0 +1,9 @@
+#!/bin/bash
+workPace=$1    #工作目录
+output=$2      #输出目录
+inDir=$3       #下载目录参数
+echo 工作目录 $1
+echo 输出目录 $2
+echo 目录参数 $3
+cd $workPace
+zip -r $2 $3

+ 10 - 0
gis_common/src/main/java/com/gis/common/constant/CmdConstant.java

@@ -41,4 +41,14 @@ public class CmdConstant {
 //    public final static String OSSUTIL_DOWNLOAD_DIR = "bash /root/java/age_convert_8003/oss_download.sh @inPath @outPath @bucket";
     public final static String OSSUTIL_DOWNLOAD_DIR = "bash /root/data/age_convert_data/baseData/oss_download.sh @inPath @outPath @bucket";
 
+    /**
+     * 压缩目录
+     * @inDir 下载目录根目录
+     * @output 指定目录
+     * @code  需要下载的目录参数, 参数用逗号隔开
+     */
+//    public final static String zip = "zip -r @output @inDir{@code}";
+//    public final static String zip = "bash /root/data/720yun_local_manage_data/baseData/zip.sh @workPace @output \"@inDir\"";
+    public final static String zip = "bash /root/data/age_convert_data/baseData/zip.sh @workPace @output '@inDir'";
+
 }

+ 3 - 0
gis_common/src/main/java/com/gis/common/constant/ConfigConstant.java

@@ -25,6 +25,9 @@ public class ConfigConstant {
     @Value("${project.name}")
     public String projectName;
 
+    @Value("${server.file.allow}")
+    public String serverFileFallow;
+
 //    @Value("${project.name}")
 //    public String serverDomain;
 

+ 68 - 9
gis_common/src/main/java/com/gis/common/exception/BaseRuntimeException.java

@@ -2,6 +2,9 @@ package com.gis.common.exception;
 
 import cn.hutool.core.util.StrUtil;
 import com.gis.common.constant.ErrorEnum;
+import org.springframework.util.CollectionUtils;
+
+import java.util.List;
 
 public class BaseRuntimeException extends RuntimeException{
 
@@ -15,12 +18,22 @@ public class BaseRuntimeException extends RuntimeException{
         this.msg = msg;
     }
 
+    /**
+     *
+     * @param code 允许为null
+     * @param msg
+     */
     public BaseRuntimeException(Integer code, String msg){
         super(msg);
-        this.code = code;
+        this.code = code == null? -1 : code;
         this.msg = msg;
     }
 
+    public BaseRuntimeException(ErrorEnum errorEnum){
+        this.code = errorEnum.code();
+        this.msg = errorEnum.message();
+    }
+
     public Integer getCode() {
         return code;
     }
@@ -37,26 +50,27 @@ public class BaseRuntimeException extends RuntimeException{
         this.msg = msg;
     }
 
+
     public static void isNull(Object obj, Integer code, String msg){
         if (obj == null){
-            throw new BaseRuntimeException(code, msg);
+            getExc(code, msg);
         }
     }
 
     public static void isBlank(Object obj, Integer code, String msg){
         if (obj == null){
-            throw new BaseRuntimeException(code, msg);
+            getExc(code, msg);
         }
 
-        if (obj instanceof String || StrUtil.isBlank(obj.toString())){
-            throw new BaseRuntimeException(code, msg);
+        if (obj instanceof String && StrUtil.isBlank(obj.toString())){
+            getExc(code, msg);
         }
 
     }
 
     public static void isNull(Object obj, ErrorEnum errorEnum){
         if (obj == null){
-            throw new BaseRuntimeException(errorEnum.code(), errorEnum.message());
+            getExc(errorEnum.code(), errorEnum.message());
         }
     }
 
@@ -64,12 +78,57 @@ public class BaseRuntimeException extends RuntimeException{
         Integer code = errorEnum.code();
         String msg = errorEnum.message();
         if (obj == null){
-            throw new BaseRuntimeException(code, msg);
+            getExc(code, msg);
+        }
+
+        if (obj instanceof String && StrUtil.isBlank(obj.toString())){
+            getExc(code, msg);
+        }
+
+    }
+
+    /**
+     *
+     * @param obj true 存在抛异常
+     * @param errorEnum
+     */
+    public static void isTrue(boolean obj, ErrorEnum errorEnum){
+        if (obj){
+            getExc(errorEnum.code(), errorEnum.message());
         }
+    }
+
+    /**
+     *
+     * @param obj 存在抛异常
+     * @param code 允许为null
+     * @param msg
+     */
+    public static void isTrue(boolean obj, Integer code, String msg){
+        if (obj){
+            getExc(code, msg);
+        }
+    }
 
-        if (obj instanceof String || StrUtil.isBlank(obj.toString())){
-            throw new BaseRuntimeException(code, msg);
+    public static void  getExc(Integer code, String msg){
+        throw new BaseRuntimeException(code, msg);
+    }
+
+    /**
+     *
+     * @param obj 集合
+     * @param errorEnum
+     */
+    public static void isEmpty(List obj, ErrorEnum errorEnum){
+        if (CollectionUtils.isEmpty(obj)){
+            getExc(errorEnum.code(), errorEnum.message());
         }
+    }
 
+    public static void isEmpty(List obj, Integer code, String msg){
+        if (CollectionUtils.isEmpty(obj)){
+            getExc(code, msg);
+        }
     }
+
 }

+ 20 - 17
gis_common/src/main/java/com/gis/common/util/CmdUtils.java

@@ -84,26 +84,29 @@ public class CmdUtils {
 
     }
 
-
     /**
-     * 调用算法 xx.sh 脚本
-     * @param command
+     * 运行xx.sh 脚本
+     * @param command 命令
+     * @param lineSize 日志输出行数 ,可以为null
+     *
      */
-//    public static void callPano(String command){
-//        log.info("cmd: " + command);
-//        try {
-//            Process process = Runtime.getRuntime().exec(command);
-//            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
-//            errorGobbler.start();
-//            // 200行打印一次日志
-//            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", 200);
-//            outGobbler.start();
-//            process.waitFor();
-//        } catch (Exception e) {
-//            e.printStackTrace();
-//        }
-//    }
+    public static void callShLine(String command, Integer lineSize){
+        log.info("cmd: " + command);
+        try {
+            String[] cmd = new String[]{"/bin/sh", "-c", command};
+            Process process = Runtime.getRuntime().exec(cmd);
+            log.info("开始运行");
+            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+            errorGobbler.start();
+            // 200行打印一次日志
+            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT", lineSize);
+            outGobbler.start();
+            process.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
 
+    }
 
     public static void callLine(String command){
         callLine(command, null);

+ 69 - 0
gis_common/src/main/java/com/gis/common/util/DateUtils.java

@@ -0,0 +1,69 @@
+package com.gis.common.util;
+
+import cn.hutool.core.date.DateUtil;
+import org.junit.Test;
+
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+
+/**
+ * Created by owen on 2022/4/8 0008 14:32
+ */
+public class DateUtils extends DateUtil {
+
+    private static String YYYY_MM = "yyyy-MM";
+
+    private static String YYYYMM = "yyyyMM";
+
+    private static String YYYY_MM_DD = "yyyy-MM-dd";
+
+    private static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";
+
+
+    private static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
+
+    private static String YYYYMMDD_HHMMSSSSS = "yyyyMMdd_HHmmssSSS";
+
+    private static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
+
+
+    /**
+     * 获取当前月
+     * @return
+     */
+    public static String getMonth(){
+        return format(LocalDateTime.now(), YYYYMM);
+    }
+
+    /**
+     * 获取当前月份的前几个月
+     * @param month
+     * @return
+     */
+    public static String minusMonths(int month){
+        return format(LocalDateTime.now().minusMonths(month), YYYYMM);
+    }
+
+    /**
+     * 获取当前时间戳
+     * @return
+     */
+    public static String getDateTime(){
+        return format(LocalDateTime.now(), YYYYMMDD_HHMMSSSSS);
+    }
+
+
+    /**
+     * 字符串转LocalDateTime
+     * @param time 字符串
+     * @return LocalDateTime
+     */
+    public static LocalDateTime srtToLocalDateTime(String time){
+        return LocalDateTime.parse(time, DateTimeFormatter.ofPattern(YYYY_MM_DD_HH_MM));
+    }
+
+    @Test
+    public void test(){
+        System.out.println(srtToLocalDateTime("2022-02-16 12:26"));
+    }
+}

+ 81 - 170
gis_common/src/main/java/com/gis/common/util/FileUtils.java

@@ -1,214 +1,125 @@
 package com.gis.common.util;
 
-import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
-import cn.hutool.core.util.URLUtil;
+import cn.hutool.core.util.StrUtil;
+import com.gis.common.constant.ConfigConstant;
+import com.gis.common.exception.BaseRuntimeException;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-import org.junit.Test;
-import org.springframework.util.ResourceUtils;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
 import org.springframework.web.multipart.MultipartFile;
 
-import java.io.*;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.*;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
 
 /**
  * Created by owen on 2020/5/12 0012 17:21
  */
 @Slf4j
+@Component
 public class FileUtils {
 
+    @Autowired
+    ConfigConstant configConstant;
 
-    /**
-     * 多文件上传
-     * savePath 路径(目录)
-     */
-    public static List<Map<String, String>> uploads(MultipartFile[] files, String savePath) throws IOException {
-        if (files == null) {
-            log.error("文件不能为空");
-            return null;
-        }
+    // 确保同一时间上传文件的唯一性
+    private static final AtomicInteger ATOMIC_INTEGER = new AtomicInteger();
 
-        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
-        savePath = savePath + time + File.separator;
-        FileUtil.mkdir(time);
-
-        ArrayList<Map<String, String>> list = new ArrayList<>();
-
-        String path;
-        int i = 0;
-        for (MultipartFile file : files) {
-            String fileName = file.getOriginalFilename();
-            String suffix = StringUtils.substringAfterLast(fileName, ".");
-            String newName = time + "_" + i + "."  +suffix;
-            path = savePath + newName;
-            FileUtil.writeFromStream(file.getInputStream(), path);
-
-            HashMap<String, String> fileInfo = new HashMap<>();
-            fileInfo.put("path", path);
-            fileInfo.put("name", fileName);
-            fileInfo.put("newName", newName);
-            fileInfo.put("dir", time);
-
-            list.add(fileInfo);
-            ++ i;
+    public boolean checkFile(MultipartFile file) {
+        //设置允许上传文件类型
+        String suffixList = configConstant.serverFileFallow;
+        // 获取文件后缀
+        if(file == null){
+            log.error("文件流为空不可上传");
+            return false;
         }
-
-        return list;
-
+        String fileName = file.getOriginalFilename();
+        String suffix = fileName.substring(fileName.lastIndexOf(".")
+                + 1, fileName.length());
+        if (suffixList.contains(suffix.trim().toLowerCase())) {
+            log.info("无非法参数可以放行!!!");
+            return true;
+        }
+        log.error("存在非法参数不能放行!请核对上传文件格式,重新刷新页面再次上传!输入文件后缀: {}", suffix);
+        return false;
     }
 
 
     /**
-     * 单文件上传
+     *
      * @param file
-     * @param savePath
-     * @return
-     * @throws IOException
+     * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
+     * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
+     * @return 文件名
      */
-    public static HashMap<String, String> upload(MultipartFile file, String savePath) throws IOException {
-        if (file == null) {
-            log.error("文件不能为空");
-            return null;
-        }
+    public String upload(MultipartFile file, String savePath, boolean isPinYinRename) {
 
-        String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
+        // 检查非法文件上传
+        boolean checkFile = this.checkFile(file);
+        if (!checkFile) {
+            throw new BaseRuntimeException("上传文件格式有误, 请重新上传");
+        }
 
+        // 文件目录
         String fileName = file.getOriginalFilename();
-        String suffix = StringUtils.substringAfterLast(fileName, ".");
-        String newName = time  + "."  +suffix;
-        savePath = savePath  + newName;
-
-        FileUtil.writeFromStream(file.getInputStream(), savePath);
-        HashMap<String, String> fileInfo = new HashMap<>();
+        String newName;
+        if (isPinYinRename){
+             newName = RegexUtil.getPinyinName(fileName);
+        } else {
+//            String suffix = StrUtil.substringAfterLast(fileName, ".");
+            String suffix = StrUtil.subAfter(fileName, ".", true);
+            newName =  DateUtils.getDateTime() + ATOMIC_INTEGER.incrementAndGet() + "." + suffix;
+        }
 
-        fileInfo.put("path", savePath);
-        fileInfo.put("name", fileName);
-        fileInfo.put("newName", newName);
+        savePath = configConstant.serverBasePath + savePath + "/" + newName;
+        log.info("保存文件地址:{}", savePath);
 
-        return fileInfo;
+        try {
+            FileUtil.writeFromStream(file.getInputStream(), savePath);
 
+            return newName;
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+        return null;
     }
 
-
     /**
-     * 根据路径写入文件,适合oss
-     * @param inPath 网络输入路径
-     * @param outPath 保存文件路径
-     * @throws IOException
+     *
+     * @param file
+     * @param isPinYinRename false:时间戳重命名, true:用拼音重名文件
+     * @param savePath 保存地址(前面有斜杠, 后面没有),没有文件名
+     * @return map
      */
-    public static void fielWrite(String inPath, String outPath) throws IOException {
-        InputStream in = URLUtil.getStream(new URL(inPath));
-        FileUtil.writeFromStream(in, outPath);
-    }
+    public Map<String, Object> uploadMap(MultipartFile file, String savePath, boolean isPinYinRename) {
+        String newName = this.upload(file, savePath, isPinYinRename);
+        HashMap<String, Object> result = new HashMap<>();
+        result.put("fileName", file.getOriginalFilename());
+        result.put("filePath", savePath + "/" + newName);
+        return result;
 
-
-    /**
-     * 获取文件资源路径
-     * 这方法,测试时是用当前类路径,当打包成jar包时时,会变成跟目录下。所以要把资源文件放入口类的资源文件夹
-     * @param filePath 文件路径
-     * @return
-     * @throws IOException
-     */
-    public static String getResourceUrl(String filePath) throws IOException {
-        String path = ResourceUtils.getURL("classpath:").getPath();
-        path = path + filePath;
-        return path;
     }
 
-
     /**
-     * 从网络Url中下载文件
-     *
-     * @param urlStr
-     * @param fileName
-     * @param savePath
-     * @return
-     * @throws IOException
+     * 真删除文件
+     * @param path 参数是相对地址
      */
-    public static boolean downLoadFromUrl(String urlStr, String fileName, String savePath){
-        FileOutputStream fos = null;
-        InputStream inputStream = null;
-        try {
-            URL url = new URL(urlStr);
-            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
-            // 设置超时间为3秒
-            conn.setConnectTimeout(3 * 1000);
-            // 防止屏蔽程序抓取而返回403错误
-            conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
-
-            // 得到输入流
-            inputStream = conn.getInputStream();
-            // 获取自己数组
-            byte[] getData = readInputStream(inputStream);
-
-            // 文件保存位置
-            File saveDir = new File(savePath);
-            if (!saveDir.exists()) {
-                saveDir.mkdirs();
-            }
-            String filePath = saveDir + File.separator + fileName;
-            String filePathFolder = filePath.substring(0, filePath.lastIndexOf("/") + 1);
-            FileUtil.mkdir(filePathFolder);
-
-            File file = new File(filePath);
-            fos = new FileOutputStream(file);
-            fos.write(getData);
-            if (fos != null) {
-                fos.close();
-            }
-            if (inputStream != null) {
-                inputStream.close();
-            }
-            System.out.println("info:" + url + " download success");
-        } catch(FileNotFoundException e){
-            return false;
-        } catch (IOException e) {
-            return false;
-        }finally {
-            if (fos != null) {
-                try {
-                    fos.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
-            if (inputStream != null) {
-                try {
-                    inputStream.close();
-                } catch (IOException e) {
-                    e.printStackTrace();
-                }
-            }
+    public void del(String path){
+        if (StrUtil.isNotBlank(path)){
+            String delPath = configConstant.serverBasePath + path;
+            FileUtil.del(delPath);
+            log.info("真删除文件: {}", delPath);
         }
-        return true;
+
     }
 
 
-    /**
-     * 从输入流中获取字节数组
-     *
-     * @param inputStream
-     * @return
-     * @throws IOException
-     */
-    private static byte[] readInputStream(InputStream inputStream) throws IOException {
-        byte[] buffer = new byte[1024];
-        int len = 0;
-        ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        while ((len = inputStream.read(buffer)) != -1) {
-            bos.write(buffer, 0, len);
-        }
-        bos.close();
-        return bos.toByteArray();
-    }
 
 
-    @Test
-    public void test(){
-        String a = "https://super.4dage.com/data/690/vision.modeldata";
-        downLoadFromUrl("https://super.4dage.com/data/690/vision.modeldata", "vision.modeldata", "F:\\test\\bigScene\\");
-    }
+
+
+
+
 }

+ 97 - 0
gis_common/src/main/java/com/gis/common/util/RegexUtil.java

@@ -0,0 +1,97 @@
+package com.gis.common.util;
+
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.pinyin.PinyinUtil;
+import com.gis.common.exception.BaseRuntimeException;
+import lombok.extern.slf4j.Slf4j;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.List;
+
+/**
+ * Created by owen on 2021/11/18 0011 16:16
+ * 字符串过滤
+ */
+@Slf4j
+public class RegexUtil {
+
+    // 需要过滤的特殊字符
+    static String [] specialSql = {"%","or","=","and","truncate","delete","update","exec"};
+
+
+    /** 处理特殊符号,变空值*/
+    public static String specificSymbol(String str){
+
+        String regEx = "[\\s`~!@#$%^&*()+=|{}':;\\[\\]<>/?·~!@#¥%……&*()——+|{}【】‘;:“”。,、?]";
+        return str.replaceAll(regEx, "");
+    }
+
+
+    /** 中文转拼音*/
+    public static String getPinyinName(String str){
+        // 去除特殊符号
+        String pinyinName = RegexUtil.specificSymbol(str);
+        pinyinName = PinyinUtil.getPinyin(pinyinName, "");
+        // 转小写
+        pinyinName =  pinyinName.toLowerCase();
+        return pinyinName;
+
+    }
+
+
+    /**防止sql注入*/
+    public static void regSql(String str){
+        String key = "and|exec|insert|select|delete|update|count|*|%|chr|mid|master|truncate|char|declare|;|or|-|+";
+        String[] split = StrUtil.split(key, "|");
+        List<String> list = Arrays.asList(split);
+        for (String s : list) {
+            if (str.toLowerCase().contains(s)){
+                String msg = "存在sql注入字符";
+                log.error(msg);
+                throw new BaseRuntimeException(msg);
+            }
+        }
+
+    }
+
+    /**
+     * sql 过滤特殊字符
+     * @param str
+     * @return
+     */
+    public static String sqlReplaceSpecialStr(String str){
+        str = StrUtil.trim(str);
+        str = str.toLowerCase();
+        for (String s : specialSql) {
+            if (str.contains(s)) {
+                str = str.replaceAll(s, "");
+            }
+        }
+        return str;
+    }
+
+
+
+
+
+
+
+    public static void main(String[] args) {
+        String str = "我·是 中—国(人), 你-在{干嘛}--哈—哈。 ddd.jpg";
+        System.out.println(specificSymbol(str));
+    }
+
+
+    @Test
+    public void test(){
+        String regEx = "12,15,+ delete";
+        regSql(regEx);
+
+
+}
+
+
+}
+
+

+ 27 - 0
gis_service/src/main/java/com/gis/YunDto.java

@@ -0,0 +1,27 @@
+package com.gis;
+
+import io.swagger.annotations.ApiModelProperty;
+import lombok.Data;
+
+import javax.validation.constraints.NotBlank;
+import java.util.List;
+
+/**
+ * Created by owen on 2022/5/30 0030 10:12
+ *
+ */
+@Data
+public class YunDto {
+
+    @NotBlank(message = "场景码不能为空")
+    @ApiModelProperty(value = "该作品下的场景码", required = true)
+    private List<String> sceneCodes;
+
+    @NotBlank(message = "oss环境不能为空")
+    @ApiModelProperty(value = "oss环境(sit,全景看看sit, pro:全景看看pro)", required = true)
+    private String evn;
+
+    @NotBlank(message = "作品码不能为空")
+    @ApiModelProperty(value = "作品码", required = true)
+    private String workId;
+}

+ 4 - 1
gis_service/src/main/java/com/gis/service/AliOssService.java

@@ -1,6 +1,7 @@
 package com.gis.service;
 
 
+import com.gis.YunDto;
 import com.gis.common.util.Result;
 
 import java.util.List;
@@ -12,5 +13,7 @@ import java.util.List;
 public interface AliOssService {
 
 
-    Result batchDownload(List<String> code, String evn, String workId);
+    Result batchDownload(YunDto param);
+
+
 }

+ 117 - 17
gis_service/src/main/java/com/gis/service/impl/AliOssServiceImpl.java

@@ -1,13 +1,18 @@
 package com.gis.service.impl;
 
+import cn.hutool.core.io.FileUtil;
 import cn.hutool.core.util.StrUtil;
+import cn.hutool.http.HttpUtil;
+import com.alibaba.fastjson.JSONArray;
+import com.alibaba.fastjson.JSONObject;
+import com.gis.YunDto;
 import com.gis.common.constant.CmdConstant;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.PathConstant;
 import com.gis.common.exception.BaseRuntimeException;
 import com.gis.common.util.CmdUtils;
+import com.gis.common.util.FileUtils;
 import com.gis.common.util.Result;
-import com.gis.common.util.StrUtils;
 import com.gis.service.AliOssService;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -16,6 +21,7 @@ import org.springframework.stereotype.Service;
 import java.io.File;
 import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 
 /**
@@ -28,6 +34,9 @@ public class AliOssServiceImpl implements AliOssService {
     @Autowired
     ConfigConstant configConstant;
 
+    @Autowired
+    FileUtils fileUtils;
+
     static final  HashMap<Object, String> bucketMap;
 
     static {
@@ -38,39 +47,128 @@ public class AliOssServiceImpl implements AliOssService {
 
 
     @Override
-    public Result batchDownload(List<String> code, String evn, String workId) {
-        log.info("输入场景码数量: {}", code.size());
+    public Result batchDownload(YunDto param) {
 
-        String bucket = bucketMap.get(evn);
-        BaseRuntimeException.isNull(bucket, null, "bucket环境不能为空");
+        String workId = param.getWorkId();
+        // 1.删除旧目录
+        fileUtils.del("/" + workId);
 
+        // 2.下载someData.json
+        downloadSomeDataJson(workId);
+
+        // 3.下载场景
+        downloadScenes(param);
+
+        // 4. 获取导览图集合
+        HashMap<String, String> guideThumbs = getGuideThumbBySomeData(workId);
+
+        // 5.下载导览图
+        downloadGuideThumb(guideThumbs, workId);
+
+        // 6.压缩zip
+        zipWork(workId);
+        String outPath = configConstant.serverBasePath + "/download/" + workId +".zip";
+        return Result.success(outPath);
+    }
+
+    private void zipWork(String workId) {
+        String outPath = configConstant.serverBasePath + "/download/" + workId +".zip";
+
+        String cmd = CmdConstant.zip;
+        cmd = cmd.replaceAll("@output", outPath);
+        cmd = cmd.replaceAll("@workPace", configConstant.serverBasePath + "/download" );
+        // 已引入工作目录, 直接填写目录即可
+        cmd = cmd.replaceAll("@inDir", workId);
+
+        CmdUtils.callShLine(cmd, 100);
+        log.info("打包压缩完成: {}", outPath);
+
+    }
+
+
+    /**
+     * 下载场景目录
+     * @param param
+     */
+    private void downloadScenes(YunDto param){
+        List<String> sceneCodes = param.getSceneCodes();
+        log.info("输入场景码数量: {}", sceneCodes.size());
+
+        String bucket = bucketMap.get(param.getEvn());
+        BaseRuntimeException.isNull(bucket, null, "bucket环境不能为空");
 
-        String dir ;
-        if (StrUtil.isBlank(workId)){
-            dir = "720yun_" + StrUtils.getTimeStr();
-        } else {
-            dir = "720yun_" + workId + "_" + StrUtils.getTimeStr();
-        }
-        String outPath = configConstant.serverBasePath + dir;
-        log.info("下载目录: {}", outPath);
         int i = 1;
-        for (String sceneCode : code) {
-            downloadOss(sceneCode, outPath, bucket);
+        for (String sceneCode : sceneCodes) {
+            downloadOss(sceneCode, bucket, param.getWorkId());
             log.info("第:{} 下载完成, 场景为:{}",  i, sceneCode);
             i++;
         }
+    }
 
-        return Result.success(outPath);
+    /**
+     * 2022-5-30
+     * 建议前端将导览图片固定命名
+     * 通过读取someData.json获取导图图片
+     *
+     * @param workId
+     */
+    private  HashMap<String, String> getGuideThumbBySomeData(String workId){
+        String someDataPath = configConstant.serverBasePath + "/download/" + workId + "/someData.json";
+        String str = FileUtil.readString(someDataPath, "utf-8");
+        JSONObject parent = JSONObject.parseObject(str);
+        // 场景数组
+        JSONArray scenes = parent.getJSONArray("scenes");
+        HashMap<String, String> resMap = new HashMap<>();
+        for (Object scene : scenes) {
+            // 场景对象
+            JSONObject row = JSONObject.parseObject(scene.toString());
+            resMap.put(row.getString("sceneCode"), row.getString("icon"));
+        }
+        log.info("处理导览图片完成, 图片数量: {}", resMap.size());
+        return resMap;
+
+    }
+
+
+
+    /**
+     * 下载导览封面图片
+     * @param iconMap 需要下载的缩略图
+     * @param workId
+     */
+    private void downloadGuideThumb(Map<String, String> iconMap, String workId){
+        String savePath = configConstant.serverBasePath + "/download/" + workId + "/images";
+        for (Map.Entry<String, String> m : iconMap.entrySet()) {
+            String sceneCode = m.getKey();
+            String ossUrl = m.getValue();
+            HttpUtil.downloadFile(ossUrl, savePath);
+            log.info("场景:{} 保存路径: {} 完成", sceneCode, savePath);
+        }
+        log.info("下载导览图片完成: {}", savePath);
     }
 
 
     /**
+     * 下载someData.json
+     */
+    private void downloadSomeDataJson(String workId){
+        String baseUrl = "https://4dkk.4dage.com/720yun_fd_manage/";
+        String url = baseUrl + workId + "/someData.json?m="+ System.currentTimeMillis();
+        String savePath = configConstant.serverBasePath + "/download/" + workId + "/someData.json";
+        HttpUtil.downloadFile(url, savePath);
+        log.info("下载someData.json完成: {}", workId);
+    }
+
+    /**
      * 下载oss目录
      *  @outPath 下载时会以目录形式下载, 指定输出根目录
+     *
+     *
      */
-    private void downloadOss(String sceneCode, String outPath, String bucket){
+    private void downloadOss(String sceneCode,  String bucket, String workId){
         String cmd = CmdConstant.OSSUTIL_DOWNLOAD_DIR;
         String inPath = PathConstant.OSS_720YUN_MANAGE + File.separator + sceneCode;
+        String outPath = configConstant.serverBasePath + "/" + workId;
         cmd = cmd.replaceAll("@inPath", inPath);
         cmd = cmd.replaceAll("@outPath", outPath);
         cmd = cmd.replaceAll("@bucket", bucket);
@@ -81,3 +179,5 @@ public class AliOssServiceImpl implements AliOssService {
         log.info("目录下载完成: {}, 耗时:{} s" , sceneCode, (end-start)/1000 );
     }
 }
+
+

+ 21 - 6
gis_web/src/main/java/com/gis/web/controller/AliOssController.java

@@ -1,5 +1,6 @@
 package com.gis.web.controller;
 
+import com.gis.YunDto;
 import com.gis.common.util.Result;
 import com.gis.service.AliOssService;
 import io.swagger.annotations.Api;
@@ -7,7 +8,7 @@ import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
-import java.util.List;
+import javax.validation.Valid;
 
 /**
  * Created by owen on 2022/3/9 0002 15:46
@@ -22,12 +23,26 @@ public class AliOssController {
     @Autowired
     AliOssService aliOssService;
 
-    @PostMapping("download/{evn}")
-    @ApiOperation(value = "720yun-批量下载场景数据", notes = "批量下载, 场景码以逗号分隔, " +
-            "env: sit,全景看看sit, pro:全景看看pro;  workId:作品id, 用来做目录(允许为空)")
-    public Result batchDownload(@RequestBody List<String> code, @PathVariable String evn, String workId){
-        return aliOssService.batchDownload(code, evn, workId);
+    /**
+     * 目录结构
+     *  workId
+     *      -images: 导览缩略图
+     *      -someData.json  作品someData.json
+     *      - xxx 场景码目录
+     * @return
+     */
+    @PostMapping("download")
+    @ApiOperation(value = "720yun-批量下载场景数据", notes = "批量下载, 场景码以逗号分隔")
+    public Result batchDownload(@Valid @RequestBody YunDto param){
+        return aliOssService.batchDownload(param);
     }
 
 
+
+
+
+
+
+
+
 }