|
@@ -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;
|
|
|
+ }
|
|
|
+}
|