Bladeren bron

file-service 中的一些动态配置全部放到配置文件中, 代码中不应出现域名, 文件系统路径等变量

reghao 1 jaar geleden
bovenliggende
commit
715e3337b4

+ 25 - 0
file/file-service/src/main/java/cn/reghao/tnb/file/app/config/AppProperties.java

@@ -0,0 +1,25 @@
+package cn.reghao.tnb.file.app.config;
+
+import cn.reghao.tnb.file.app.model.constant.OssType;
+import lombok.Getter;
+import lombok.Setter;
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.context.annotation.Configuration;
+
+/**
+ * @author reghao
+ * @date 2024-11-13 15:09:54
+ */
+@Configuration
+@ConfigurationProperties(prefix = "app")
+@Setter
+@Getter
+public class AppProperties {
+    private String siteAddress;
+    private Integer ossType;
+    private Integer avatarChannelCode;
+
+    public AppProperties() {
+        this.ossType = OssType.localOss.getCode();
+    }
+}

+ 0 - 28
file/file-service/src/main/java/cn/reghao/tnb/file/app/config/OssProperties.java

@@ -1,28 +0,0 @@
-package cn.reghao.tnb.file.app.config;
-
-import cn.reghao.tnb.file.app.model.constant.OssType;
-import lombok.Getter;
-import lombok.Setter;
-import org.springframework.boot.context.properties.ConfigurationProperties;
-import org.springframework.stereotype.Component;
-
-import java.util.Collections;
-import java.util.List;
-
-/**
- * @author reghao
- * @date 2021-12-30 11:01:46
- */
-@Getter
-@Setter
-@Component
-@ConfigurationProperties(prefix = "oss")
-public class OssProperties {
-    private int ossType;
-    private List<String> diskDirs;
-
-    public OssProperties() {
-        this.ossType = OssType.localOss.getCode();
-        this.diskDirs = Collections.emptyList();
-    }
-}

+ 4 - 4
file/file-service/src/main/java/cn/reghao/tnb/file/app/config/SpringLifecycle.java

