Przeglądaj źródła

添加私有云配置。
添加dataUrlPrefix,以指定数据下载的url前缀
修改ossServiceImpl,私有云则复制到本地指定的文件系统中

LiHaoyu 4 lat temu
rodzic
commit
58c4c1b7ee

+ 12 - 0
src/main/java/net/mooctest/www/android_auto_test/common/DataUploadInfo.java

@@ -0,0 +1,12 @@
+package net.mooctest.www.android_auto_test.common;
+
+import lombok.Data;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Component;
+
+@Component
+@Data
+public class DataUploadInfo {
+    @Value("${data_url_prefix}")
+    private String dataUrlPrefix;
+}

+ 7 - 6
src/main/java/net/mooctest/www/android_auto_test/services/Impl/OssServiceImpl.java

@@ -5,8 +5,8 @@ import com.aliyun.oss.model.GetObjectRequest;
 import net.mooctest.www.android_auto_test.common.constant.Consts;
 import net.mooctest.www.android_auto_test.services.OssService;
 import net.mooctest.www.android_auto_test.utils.AddressUtil;
-import net.mooctest.www.android_auto_test.utils.FileUtil;
 import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
 import org.springframework.stereotype.Service;
 
 import java.io.*;
@@ -16,6 +16,7 @@ import java.net.URL;
  * @author henrylee
  */
 @Service
