From 7d0facde2378bf5e3709306ed0dfbd9f59967d48 Mon Sep 17 00:00:00 2001 From: "sean.zhou" Date: Mon, 28 Mar 2022 17:47:27 +0800 Subject: [PATCH] Disable the media module by default --- .../oss/model/AliyunOSSConfiguration.java | 3 +++ .../component/oss/model/MinIOConfiguration.java | 3 +++ .../oss/service/impl/AliyunOssServiceImpl.java | 15 ++++++++++----- .../oss/service/impl/MinIOServiceImpl.java | 2 +- .../storage/controller/StorageController.java | 8 +++++--- .../wayline/controller/WaylineFileController.java | 2 +- .../service/impl/WaylineFileServiceImpl.java | 7 +++++-- src/main/resources/application.yml | 2 +- 8 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/dji/sample/component/oss/model/AliyunOSSConfiguration.java b/src/main/java/com/dji/sample/component/oss/model/AliyunOSSConfiguration.java index 32624ea..98b5b0d 100644 --- a/src/main/java/com/dji/sample/component/oss/model/AliyunOSSConfiguration.java +++ b/src/main/java/com/dji/sample/component/oss/model/AliyunOSSConfiguration.java @@ -99,6 +99,9 @@ public class AliyunOSSConfiguration { @Bean @Lazy public OSS ossClient() { + if (!enable) { + return null; + } return new OSSClientBuilder().build(endpoint, accessKey, secretKey); } } diff --git a/src/main/java/com/dji/sample/component/oss/model/MinIOConfiguration.java b/src/main/java/com/dji/sample/component/oss/model/MinIOConfiguration.java index 5768023..1902320 100644 --- a/src/main/java/com/dji/sample/component/oss/model/MinIOConfiguration.java +++ b/src/main/java/com/dji/sample/component/oss/model/MinIOConfiguration.java @@ -82,6 +82,9 @@ public class MinIOConfiguration { @Bean @Lazy public MinioClient minioClient() { + if (!enable) { + return null; + } MinioClient.Builder builder = MinioClient.builder() .endpoint(endpoint) .credentials(accessKey, secretKey); diff --git a/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java b/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java index f9f92d3..5ffae6c 100644 --- a/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java +++ b/src/main/java/com/dji/sample/component/oss/service/impl/AliyunOssServiceImpl.java @@ -27,7 +27,7 @@ import java.util.Date; @Slf4j public class AliyunOssServiceImpl implements IOssService { - @Autowired + @Autowired(required = false) private OSS ossClient; @Override @@ -58,11 +58,16 @@ public class AliyunOssServiceImpl implements IOssService { if (!StringUtils.hasText(bucket) || !StringUtils.hasText(objectKey)) { return null; } - // First check if the object can be fetched. - ossClient.getObject(bucket, objectKey); + try { + // First check if the object can be fetched. + ossClient.getObject(bucket, objectKey); - return ossClient.generatePresignedUrl(bucket, objectKey, - new Date(System.currentTimeMillis() + AliyunOSSConfiguration.expire * 1000)); + return ossClient.generatePresignedUrl(bucket, objectKey, + new Date(System.currentTimeMillis() + AliyunOSSConfiguration.expire * 1000)); + } catch (NullPointerException e) { + e.printStackTrace(); + } + return null; } } diff --git a/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java b/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java index 1b5e74e..76d4e5d 100644 --- a/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java +++ b/src/main/java/com/dji/sample/component/oss/service/impl/MinIOServiceImpl.java @@ -26,7 +26,7 @@ import java.security.NoSuchAlgorithmException; @Slf4j public class MinIOServiceImpl implements IOssService { - @Autowired + @Autowired(required = false) private MinioClient client; @Override diff --git a/src/main/java/com/dji/sample/storage/controller/StorageController.java b/src/main/java/com/dji/sample/storage/controller/StorageController.java index 801146c..7c40607 100644 --- a/src/main/java/com/dji/sample/storage/controller/StorageController.java +++ b/src/main/java/com/dji/sample/storage/controller/StorageController.java @@ -7,6 +7,7 @@ import com.dji.sample.media.model.StsCredentialsDTO; import com.dji.sample.storage.service.IStorageService; import com.dji.sample.storage.service.impl.AliyunStorageServiceImpl; import com.dji.sample.storage.service.impl.MinIOStorageServiceImpl; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -20,13 +21,14 @@ import org.springframework.web.bind.annotation.RestController; */ @RestController @RequestMapping("${url.storage.prefix}${url.storage.version}/workspaces/") +@Slf4j public class StorageController { private IStorageService storageService; @Autowired - private void setOssService(@Autowired AliyunStorageServiceImpl aliyunStorageService, - @Autowired MinIOStorageServiceImpl minIOStorageService) { + private void setOssService(@Autowired(required = false) AliyunStorageServiceImpl aliyunStorageService, + @Autowired(required = false) MinIOStorageServiceImpl minIOStorageService) { if (AliyunOSSConfiguration.enable) { this.storageService = aliyunStorageService; return; @@ -35,7 +37,7 @@ public class StorageController { this.storageService = minIOStorageService; return; } - throw new NullPointerException("storageService is null."); + log.error("storageService is null."); } /** * Get temporary credentials for uploading the media and wayline in DJI Pilot. diff --git a/src/main/java/com/dji/sample/wayline/controller/WaylineFileController.java b/src/main/java/com/dji/sample/wayline/controller/WaylineFileController.java index dbd9025..5af35a3 100644 --- a/src/main/java/com/dji/sample/wayline/controller/WaylineFileController.java +++ b/src/main/java/com/dji/sample/wayline/controller/WaylineFileController.java @@ -75,7 +75,7 @@ public class WaylineFileController { try { response.sendRedirect(url.toString()); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); } } diff --git a/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java b/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java index eb6bc5e..3d25fb6 100644 --- a/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java +++ b/src/main/java/com/dji/sample/wayline/service/impl/WaylineFileServiceImpl.java @@ -15,6 +15,7 @@ import com.dji.sample.wayline.model.WaylineFileDTO; import com.dji.sample.wayline.model.WaylineFileEntity; import com.dji.sample.wayline.model.WaylineQueryParam; import com.dji.sample.wayline.service.IWaylineFileService; +import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -33,6 +34,7 @@ import java.util.stream.Collectors; */ @Service @Transactional +@Slf4j public class WaylineFileServiceImpl implements IWaylineFileService { @Autowired @@ -41,7 +43,8 @@ public class WaylineFileServiceImpl implements IWaylineFileService { private IOssService ossService; @Autowired - private void setOssService(@Autowired AliyunOssServiceImpl aliyunOssService, @Autowired MinIOServiceImpl minIOService) { + private void setOssService(@Autowired(required = false) AliyunOssServiceImpl aliyunOssService, + @Autowired(required = false) MinIOServiceImpl minIOService) { if (AliyunOSSConfiguration.enable) { this.ossService = aliyunOssService; return; @@ -50,7 +53,7 @@ public class WaylineFileServiceImpl implements IWaylineFileService { this.ossService = minIOService; return; } - throw new NullPointerException("ossService is null."); + log.error("ossService is null."); } @Override diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 9cb4e88..8c95358 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -58,7 +58,7 @@ url: # Tutorial: https://help.aliyun.com/document_detail/100624.htm?spm=a2c4g.11186623.0.0.74075e34eIhK7T#concept-xzh-nzk-2gb aliyun: oss: - enable: true + enable: false endpoint: Please enter your endpoint. # Example: https://oss-cn-shenzhen.aliyuncs.com access-key: Please enter your access key. secret-key: Please enter your secret key.