فهرست منبع

Merge remote-tracking branch 'origin/master'

xuqiang 1 سال پیش
والد
کامیت
224f05c7ee

+ 10 - 1
authentication/src/main/resources/application.yml

@@ -1,6 +1,8 @@
 server:
   port: 8081
 spring:
+  main:
+    allow-bean-definition-overriding: true
   redis:
     redisson:
       address: redis://192.168.0.250:6379
@@ -18,4 +20,11 @@ mybatis-plus:
   configuration:
     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
 config:
-  sso: 0
+  sso: 0
+
+
+minio:
+  url: http://192.168.0.250:9000
+  access-key: v7Zb8XhyLcYNcSHlxvyK
+  secret-key: 58YibMwpxGKtXcekPL065OM4GonC1hf2tFMjI9EN
+  bucket-name: jzg

+ 6 - 0
commons/pom.xml

@@ -55,5 +55,11 @@
             <groupId>cn.hutool</groupId>
             <artifactId>hutool-core</artifactId>
         </dependency>
+
+        <dependency>
+            <groupId>io.minio</groupId>
+            <artifactId>minio</artifactId>
+            <version>${minio.version}</version>
+        </dependency>
     </dependencies>
 </project>

+ 1 - 1
platform/src/main/java/com/jzg/config/MinioConfig.java → commons/src/main/java/com/jzg/commons/config/MinioConfig.java

@@ -1,4 +1,4 @@
-package com.jzg.config;
+package com.jzg.commons.config;
 
 import io.minio.MinioClient;
 import org.springframework.beans.factory.annotation.Value;

+ 12 - 4
platform/src/main/java/com/jzg/util/MinioUtils.java → commons/src/main/java/com/jzg/commons/util/MinioUtils.java

@@ -1,4 +1,4 @@
-package com.jzg.util;
+package com.jzg.commons.util;
 
 import io.minio.*;
 import io.minio.http.Method;