@@ -18,16 +18,16 @@ import org.springframework.stereotype.Component;
 @Slf4j
 @Slf4j
 @Component
 @Component
 public class SpringLifecycle implements ApplicationRunner, DisposableBean {
 public class SpringLifecycle implements ApplicationRunner, DisposableBean {
-    private final OssProperties ossProperties;
+    private final AppProperties appProperties;
     private final StoreConfigMapper storeConfigMapper;
     private final StoreConfigMapper storeConfigMapper;
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final AliyunOss aliyunOss;
     private final AliyunOss aliyunOss;
     private final AliyunService aliyunService;
     private final AliyunService aliyunService;
 
 
-    public SpringLifecycle(OssProperties ossProperties, StoreConfigMapper storeConfigMapper,
+    public SpringLifecycle(AppProperties appProperties, StoreConfigMapper storeConfigMapper,
                            OssConsoleClientFactory ossConsoleClientFactory, AliyunOss aliyunOss,
                            OssConsoleClientFactory ossConsoleClientFactory, AliyunOss aliyunOss,
                            AliyunService aliyunService) {
                            AliyunService aliyunService) {
-        this.ossProperties = ossProperties;
+        this.appProperties = appProperties;
         this.storeConfigMapper = storeConfigMapper;
         this.storeConfigMapper = storeConfigMapper;
         this.ossConsoleClientFactory = ossConsoleClientFactory;
         this.ossConsoleClientFactory = ossConsoleClientFactory;
         this.aliyunOss = aliyunOss;
         this.aliyunOss = aliyunOss;
@@ -36,7 +36,7 @@ public class SpringLifecycle implements ApplicationRunner, DisposableBean {
 
 
     @Override
     @Override
     public void run(ApplicationArguments args) throws Exception {
     public void run(ApplicationArguments args) throws Exception {
-        int ossType = ossProperties.getOssType();
+        int ossType = appProperties.getOssType();
         StoreConfig storeConfig = storeConfigMapper.findByOssType(ossType);
         StoreConfig storeConfig = storeConfigMapper.findByOssType(ossType);
         if (ossType == OssType.localOss.getCode()) {
         if (ossType == OssType.localOss.getCode()) {
             ossConsoleClientFactory.init(storeConfig);
             ossConsoleClientFactory.init(storeConfig);

+ 20 - 15
file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/FileServiceImpl.java

@@ -9,8 +9,8 @@ import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
 import cn.reghao.oss.sdk.model.rest.UploadFileRet;
 import cn.reghao.oss.sdk.model.rest.UploadFileRet;
 import cn.reghao.oss.sdk.OssConsoleClient;
 import cn.reghao.oss.sdk.OssConsoleClient;
 import cn.reghao.oss.sdk.OssStoreClient;
 import cn.reghao.oss.sdk.OssStoreClient;
+import cn.reghao.tnb.file.app.config.AppProperties;
 import cn.reghao.tnb.file.app.config.OssConsoleClientFactory;
 import cn.reghao.tnb.file.app.config.OssConsoleClientFactory;
-import cn.reghao.tnb.file.app.config.OssProperties;
 import cn.reghao.tnb.file.app.db.mapper.JobDetailMapper;
 import cn.reghao.tnb.file.app.db.mapper.JobDetailMapper;
 import cn.reghao.tnb.file.app.model.po.JobDetail;
 import cn.reghao.tnb.file.app.model.po.JobDetail;
 import cn.reghao.tnb.file.app.service.ConvertService;
 import cn.reghao.tnb.file.app.service.ConvertService;
@@ -18,6 +18,7 @@ import cn.reghao.tnb.file.app.util.ImageUtil;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import org.apache.commons.io.FileUtils;
 import org.apache.commons.io.FileUtils;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.apache.dubbo.config.annotation.DubboService;
+import org.springframework.boot.autoconfigure.web.ServerProperties;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
 
 
 import java.io.ByteArrayOutputStream;
 import java.io.ByteArrayOutputStream;
@@ -36,30 +37,34 @@ public class FileServiceImpl implements FileService {
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final int ossType;
     private final int ossType;
     private final ConvertService convertService;
     private final ConvertService convertService;
-    private JobDetailMapper jobDetailMapper;
+    private final JobDetailMapper jobDetailMapper;
+    private final String baseDir;
+    private final AppProperties appProperties;
 
 
-    private FileServiceImpl(OssProperties ossProperties, OssConsoleClientFactory ossConsoleClientFactory,
-                            ConvertService convertService, JobDetailMapper jobDetailMapper) {
+    private FileServiceImpl(OssConsoleClientFactory ossConsoleClientFactory, ConvertService convertService,
+                            JobDetailMapper jobDetailMapper, ServerProperties serverProperties,
+                            AppProperties appProperties) {
         this.ossConsoleClientFactory = ossConsoleClientFactory;
         this.ossConsoleClientFactory = ossConsoleClientFactory;
-        this.ossType = ossProperties.getOssType();
+        this.ossType = appProperties.getOssType();
         this.convertService = convertService;
         this.convertService = convertService;
         this.jobDetailMapper = jobDetailMapper;
         this.jobDetailMapper = jobDetailMapper;
+        this.baseDir = serverProperties.getTomcat().getBasedir().getAbsolutePath();
+        this.appProperties = appProperties;
     }
     }
 
 
     public String getAccountAvatar(long userId) {
     public String getAccountAvatar(long userId) {
-        String avatarUrl = "//picx.zhimg.com/v2-abed1a8c04700ba7d72b45195223e0ff_xl.jpg";
-        String text = String.format("https://tnb.reghao.cn/user/%s", userId);
+        String text = String.format("%s/user/%s", appProperties.getSiteAddress(), userId);
         int width = 256;
         int width = 256;
         int height = 256;
         int height = 256;
-        String filePath = String.format("/opt/tmp/%s.jpg", userId);
+        String filePath = String.format("%s/%s.jpg", baseDir, userId);
+        String avatarUrl = "";
         try {
         try {
             ByteArrayOutputStream baos = ImageUtil.createQRCode(text, width, height);
             ByteArrayOutputStream baos = ImageUtil.createQRCode(text, width, height);
-            int channelId = 103;
-
+            int channelCode = appProperties.getAvatarChannelCode();
             File file = new File(filePath);
             File file = new File(filePath);
             FileUtils.writeByteArrayToFile(file, baos.toByteArray());
             FileUtils.writeByteArrayToFile(file, baos.toByteArray());
             if (ossType == OssType.localOss.getCode()) {
             if (ossType == OssType.localOss.getCode()) {
-                avatarUrl = getImageUrl(channelId, file);
+                avatarUrl = getImageUrl(channelCode, file);
             }
             }
         } catch (Exception e) {
         } catch (Exception e) {
             e.printStackTrace();
             e.printStackTrace();
@@ -70,9 +75,9 @@ public class FileServiceImpl implements FileService {
         return avatarUrl;
         return avatarUrl;
     }
     }
 
 
-    private String getImageUrl(int channelId, File file) throws Exception {
+    private String getImageUrl(int channelCode, File file) throws Exception {
         OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
         OssConsoleClient ossConsoleClient = ossConsoleClientFactory.getOssConsoleClient();
-        ServerInfo serverInfo = ossConsoleClient.getUploadStore(channelId);
+        ServerInfo serverInfo = ossConsoleClient.getUploadStore(channelCode);
         if (serverInfo == null) {
         if (serverInfo == null) {
             return null;
             return null;
         }
         }
@@ -80,10 +85,10 @@ public class FileServiceImpl implements FileService {
         String ossUrl = serverInfo.getOssUrl();
         String ossUrl = serverInfo.getOssUrl();
         String token = serverInfo.getToken();
         String token = serverInfo.getToken();
         OssStoreClient ossStoreClient = new OssStoreClient(ossUrl);
         OssStoreClient ossStoreClient = new OssStoreClient(ossUrl);
-        UploadFileRet uploadFileRet = ossStoreClient.postObject(file, channelId, token);
+        UploadFileRet uploadFileRet = ossStoreClient.postObject(file, channelCode, token);
         if (uploadFileRet != null) {
         if (uploadFileRet != null) {
             String uploadId = uploadFileRet.getUploadId();
             String uploadId = uploadFileRet.getUploadId();
-            ImageInfo imageInfo = ossConsoleClient.getImageInfo(channelId, uploadId);
+            ImageInfo imageInfo = ossConsoleClient.getImageInfo(channelCode, uploadId);
             if (imageInfo != null) {
             if (imageInfo != null) {
                 return imageInfo.getUrl();
                 return imageInfo.getUrl();
             }
             }

+ 3 - 3
file/file-service/src/main/java/cn/reghao/tnb/file/app/rpc/StoreServiceWrapperRouter.java

@@ -9,8 +9,8 @@ import cn.reghao.oss.sdk.model.dto.media.ConvertedImageInfo;
 import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
 import cn.reghao.oss.sdk.model.dto.media.ImageInfo;
 import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
 import cn.reghao.oss.sdk.model.dto.media.VideoInfo;
 import cn.reghao.oss.sdk.OssConsoleClient;
 import cn.reghao.oss.sdk.OssConsoleClient;
+import cn.reghao.tnb.file.app.config.AppProperties;
 import cn.reghao.tnb.file.app.config.OssConsoleClientFactory;
 import cn.reghao.tnb.file.app.config.OssConsoleClientFactory;
-import cn.reghao.tnb.file.app.config.OssProperties;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import cn.reghao.tnb.file.app.model.constant.OssType;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.apache.dubbo.config.annotation.DubboService;
 import org.springframework.stereotype.Service;
 import org.springframework.stereotype.Service;
@@ -25,9 +25,9 @@ public class StoreServiceWrapperRouter implements OssService {
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final OssConsoleClientFactory ossConsoleClientFactory;
     private final int ossType;
     private final int ossType;
 
 
-    public StoreServiceWrapperRouter(OssConsoleClientFactory ossConsoleClientFactory, OssProperties ossProperties) {
+    public StoreServiceWrapperRouter(OssConsoleClientFactory ossConsoleClientFactory, AppProperties appProperties) {
         this.ossConsoleClientFactory = ossConsoleClientFactory;
         this.ossConsoleClientFactory = ossConsoleClientFactory;
-        this.ossType = ossProperties.getOssType();
+        this.ossType = appProperties.getOssType();
     }
     }
 
 
     public ServerInfo getUploadStore(int channelId) throws Exception {
     public ServerInfo getUploadStore(int channelId) throws Exception {

+ 4 - 4
file/file-service/src/main/resources/application.yml

@@ -37,7 +37,7 @@ mybatis:
     map-underscore-to-camel-case: true
     map-underscore-to-camel-case: true
   mapper-locations: classpath*:mapper/**.xml
   mapper-locations: classpath*:mapper/**.xml
   type-aliases-package: cn.reghao.tnb.file.app.model.po
   type-aliases-package: cn.reghao.tnb.file.app.model.po
-oss:
-  ossType: 1
-  diskDirs:
-    - /opt/disk/4f664a07-12b0-4d41-9a0c-26ed873fcd6e
+app:
+  siteAddress: http://tnb.reghao.cn
+  avatarChannelCode: 104
+  ossType: 1