+@ConditionalOnExpression("${is_private}==false")
 public class OssServiceImpl implements OssService {
 
     @Value("${oss.endPoint}")
@@ -84,11 +85,11 @@ public class OssServiceImpl implements OssService {
 
     @Override
     public String uploadInputStreamToTraceDir(InputStream is, String traceId, String fileName){
-            String filePath = Consts.TRACE_DATA_PATH + traceId + "/" + fileName;
-            OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
-            client.putObject(bucketName, filePath , is);
-            client.shutdown();
-            return buildHttpPath(filePath);
+        String filePath = Consts.TRACE_DATA_PATH + traceId + "/" + fileName;
+        OSSClient client = new OSSClient(endPoint, accessKeyId, accessKeySecret);
+        client.putObject(bucketName, filePath , is);
+        client.shutdown();
+        return buildHttpPath(filePath);
     }
 
     private String buildHttpPath(String filePath) {

+ 96 - 0
src/main/java/net/mooctest/www/android_auto_test/services/Impl/OssServiceImpl4PrivateCloud.java

@@ -0,0 +1,96 @@
+package net.mooctest.www.android_auto_test.services.Impl;
+
+import net.mooctest.www.android_auto_test.common.constant.Consts;
+import net.mooctest.www.android_auto_test.services.OssService;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
+import org.springframework.stereotype.Service;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.nio.file.Files;
+
+@Service
+@ConditionalOnExpression("${is_private}==true")
+public class OssServiceImpl4PrivateCloud implements OssService {
+
+    /**
+     * 私有云环境下需要将数据上传到的静态文件目录
+     * 请务必通过java启动项指定,比如
+     * java -jar xxx.jar --fs_dir=/Users/myHome/business/download
+     */
+    @Value("${fs_dir}")
+    private String fileSystemDir;
+    /**
+     * 上传文件的下载url前缀
+     */
+    @Value("${data_url_prefix}")
+    private String urlPrefix;
+
+    @Override
+    public String downloadFile(String downloadUrl, String traceId) {
+        return null;
+    }
+
+    @Override
+    public String getDadaJsonDownloadPath(String traceId, String fileName) {
+        return urlPrefix + traceId + "/" + fileName;
+    }
+
+    @Override
+    public String uploadFileToTraceDir(File file, String traceId, String fileName) {
+        try {
+            File dir = new File(String.format("%s/%s/%s", fileSystemDir, Consts.TRACE_DATA_PATH, traceId));
+            if (!dir.exists()){
+                dir.mkdirs();
+            }
+            File dest = new File(String.format("%s/%s/%s/%s", fileSystemDir, Consts.TRACE_DATA_PATH, traceId, fileName));
+            if (dest.exists()){
+                dest.delete();
+            }
+            Files.copy(file.toPath(), dest.toPath());
+            return urlPrefix + traceId + "/" + fileName;
+        }catch (Exception e){
+            e.printStackTrace();
+        }
+        return null;
+    }
+
+    @Override
+    public void uploadFilesToTraceDir(File[] files, String traceId) {
+        for (File file: files){
+            try {
+                File dir = new File(String.format("%s/%s/%s", fileSystemDir, Consts.TRACE_DATA_PATH, traceId));
+                if (!dir.exists()){
+                    dir.mkdirs();
+                }
+                File dest = new File(String.format("%s/%s/%s/%s", fileSystemDir, Consts.TRACE_DATA_PATH, traceId, file.getName()));
+                if (dest.exists()){
+                    dest.delete();
+                }
+                Files.copy(file.toPath(), dest.toPath());
+            }catch (Exception ignored){}
+
+        }
+    }
+
+    @Override
+    public String uploadInputStreamToTraceDir(InputStream is, String traceId, String fileName) {
+        String filePath = Consts.TRACE_DATA_PATH + traceId + "/" + fileName;
+        try {
+            File dir = new File(String.format("%s/%s/%s", fileSystemDir, Consts.TRACE_DATA_PATH, traceId));
+            if (!dir.exists()) {
+                dir.mkdirs();
+            }
+            FileOutputStream fos = new FileOutputStream(String.format("%s/%s", fileSystemDir, filePath));
+            byte[] b = new byte[1024];
+            while ((is.read(b)) != -1) {
+                fos.write(b);
+            }
+            is.close();
+            fos.close();
+        }catch (Exception ignore){}
+        return urlPrefix + traceId + "/" + fileName;
+    }
+}

+ 4 - 1
src/main/java/net/mooctest/www/android_auto_test/utils/TraceDaemon.java

@@ -7,6 +7,7 @@ import com.alibaba.fastjson.JSONObject;
 import com.google.gson.JsonArray;
 import com.google.gson.JsonObject;
 import net.mooctest.www.android_auto_test.common.BeanFactory;
+import net.mooctest.www.android_auto_test.common.DataUploadInfo;
 import net.mooctest.www.android_auto_test.common.constant.Consts;
 import net.mooctest.www.android_auto_test.common.constant.enums.DeviceStatus;
 import net.mooctest.www.android_auto_test.common.constant.enums.TraceStatus;
@@ -30,6 +31,7 @@ public class TraceDaemon extends Thread{
     private TraceService traceService;
     private OssService ossService;
     private ApkService apkService;
+    private DataUploadInfo dataUploadInfo;
 
     public static final String TAG = Thread.currentThread() .getStackTrace()[1].getClassName();
 
@@ -51,6 +53,7 @@ public class TraceDaemon extends Thread{
         traceService = (TraceService) BeanFactory.getBean(TraceService.class);
         ossService = (OssService) BeanFactory.getBean(OssService.class);
         apkService = (ApkService) BeanFactory.getBean(ApkService.class);
+        dataUploadInfo = (DataUploadInfo) BeanFactory.getBean(DataUploadInfo.class);
     }
 
     public void setNeedCrowdSourcedAnalysis(boolean i){
@@ -141,7 +144,7 @@ public class TraceDaemon extends Thread{
         }
         try {
             PrintUtil.print(String.format("Generate bug report by %s's docker", reportType), TAG);
-            String command = String.format("java -jar tasks/%s.jar %s", reportType, traceId);
+            String command = String.format("java -jar tasks/%s.jar %s %s", reportType, traceId, dataUploadInfo.getDataUrlPrefix());
             String result = OsUtil.runCommand(command);
             PrintUtil.print(result, TAG);
 

+ 31 - 1
src/main/resources/application.yaml

@@ -1,6 +1,6 @@
 demo: true
 poll-tasks: false
-spring.profiles.active: dev
+spring.profiles.active: privatecloud
 
 ---
 # 开发环境
@@ -19,6 +19,36 @@ spring:
 server:
     port: 15926
 
+is_private: false
+data_url_prefix: http://mooctest-enterprise-site.oss-cn-shanghai.aliyuncs.com/trace_data/
+
+oss:
+    accessKeyId: LTAI4FdrT3HsfdR5edBVN7ws
+    endPoint: http://oss-cn-shanghai.aliyuncs.com
+    accessKeySecret: yroxrpm46DzTyzHrLBZzS3MRNIicP6
+    bucketName: mooctest-enterprise-site
+
+
+---
+# 私有云环境
+spring:
+    profiles: privatecloud
+    redis:
+        host: 127.0.0.1
+        password:
+        pool:
+            max-active: 8
+            max-idle: 8
+            max-wait: -1
+            min-idle: 0
+        port: 6379
+
+server:
+    port: 15926
+
+is_private: true
+data_url_prefix: http://localhost/download/trace_data/
+
 oss:
     accessKeyId: LTAI4FdrT3HsfdR5edBVN7ws
     endPoint: http://oss-cn-shanghai.aliyuncs.com

BIN
tasks/BugReport.jar