mirror of
https://gitee.com/orangeform/orange-admin.git
synced 2026-01-18 11:06:36 +08:00
commit:支持app的纯后台服务
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
package com.orange.demo.common.minio.config;
|
||||
|
||||
import com.orange.demo.common.minio.wrapper.MinioTemplate;
|
||||
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 2020-08-08
|
||||
*/
|
||||
@EnableConfigurationProperties(MinioProperties.class)
|
||||
@ConditionalOnProperty(prefix = "minio", name = "enabled")
|
||||
public class MinioAutoConfiguration {
|
||||
|
||||
/**
|
||||
* 将minio原生的客户端类封装成bean对象,便于集成,同时也可以灵活使用客户端的所有功能。
|
||||
*
|
||||
* @param p 属性配置对象。
|
||||
* @return minio的原生客户端对象。
|
||||
*/
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public MinioClient minioClient(MinioProperties p) throws Exception {
|
||||
MinioClient client = new MinioClient(p.getEndpoint(), p.getAccessKey(), p.getSecretKey());
|
||||
if (!client.bucketExists(p.getBucketName())) {
|
||||
client.makeBucket(p.getBucketName());
|
||||
}
|
||||
return client;
|
||||
}
|
||||
|
||||
/**
|
||||
* 封装的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.orange.demo.common.minio.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
/**
|
||||
* common-minio模块的配置类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@Data
|
||||
@ConfigurationProperties(prefix = "minio")
|
||||
public class MinioProperties {
|
||||
|
||||
/**
|
||||
* 访问入口地址。
|
||||
*/
|
||||
private String endpoint;
|
||||
/**
|
||||
* 访问安全的key。
|
||||
*/
|
||||
private String accessKey;
|
||||
/**
|
||||
* 访问安全的密钥。
|
||||
*/
|
||||
private String secretKey;
|
||||
/**
|
||||
* 缺省桶名称。
|
||||
*/
|
||||
private String bucketName;
|
||||
}
|
||||
@@ -0,0 +1,100 @@
|
||||
package com.orange.demo.common.minio.util;
|
||||
|
||||
import cn.hutool.core.io.IoUtil;
|
||||
import com.orange.demo.common.core.constant.ErrorCodeEnum;
|
||||
import com.orange.demo.common.core.upload.UpDownloaderFactory;
|
||||
import com.orange.demo.common.core.upload.UploadResponseInfo;
|
||||
import com.orange.demo.common.core.upload.BaseUpDownloader;
|
||||
import com.orange.demo.common.core.upload.UploadStoreTypeEnum;
|
||||
import com.orange.demo.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 javax.annotation.PostConstruct;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.*;
|
||||
import java.util.Objects;
|
||||
|
||||
/**
|
||||
* 基于Minio上传和下载文件操作的工具类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@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);
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行文件上传操作,将文件数据存入Minio。
|
||||
*
|
||||
* @param serviceContextPath 微服务的上下文路径,如: /admin/upms。
|
||||
* @param rootBaseDir 存放上传文件的根目录。(minio中忽略该值,因为使用了bucket)
|
||||
* @param modelName 所在数据表的实体对象名。
|
||||
* @param fieldName 关联字段的实体对象属性名。
|
||||
* @param uploadFile Http请求中上传的文件对象。
|
||||
* @param asImage 是否为图片对象。图片是无需权限验证的,因此和附件存放在不同的子目录。
|
||||
* @return 上传应答信息对象。该对象始终不为null。
|
||||
* @throws Exception minio抛出的异常。
|
||||
*/
|
||||
@Override
|
||||
public UploadResponseInfo doUpload(
|
||||
String serviceContextPath,
|
||||
String rootBaseDir,
|
||||
String modelName,
|
||||
String fieldName,
|
||||
Boolean asImage,
|
||||
MultipartFile uploadFile) throws Exception {
|
||||
UploadResponseInfo responseInfo = new UploadResponseInfo();
|
||||
if (Objects.isNull(uploadFile) || uploadFile.isEmpty()) {
|
||||
responseInfo.setUploadFailed(true);
|
||||
responseInfo.setErrorMessage(ErrorCodeEnum.INVALID_UPLOAD_FILE_ARGUMENT.getErrorMessage());
|
||||
return responseInfo;
|
||||
}
|
||||
String uploadPath = super.makeFullPath(null, modelName, fieldName, asImage);
|
||||
super.fillUploadResponseInfo(responseInfo, serviceContextPath, uploadFile.getOriginalFilename());
|
||||
minioTemplate.putObject(uploadPath + "/" + responseInfo.getFilename(), uploadFile.getInputStream());
|
||||
return responseInfo;
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行下载操作,从Minio读取数据。并将读取的文件数据直接写入到HttpServletResponse应答对象。
|
||||
*
|
||||
* @param rootBaseDir 文件下载的根目录。(minio中忽略该值,因为使用了bucket)
|
||||
* @param modelName 所在数据表的实体对象名。
|
||||
* @param fieldName 关联字段的实体对象属性名。
|
||||
* @param fileName 文件名。
|
||||
* @param asImage 是否为图片对象。图片是无需权限验证的,因此和附件存放在不同的子目录。
|
||||
* @param response Http 应答对象。
|
||||
*/
|
||||
@Override
|
||||
public void doDownload(
|
||||
String rootBaseDir,
|
||||
String modelName,
|
||||
String fieldName,
|
||||
String fileName,
|
||||
Boolean asImage,
|
||||
HttpServletResponse response) throws Exception {
|
||||
String uploadPath = this.makeFullPath(null, modelName, fieldName, asImage);
|
||||
String fullFileanme = uploadPath + "/" + fileName;
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,209 @@
|
||||
package com.orange.demo.common.minio.wrapper;
|
||||
|
||||
import com.orange.demo.common.minio.config.MinioProperties;
|
||||
import io.minio.MinioClient;
|
||||
import io.minio.ObjectStat;
|
||||
import io.minio.PutObjectOptions;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
|
||||
/**
|
||||
* 封装的minio客户端模板类。
|
||||
*
|
||||
* @author Jerry
|
||||
* @date 2020-08-08
|
||||
*/
|
||||
@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(bucketName);
|
||||
} catch (Exception e) {
|
||||
log.error("", e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建桶。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
*/
|
||||
public void makeBucket(String bucketName) throws Exception {
|
||||
if (!client.bucketExists(bucketName)) {
|
||||
client.makeBucket(bucketName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 存放对象。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @param filename 本地上传的文件名称。
|
||||
*/
|
||||
public void putObject(String bucketName, String objectName, String filename) throws Exception {
|
||||
client.putObject(bucketName, objectName, filename, new PutObjectOptions(-1, -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 存放对象。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @param filename 本地上传的文件名称。
|
||||
*/
|
||||
public void putObject(String objectName, String filename) throws Exception {
|
||||
this.putObject(properties.getBucketName(), objectName, filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取输入流并存放。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @param stream 读取后上传的文件流。
|
||||
*/
|
||||
public void putObject(String bucketName, String objectName, InputStream stream) throws Exception {
|
||||
client.putObject(bucketName, objectName, stream, new PutObjectOptions(stream.available(), -1));
|
||||
}
|
||||
|
||||
/**
|
||||
* 读取输入流并存放。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @param stream 读取后上传的文件流。
|
||||
*/
|
||||
public void putObject(String objectName, InputStream stream) throws Exception {
|
||||
this.putObject(properties.getBucketName(), objectName, stream);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除对象。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
*/
|
||||
public void removeObject(String bucketName, String objectName) throws Exception {
|
||||
client.removeObject(bucketName, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 移除对象。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
*/
|
||||
public void removeObject(String objectName) throws Exception {
|
||||
this.removeObject(properties.getBucketName(), objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件输入流。
|
||||
*
|
||||
* @param bucket 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @return 文件的输入流。
|
||||
*/
|
||||
public InputStream getStream(String bucket, String objectName) throws Exception {
|
||||
return client.getObject(bucket, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文件输入流。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @return 文件的输入流。
|
||||
*/
|
||||
public InputStream getStream(String objectName) throws Exception {
|
||||
return this.getStream(properties.getBucketName(), objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存储的文件对象。
|
||||
*
|
||||
* @param bucket 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @return 读取后存储到文件的文件对象。
|
||||
*/
|
||||
public File getFile(String bucket, String objectName) throws Exception {
|
||||
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);
|
||||
return file;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取存储的文件对象。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @return 读取后存储到文件的文件对象。
|
||||
*/
|
||||
public File getFile(String objectName) throws Exception {
|
||||
return this.getFile(properties.getBucketName(), objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定文件的URL。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @return 指定文件的URL。
|
||||
*/
|
||||
public String getObjectUrl(String bucketName, String objectName) throws Exception {
|
||||
return client.getObjectUrl(bucketName, objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定文件的URL。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @return 指定文件的URL。
|
||||
*/
|
||||
public String getObjectUrl(String objectName) throws Exception {
|
||||
return this.getObjectUrl(properties.getBucketName(), objectName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象状态信息。
|
||||
*
|
||||
* @param bucketName 桶名称。
|
||||
* @param objectName 对象名称。
|
||||
* @return 对象状态和meta信息。
|
||||
*/
|
||||
public ObjectStat statObject(String bucketName, String objectName) throws Exception {
|
||||
return client.statObject(bucketName, objectName, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取对象状态信息。桶名称为配置中的桶名称。
|
||||
*
|
||||
* @param objectName 对象名称。
|
||||
* @return 对象状态和meta信息。
|
||||
*/
|
||||
public ObjectStat statObject(String objectName) throws Exception {
|
||||
return client.statObject(properties.getBucketName(), objectName, null);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.orange.demo.common.minio.config.MinioAutoConfiguration
|
||||
Reference in New Issue
Block a user