@@ -29,9 +29,17 @@ import java.util.*;
 @Component
 public class MinioUtils {
 
+    private static MinioClient minioClient;
+
     @Autowired
-    private MinioClient minioClient;
+    public void setMinioClient(MinioClient minioClient) {
+        MinioUtils.minioClient = minioClient;
+        log.info("MinioClient has been initialized: {}", minioClient);
+    }
 
+    public static MinioClient getMinioClient() {
+        return MinioUtils.minioClient;
+    }
 
     /**
      * 启动SpringBoot容器的时候初始化Bucket
@@ -357,7 +365,7 @@ public class MinioUtils {
      * @return url
      */
     @SneakyThrows(Exception.class)
-    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {
+    public static String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {
         GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build();
         return minioClient.getPresignedObjectUrl(args);
     }
@@ -370,7 +378,7 @@ public class MinioUtils {
      * @return url
      */
     @SneakyThrows(Exception.class)
-    public String getPresignedObjectUrl(String bucketName, String objectName) {
+    public static String getPresignedObjectUrl(String bucketName, String objectName) {
         GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build();
         return minioClient.getPresignedObjectUrl(args);
     }

+ 3 - 5
platform/src/main/java/com/jzg/controller/FileUploadController.java

@@ -5,9 +5,9 @@ import com.jzg.commons.core.base.BaseController;
 import com.jzg.commons.core.page.HttpResult;
 import com.jzg.commons.entity.po.SysUploadFiles;
 import com.jzg.commons.util.FileUtils;
+import com.jzg.commons.util.MinioUtils;
 import com.jzg.commons.util.idgen.IdGenerate;
 import com.jzg.mapper.SysUploadFilesMapper;
-import com.jzg.util.MinioUtils;
 import io.minio.*;
 import io.swagger.v3.oas.annotations.Operation;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -33,8 +33,6 @@ public class FileUploadController extends BaseController {
     @Autowired
     private MinioClient minioClient;
 
-    @Autowired
-    private MinioUtils minioUtils;
 
     @Autowired
     private SysUploadFilesMapper sysUploadFilesMapper;
@@ -110,7 +108,7 @@ public class FileUploadController extends BaseController {
             sysUploadFiles.setCreateBy(getUserName());
             sysUploadFilesMapper.insert(sysUploadFiles);
         }
-        sysUploadFiles.setDownloadUrl(minioUtils.getPresignedObjectUrl(bucketName,fileName));
+        sysUploadFiles.setDownloadUrl(MinioUtils.getPresignedObjectUrl(bucketName,fileName));
         return sysUploadFiles;
     }
 
@@ -131,7 +129,7 @@ public class FileUploadController extends BaseController {
         sysUploadFiles.setBucketName(bucketName);
         sysUploadFiles.setEtag(objectWriteResponse.etag());
         sysUploadFiles.setCreateTime(LocalDateTime.now());
-        sysUploadFiles.setDownloadUrl(minioUtils.getPresignedObjectUrl(bucketName,objectName));
+        sysUploadFiles.setDownloadUrl(MinioUtils.getPresignedObjectUrl(bucketName,objectName));
         sysUploadFilesMapper.insert(sysUploadFiles);
         return sysUploadFiles;
     }

+ 3 - 6
platform/src/main/java/com/jzg/service/impl/SysSystemServiceImpl.java

@@ -13,11 +13,10 @@ import com.jzg.commons.entity.po.*;
 import com.jzg.commons.entity.vo.SysSystemPermsVo;
 import com.jzg.commons.entity.vo.SysSystemVo;
 import com.jzg.commons.entity.vo.UpAndDownVo;
+import com.jzg.commons.util.MinioUtils;
 import com.jzg.commons.util.StringUtils;
-import com.jzg.commons.util.idgen.IdGenerate;
 import com.jzg.mapper.*;
 import com.jzg.service.SysSystemService;
-import com.jzg.util.MinioUtils;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
@@ -56,8 +55,6 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
     private SysUserJzgInfoMapper sysUserJzgInfoMapper;
     @Autowired
     private SysUserPlateInfoMapper sysUserPlateInfoMapper;
-    @Autowired
-    private MinioUtils minioUtils;
     @Value("${minio.bucket-name}")
     private String bucketName;
 
@@ -178,7 +175,7 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
             sysSystemList.forEach(sysSystem->{
                 SysUploadFiles files = sysUploadFilesMapper.selectById(sysSystem.getSysIcon());
                 if(StringUtils.isNotNull(files)){
-                    sysSystem.setSysIconUrl(minioUtils.getPresignedObjectUrl(bucketName,files.getFileName()));
+                    sysSystem.setSysIconUrl(MinioUtils.getPresignedObjectUrl(bucketName,files.getFileName()));
                 }
                 if(deptMap.containsKey(sysSystem.getSysCode())){
                     int count = deptMap.get(sysSystem.getSysCode()).size();
@@ -197,7 +194,7 @@ public class SysSystemServiceImpl extends ServiceImpl<SysSystemMapper, SysSystem
         if(StringUtils.isNotEmpty(vo.getSysIcon())){
             SysUploadFiles files = sysUploadFilesMapper.selectById(system.getSysIcon());
             if(StringUtils.isNotNull(files)){
-                vo.setSysIconUrl(minioUtils.getPresignedObjectUrl(bucketName,files.getFileName()));
+                vo.setSysIconUrl(MinioUtils.getPresignedObjectUrl(bucketName,files.getFileName()));
             }
         }
         SysUser user = sysUserMapper.selectById(system.getUserId());

+ 0 - 27
tenant/organization/src/main/java/com/jzg/organization/config/MinioConfig.java

@@ -1,27 +0,0 @@
-package com.jzg.organization.config;
-
-import io.minio.MinioClient;
-import org.springframework.beans.factory.annotation.Value;
-import org.springframework.context.annotation.Bean;
-import org.springframework.context.annotation.Configuration;
-
-@Configuration
-public class MinioConfig {
-
-    @Value("${minio.url}")
-    private String minioUrl;
-
-    @Value("${minio.access-key}")
-    private String accessKey;
-
-    @Value("${minio.secret-key}")
-    private String secretKey;
-
-    @Bean
-    public MinioClient minioClient() {
-        return MinioClient.builder()
-                .endpoint(minioUrl)
-                .credentials(accessKey, secretKey)
-                .build();
-    }
-}

+ 0 - 389
tenant/organization/src/main/java/com/jzg/organization/utils/MinioUtils.java

@@ -1,389 +0,0 @@
-package com.jzg.organization.utils;
-
-import io.minio.*;
-import io.minio.http.Method;
-import io.minio.messages.Bucket;
-import io.minio.messages.DeleteObject;
-import io.minio.messages.Item;
-import jakarta.annotation.Resource;
-import lombok.SneakyThrows;
-import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
-import org.springframework.stereotype.Component;
-import org.springframework.web.multipart.MultipartFile;
-import java.io.ByteArrayInputStream;
-import java.io.InputStream;
-import java.io.UnsupportedEncodingException;
-import java.net.URLDecoder;
-import java.util.*;
-
-/**
- * @program: xxkfz-minio
- * @ClassName MinioUtils.java
- * @author: 公众号:小小开发者
- * @create: 2024-03-13 10:55
- * @description: MinIO操作工具类
- **/
-@Slf4j
-@Component
-public class MinioUtils {
-
-
-    @Resource
-    private MinioClient minioClient;
-
-
-    /**
-     * 启动SpringBoot容器的时候初始化Bucket
-     * 如果没有Bucket则创建
-     *
-     * @param bucketName
-     */
-    public void createBucket(String bucketName) {
-        try {
-            if (!bucketExists(bucketName)) {
-                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
-                log.info("创建bucketName = {}完成!", bucketName);
-                return;
-            }
-            log.info("bucketName = {}已存在!策略为:{}", bucketName, getBucketPolicy(bucketName));
-        } catch (Exception e) {
-            log.error("创建bucketName = {}异常!e = {}", bucketName, e);
-        }
-    }
-
-    /**
-     * 判断Bucket是否存在,true:存在,false:不存在
-     *
-     * @param bucketName
-     * @return
-     */
-    @SneakyThrows
-    public boolean bucketExists(String bucketName) {
-        return minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
-    }
-
-    /**
-     * 获得Bucket的策略
-     *
-     * @param bucketName
-     * @return
-     */
-    @SneakyThrows
-    public String getBucketPolicy(String bucketName) {
-        return minioClient.getBucketPolicy(GetBucketPolicyArgs.builder().bucket(bucketName).build());
-    }
-
-    /**
-     * 获得所有Bucket列表
-     *
-     * @return
-     */
-    @SneakyThrows
-    public List<Bucket> getAllBuckets() {
-        return minioClient.listBuckets();
-    }
-
-    /**
-     * 根据bucketName获取其相关信息
-     *
-     * @param bucketName
-     * @return
-     */
-    @SneakyThrows(Exception.class)
-    public Optional<Bucket> getBucket(String bucketName) {
-        return getAllBuckets().stream().filter(b -> b.name().equals(bucketName)).findFirst();
-    }
-
-    /**
-     * 根据bucketName删除Bucket,true:删除成功; false:删除失败,文件或已不存在
-     *
-     * @param bucketName
-     * @throws Exception
-     */
-    @SneakyThrows(Exception.class)
-    public void removeBucket(String bucketName) {
-        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
-    }
-
-
-    /**
-     * 判断文件是否存在
-     *
-     * @param bucketName
-     * @param objectName
-     * @return
-     */
-    public boolean isObjectExist(String bucketName, String objectName) {
-        boolean exist = true;
-        try {
-            minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
-        } catch (Exception e) {
-            log.error("[Minio工具类]>>>> 判断文件是否存在, 异常:", e);
-            exist = false;
-        }
-        return exist;
-    }
-
-    /**
-     * 判断文件夹是否存在
-     *
-     * @param bucketName
-     * @param objectName
-     * @return
-     */
-    public boolean isFolderExist(String bucketName, String objectName) {
-        boolean exist = false;
-        try {
-            Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(objectName).recursive(false).build());
-            for (Result<Item> result : results) {
-                Item item = result.get();
-                if (item.isDir() && objectName.equals(item.objectName())) {
-                    exist = true;
-                }
-            }
-        } catch (Exception e) {
-            log.error("[Minio工具类]>>>> 判断文件夹是否存在,异常:", e);
-            exist = false;
-        }
-        return exist;
-    }
-
-    /**
-     * 根据文件前置查询文件
-     *
-     * @param bucketName 存储桶
-     * @param prefix     前缀
-     * @param recursive  是否使用递归查询
-     * @return MinioItem 列表
-     */
-    @SneakyThrows(Exception.class)
-    public List<Item> getAllObjectsByPrefix(String bucketName, String prefix, boolean recursive) {
-        List<Item> list = new ArrayList<>();
-        Iterable<Result<Item>> objectsIterator = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
-        if (objectsIterator != null) {
-            for (Result<Item> o : objectsIterator) {
-                Item item = o.get();
-                list.add(item);
-            }
-        }
-        return list;
-    }
-
-    /**
-     * 获取文件流
-     *
-     * @param bucketName 存储桶
-     * @param objectName 文件名
-     * @return 二进制流
-     */
-    @SneakyThrows(Exception.class)
-    public InputStream getObject(String bucketName, String objectName) {
-        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
-    }
-
-    /**
-     * 断点下载
-     *
-     * @param bucketName 存储桶
-     * @param objectName 文件名称
-     * @param offset     起始字节的位置
-     * @param length     要读取的长度
-     * @return 二进制流
-     */
-    @SneakyThrows(Exception.class)
-    public InputStream getObject(String bucketName, String objectName, long offset, long length) {
-        return minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).offset(offset).length(length).build());
-    }
-
-    /**
-     * 获取路径下文件列表
-     *
-     * @param bucketName 存储桶
-     * @param prefix     文件名称
-     * @param recursive  是否递归查找,false:模拟文件夹结构查找
-     * @return 二进制流
-     */
-    public Iterable<Result<Item>> listObjects(String bucketName, String prefix, boolean recursive) {
-        return minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).prefix(prefix).recursive(recursive).build());
-    }
-
-    /**
-     * 使用MultipartFile进行文件上传
-     *
-     * @param bucketName  存储桶
-     * @param file        文件名
-     * @param objectName  对象名
-     * @param contentType 类型
-     * @return
-     */
-    @SneakyThrows(Exception.class)
-    public ObjectWriteResponse uploadFile(String bucketName, MultipartFile file, String objectName, String contentType) {
-        InputStream inputStream = file.getInputStream();
-        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).contentType(contentType).stream(inputStream, inputStream.available(), -1).build());
-    }
-
-    /**
-     * 图片上传
-     *
-     * @param bucketName
-     * @param imageBase64
-     * @param imageName
-     * @return
-     */
-    public ObjectWriteResponse uploadImage(String bucketName, String imageBase64, String imageName) {
-        if (!StringUtils.isEmpty(imageBase64)) {
-            InputStream in = base64ToInputStream(imageBase64);
-            String newName = System.currentTimeMillis() + "_" + imageName + ".jpg";
-            String year = String.valueOf(new Date().getYear());
-            String month = String.valueOf(new Date().getMonth());
-            return uploadFile(bucketName, year + "/" + month + "/" + newName, in);
-
-        }
-        return null;
-    }
-
-    public static InputStream base64ToInputStream(String base64) {
-        ByteArrayInputStream stream = null;
-        try {
-            byte[] bytes = Base64.getEncoder().encode(base64.trim().getBytes());
-            stream = new ByteArrayInputStream(bytes);
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-        return stream;
-    }
-
-
-    /**
-     * 上传本地文件
-     *
-     * @param bucketName 存储桶
-     * @param objectName 对象名称
-     * @param fileName   本地文件路径
-     * @return
-     */
-    @SneakyThrows(Exception.class)
-    public ObjectWriteResponse uploadFile(String bucketName, String objectName, String fileName) {
-        return minioClient.uploadObject(UploadObjectArgs.builder().bucket(bucketName).object(objectName).filename(fileName).build());
-    }
-
-    /**
-     * 通过流上传文件
-     *
-     * @param bucketName  存储桶
-     * @param objectName  文件对象
-     * @param inputStream 文件流
-     * @return
-     */
-    @SneakyThrows(Exception.class)
-    public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) {
-        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(inputStream, inputStream.available(), -1).build());
-    }
-
-    /**
-     * 创建文件夹或目录
-     *
-     * @param bucketName 存储桶
-     * @param objectName 目录路径
-     * @return
-     */
-    @SneakyThrows(Exception.class)
-    public ObjectWriteResponse createDir(String bucketName, String objectName) {
-        return minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).object(objectName).stream(new ByteArrayInputStream(new byte[]{}), 0, -1).build());
-    }
-
-    /**
-     * 获取文件信息, 如果抛出异常则说明文件不存在
-     *
-     * @param bucketName 存储桶
-     * @param objectName 文件名称
-     * @return
-     */
-    @SneakyThrows(Exception.class)
-    public String getFileStatusInfo(String bucketName, String objectName) {
-        return minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build()).toString();
-    }
-
-    /**
-     * 拷贝文件
-     *
-     * @param bucketName    存储桶
-     * @param objectName    文件名
-     * @param srcBucketName 目标存储桶
-     * @param srcObjectName 目标文件名
-     */
-    @SneakyThrows(Exception.class)
-    public ObjectWriteResponse copyFile(String bucketName, String objectName, String srcBucketName, String srcObjectName) {
-        return minioClient.copyObject(CopyObjectArgs.builder().source(CopySource.builder().bucket(bucketName).object(objectName).build()).bucket(srcBucketName).object(srcObjectName).build());
-    }
-
-    /**
-     * 删除文件
-     *
-     * @param bucketName 存储桶
-     * @param objectName 文件名称
-     */
-    @SneakyThrows(Exception.class)
-    public void removeFile(String bucketName, String objectName) {
-        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
-    }
-
-    /**
-     * 批量删除文件
-     *
-     * @param bucketName 存储桶
-     * @param keys       需要删除的文件列表
-     * @return
-     */
-    public void removeFiles(String bucketName, List<String> keys) {
-        List<DeleteObject> objects = new LinkedList<>();
-        keys.forEach(s -> {
-            objects.add(new DeleteObject(s));
-            try {
-                removeFile(bucketName, s);
-            } catch (Exception e) {
-                log.error("[Minio工具类]>>>> 批量删除文件,异常:", e);
-            }
-        });
-    }
-
-    /**
-     * 获取文件外链
-     *
-     * @param bucketName 存储桶
-     * @param objectName 文件名
-     * @param expires    过期时间 <=7 秒 (外链有效时间(单位:秒))
-     * @return url
-     */
-    @SneakyThrows(Exception.class)
-    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {
-        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build();
-        return minioClient.getPresignedObjectUrl(args);
-    }
-
-    /**
-     * 获得文件外链
-     *
-     * @param bucketName
-     * @param objectName
-     * @return url
-     */
-    @SneakyThrows(Exception.class)
-    public String getPresignedObjectUrl(String bucketName, String objectName) {
-        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(objectName).method(Method.GET).build();
-        return minioClient.getPresignedObjectUrl(args);
-    }
-
-    /**
-     * 将URLDecoder编码转成UTF8
-     *
-     * @param str
-     * @return
-     * @throws UnsupportedEncodingException
-     */
-    public String getUtf8ByURLDecoder(String str) throws UnsupportedEncodingException {
-        String url = str.replaceAll("%(?![0-9a-fA-F]{2})", "%25");
-        return URLDecoder.decode(url, "UTF-8");
-    }
-}