瀏覽代碼

更改oss上传目录

wuweihao 4 年之前
父節點
當前提交
362f5bcb03

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

@@ -14,5 +14,13 @@ public class CmdConstant {
      * */
     public final static String FACE_SWAP = "face_swap -i @input -t @type -o @output";
 
+    /**
+     * ossUtil上传目录
+     * ./ossutil cp -r localfolder/ oss://examplebucket/desfolder/
+     * opt/ossutil/ossutil64 cp -r /root/owen/720yun/vtour/ oss://oss-xiaoan/720yun_fd_manage/
+     *
+     */
+    public final static String OSSUTIL_UPLOAD_DIR = "/opt/ossutil/ossutil64 cp -r @input @output";
+
 
 }

+ 18 - 0
gis_common/src/main/java/com/gis/common/util/AliyunOssUtil.java

@@ -4,6 +4,7 @@ import com.aliyun.oss.OSSClient;
 import com.aliyun.oss.model.OSSObject;
 import com.aliyun.oss.model.OSSObjectSummary;
 import com.aliyun.oss.model.ObjectListing;
+import com.gis.common.constant.CmdConstant;
 import com.gis.common.constant.ConfigConstant;
 import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -82,6 +83,23 @@ public class AliyunOssUtil {
 
 
     /**
+     * 上传目录 需要在Linux环境下使用
+     * @param uploadDir 服务器目录
+     * @param ossDir oss存放路径
+     */
+    public  void uploadDir(String uploadDir, String ossDir) {
+        log.info("目录上传目录");
+        ossDir = "oss://" + configConstant.ossBucket+"/"+ ossDir;
+        String cmd = CmdConstant.OSSUTIL_UPLOAD_DIR;
+        cmd = cmd.replaceAll("@input", uploadDir);
+        cmd = cmd.replaceAll("@output", ossDir);
+        CmdUtils.callShell(cmd);
+        log.info("目录上传完成: {}", ossDir);
+    }
+
+
+
+    /**
      * 上传的数据是文件夹,参数是文件夹路径,key是上传后的文件名
      *
      * @param filepaths key : 原文件路径

+ 22 - 0
gis_common/src/main/java/com/gis/common/util/CmdUtils.java

@@ -40,6 +40,28 @@ public class CmdUtils {
             process.waitFor();
     }
 
+    /**
+     * ossUtil 需要使用这个
+     * 调用算法 xx.sh 脚本
+     * @param command
+     *
+     * 2021-04-25 新增
+     */
+    public static void callShell(String command){
+        log.info("cmd: " + command);
+        try {
+            String[] cmd = new String[]{"/bin/sh", "-c", command};
+            Process process = Runtime.getRuntime().exec(cmd);
+            StreamGobblerLine errorGobbler = new StreamGobblerLine(process.getErrorStream(), "ERROR");
+            errorGobbler.start();
+            StreamGobblerLine outGobbler = new StreamGobblerLine(process.getInputStream(), "STDOUT");
+            outGobbler.start();
+            process.waitFor();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
 
 
     @Test

+ 38 - 9
gis_service/src/main/java/com/gis/service/impl/UploadServiceImpl.java

@@ -2,6 +2,7 @@ package com.gis.service.impl;
 
 import cn.hutool.core.date.DateUtil;
 import cn.hutool.core.io.FileUtil;
+import com.alibaba.fastjson.JSONObject;
 import com.gis.common.constant.CmdConstant;
 import com.gis.common.constant.ConfigConstant;
 import com.gis.common.constant.MsgCode;
@@ -37,6 +38,7 @@ public class UploadServiceImpl implements UploadService {
         String originalFilename = file.getOriginalFilename();
         String suffix = StringUtils.substringAfterLast(originalFilename, ".");
         String time = DateUtil.format(new Date(), "yyyyMMdd_HHmmssSSS");
+        // 目录位置
         String basePath = configConstant.serverBasePath + time;
         String filePath = basePath + "/face." + suffix;
         log.info("filePath: " + filePath);
@@ -48,7 +50,7 @@ public class UploadServiceImpl implements UploadService {
             log.info("文件写入完成");
 
             // 换装并上传
-             ossUrl = cmdReloadAndUploadOss(filePath, faceSwapName, type);
+             ossUrl = cmdReloadAndUploadOss(filePath, basePath, type, time);
             return Result.success(ossUrl);
             //发消息到mq
         } catch (Exception e) {
@@ -58,20 +60,47 @@ public class UploadServiceImpl implements UploadService {
 
     }
 
-    private String cmdReloadAndUploadOss(String input, String faceSwapName, String type) throws Exception {
-        String output = configConstant.serverBasePath + faceSwapName;
-        log.info("output: " + output);
+    /**
+     *
+     * @param input 输入文件地址
+     * @param basePath 输出目录
+     * @param type 合成图片类型
+     * @param dir 目录名称
+     * @return
+     * @throws Exception
+     */
+    private String cmdReloadAndUploadOss(String input, String basePath, String type, String dir) throws Exception {
+        log.info("输出目录: " + basePath);
         String cmd = CmdConstant.FACE_SWAP;
         cmd = cmd.replace("@input", input);
-        cmd = cmd.replace("@output", output);
+        cmd = cmd.replace("@output", basePath);
         cmd = cmd.replace("@type", type);
         log.info("cmd: {}", cmd);
         CmdUtils.callLine(cmd);
-        log.info("换装完成: {}", output);
+        log.info("换装完成: {}", basePath);
 
-        String ossKey = configConstant.ossBasePath + faceSwapName;
-        aliyunOssUtil.upload(output, ossKey);
-        String ossUrl = configConstant.ossDomain + ossKey;
+        // 检查算法是否生成upload.json
+        String uploadJsonPath = basePath + "/upload.json";
+        if (!FileUtil.exist(uploadJsonPath)) {
+            log.error("算法合成失败,upload.json不存在");
+            throw new BaseRuntimeException(MsgCode.e5005, "算法合成失败,upload.json不存在");
+        }
+
+        String str = FileUtil.readUtf8String(uploadJsonPath);
+        JSONObject uploadJson = JSONObject.parseObject(str);
+        log.info("uploadJson返回值: {}", uploadJson);
+        Integer status = uploadJson.getInteger("status");
+        // status=0 正常, 其他异常
+        if (status != 0) {
+            String msg = uploadJson.getString("msg");
+            log.error("算法合成失败: " + msg);
+            throw new BaseRuntimeException(MsgCode.e5005, "算法合成失败: " + msg);
+        }
+
+        // 合成图片上传oss
+        String ossDir = configConstant.ossBasePath + dir;
+        aliyunOssUtil.uploadDir(basePath, ossDir);
+        String ossUrl = configConstant.ossDomain + ossDir;
         log.info("oss上传完成: {}", ossUrl);
         return ossUrl;