mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-17 10:36:31 +08:00
commit:升级到vue3,更新最近工作流技术栈,支持sa-token
This commit is contained in:
29
OrangeFormsOpen-MybatisFlex/common/common-minio/pom.xml
Normal file
29
OrangeFormsOpen-MybatisFlex/common/common-minio/pom.xml
Normal file
@@ -0,0 +1,29 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<artifactId>common</artifactId>
|
||||
<groupId>com.orangeforms</groupId>
|
||||
<version>1.0.0</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>common-minio</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<name>common-minio</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>io.minio</groupId>
|
||||
<artifactId>minio</artifactId>
|
||||
<version>${minio.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.orangeforms</groupId>
|
||||
<artifactId>common-core</artifactId>
|
||||
<version>1.0.0</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,56 @@
|
||||
package com.orangeforms.common.minio.config;
|
||||
|
||||
import com.orangeforms.common.core.exception.MyRuntimeException;
|
||||
import com.orangeforms.common.minio.wrapper.MinioTemplate;
|
||||
import io.minio.BucketExistsArgs;
|
||||
import io.minio.MakeBucketArgs;
|
||||
import io.minio.MinioClient;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* common-minio模块的自动配置引导类。仅当配置项minio.enabled为true的时候加载。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2024-07-02
|
||||
*/
|
||||
@EnableConfigurationProperties(MinioProperties.class)
|
||||
@ConditionalOnProperty(prefix = "minio", name = "enabled")
|
||||
public class MinioAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 将minio原生的客户端类封装成bean对象,便于集成,同时也可以灵活使用客户端的所有功能。
|
||||
*
|
||||
* @param p 属性配置对象。
|
||||
* @return minio的原生客户端对象。
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MinioClient minioClient(MinioProperties p) {
|
||||
try {
|
||||
MinioClient client = MinioClient.builder()
|
||||
.endpoint(p.getEndpoint()).credentials(p.getAccessKey(), p.getSecretKey()).build();
|
||||
if (!client.bucketExists(BucketExistsArgs.builder().bucket(p.getBucketName()).build())) {
|
||||
client.makeBucket(MakeBucketArgs.builder().bucket(p.getBucketName()).build());
|
||||
}
|
||||
return client;
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装的minio模板类。
|
||||
*
|
||||
* @param p 属性配置对象。
|
||||
* @param c minio的原生客户端bean对象。
|
||||
* @return minio模板的bean对象。
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MinioTemplate minioTemplate(MinioProperties p, MinioClient c) {
|
||||
return new MinioTemplate(p, c);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.orangeforms.common.minio.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* common-minio模块的配置类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2024-07-02
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioProperties {
|
||||
|
||||
/**
|
||||
* 访问入口地址。
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 访问安全的key。
|
||||
*/
|
||||
private String accessKey;
|
||||
/**
|
||||
* 访问安全的密钥。
|
||||
*/
|
||||
private String secretKey;
|
||||
/**
|
||||
* 缺省桶名称。
|
||||
*/
|
||||
private String bucketName;
|
||||
}
|
||||
@@ -0,0 +1,115 @@
|
||||
package com.orangeforms.common.minio.util;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import cn.hutool.core.util.BooleanUtil;
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.orangeforms.common.core.upload.UpDownloaderFactory;
|
||||
import com.orangeforms.common.core.upload.UploadResponseInfo;
|
||||
import com.orangeforms.common.core.upload.BaseUpDownloader;
|
||||
import com.orangeforms.common.core.upload.UploadStoreTypeEnum;
|
||||
import com.orangeforms.common.minio.wrapper.MinioTemplate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
|
||||
/**
|
||||
* 基于Minio上传和下载文件操作的工具类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2024-07-02
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
@ConditionalOnProperty(prefix = "minio", name = "enabled")
|
||||
public class MinioUpDownloader extends BaseUpDownloader {
|
||||
|
||||
@Autowired
|
||||
private MinioTemplate minioTemplate;
|
||||
@Autowired
|
||||
private UpDownloaderFactory factory;
|
||||
|
||||
@PostConstruct
|
||||
public void doRegister() {
|
||||
factory.registerUpDownloader(UploadStoreTypeEnum.MINIO_SYSTEM, this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResponseInfo doUpload(
|
||||
String serviceContextPath,
|
||||
String rootBaseDir,
|
||||
String modelName,
|
||||
String fieldName,
|
||||
Boolean asImage,
|
||||
MultipartFile uploadFile) throws IOException {
|
||||
String uploadPath = super.makeFullPath(null, modelName, fieldName, asImage);
|
||||
return this.doUploadInternally(serviceContextPath, uploadPath, asImage, uploadFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadResponseInfo doUpload(
|
||||
String serviceContextPath,
|
||||
String rootBaseDir,
|
||||
String uriPath,
|
||||
MultipartFile uploadFile) throws IOException {
|
||||
String uploadPath = super.makeFullPath(null, uriPath);
|
||||
return this.doUploadInternally(serviceContextPath, uploadPath, false, uploadFile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDownload(
|
||||
String rootBaseDir,
|
||||
String modelName,
|
||||
String fieldName,
|
||||
String fileName,
|
||||
Boolean asImage,
|
||||
HttpServletResponse response) throws IOException {
|
||||
String uploadPath = this.makeFullPath(null, modelName, fieldName, asImage);
|
||||
String fullFileanme = uploadPath + "/" + fileName;
|
||||
this.downloadInternal(fullFileanme, fileName, response);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDownload(
|
||||
String rootBaseDir,
|
||||
String uriPath,
|
||||
String fileName,
|
||||
HttpServletResponse response) throws IOException {
|
||||
StringBuilder pathBuilder = new StringBuilder(128);
|
||||
if (StrUtil.isNotBlank(uriPath)) {
|
||||
pathBuilder.append(uriPath);
|
||||
}
|
||||
pathBuilder.append("/");
|
||||
String fullFileanme = pathBuilder.append(fileName).toString();
|
||||
this.downloadInternal(fullFileanme, fileName, response);
|
||||
}
|
||||
|
||||
private UploadResponseInfo doUploadInternally(
|
||||
String serviceContextPath,
|
||||
String uploadPath,
|
||||
Boolean asImage,
|
||||
MultipartFile uploadFile) throws IOException {
|
||||
UploadResponseInfo responseInfo = super.verifyUploadArgument(asImage, uploadFile);
|
||||
if (BooleanUtil.isTrue(responseInfo.getUploadFailed())) {
|
||||
return responseInfo;
|
||||
}
|
||||
responseInfo.setUploadPath(uploadPath);
|
||||
super.fillUploadResponseInfo(responseInfo, serviceContextPath, uploadFile.getOriginalFilename());
|
||||
minioTemplate.putObject(uploadPath + "/" + responseInfo.getFilename(), uploadFile.getInputStream());
|
||||
return responseInfo;
|
||||
}
|
||||
|
||||
private void downloadInternal(String fullFileanme, String fileName, HttpServletResponse response) throws IOException {
|
||||
response.setHeader("content-type", "application/octet-stream");
|
||||
response.setContentType("application/octet-stream");
|
||||
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
|
||||
InputStream in = minioTemplate.getStream(fullFileanme);
|
||||
IoUtil.copy(in, response.getOutputStream());
|
||||
in.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,199 @@
|
||||
package com.orangeforms.common.minio.wrapper;
|
||||
|
||||
import com.orangeforms.common.core.exception.MyRuntimeException;
|
||||
import com.orangeforms.common.minio.config.MinioProperties;
|
||||
import io.minio.*;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 封装的minio客户端模板类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2024-07-02
|
||||
*/
|
||||
@Slf4j
|
||||
public class MinioTemplate {
|
||||
|
||||
private static final String TMP_DIR = System.getProperty("java.io.tmpdir") + File.separator;
|
||||
private final MinioProperties properties;
|
||||
private final MinioClient client;
|
||||
|
||||
public MinioTemplate(MinioProperties properties, MinioClient client) {
|
||||
super();
|
||||
this.properties = properties;
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断bucket是否存在。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @return 存在返回true,否则false。
|
||||
*/
|
||||
public boolean bucketExists(String bucketName) {
|
||||
try {
|
||||
return client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建桶。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
*/
|
||||
public void makeBucket(String bucketName) {
|
||||
try {
|
||||
if (!client.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build())) {
|
||||
client.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 存放对象。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @param filename 本地上传的文件名称。
|
||||
*/
|
||||
public void putObject(String bucketName, String objectName, String filename) {
|
||||
try {
|
||||
this.putObject(bucketName, objectName, new FileInputStream(filename));
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 存放对象。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @param filename 本地上传的文件名称。
|
||||
*/
|
||||
public void putObject(String objectName, String filename) {
|
||||
try {
|
||||
this.putObject(properties.getBucketName(), objectName, filename);
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取输入流并存放。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @param stream 读取后上传的文件流。
|
||||
*/
|
||||
public void putObject(String bucketName, String objectName, InputStream stream) {
|
||||
try {
|
||||
client.putObject(PutObjectArgs.builder()
|
||||
.bucket(bucketName).object(objectName).stream(stream, stream.available(), -1).build());
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
} finally {
|
||||
try {
|
||||
stream.close();
|
||||
} catch (Exception e) {
|
||||
log.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取输入流并存放。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @param stream 读取后上传的文件流。
|
||||
*/
|
||||
public void putObject(String objectName, InputStream stream) {
|
||||
this.putObject(properties.getBucketName(), objectName, stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除对象。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
*/
|
||||
public void removeObject(String bucketName, String objectName) {
|
||||
try {
|
||||
client.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除对象。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
*/
|
||||
public void removeObject(String objectName) {
|
||||
this.removeObject(properties.getBucketName(), objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件输入流。
|
||||
*
|
||||
* @param bucket 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @return 文件的输入流。
|
||||
*/
|
||||
public InputStream getStream(String bucket, String objectName) {
|
||||
try {
|
||||
return client.getObject(GetObjectArgs.builder().bucket(bucket).object(objectName).build());
|
||||
} catch (Exception e) {
|
||||
throw new MyRuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件输入流。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @return 文件的输入流。
|
||||
*/
|
||||
public InputStream getStream(String objectName) {
|
||||
return this.getStream(properties.getBucketName(), objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存储的文件对象。
|
||||
*
|
||||
* @param bucket 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @return 读取后存储到文件的文件对象。
|
||||
*/
|
||||
public File getFile(String bucket, String objectName) throws IOException {
|
||||
InputStream in = getStream(bucket, objectName);
|
||||
File dir = new File(TMP_DIR);
|
||||
if (!dir.exists() || dir.isFile()) {
|
||||
dir.mkdirs();
|
||||
}
|
||||
File file = new File(TMP_DIR + objectName);
|
||||
FileUtils.copyInputStreamToFile(in, file);
|
||||
in.close();
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存储的文件对象。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @return 读取后存储到文件的文件对象。
|
||||
*/
|
||||
public File getFile(String objectName) throws IOException {
|
||||
return this.getFile(properties.getBucketName(), objectName);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
com.orangeforms.common.minio.config.MinioAutoConfiguration
|
||||
Reference in New Issue
Block a user