update 重构 使用 Spring 简化 oss 模块代码
This commit is contained in:
parent
d52ece745e
commit
ad18449753
@ -1,6 +1,5 @@
|
|||||||
package com.ruoyi.oss.enumd;
|
package com.ruoyi.oss.enumd;
|
||||||
|
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
|
||||||
import com.ruoyi.oss.service.impl.AliyunOssStrategy;
|
import com.ruoyi.oss.service.impl.AliyunOssStrategy;
|
||||||
import com.ruoyi.oss.service.impl.MinioOssStrategy;
|
import com.ruoyi.oss.service.impl.MinioOssStrategy;
|
||||||
import com.ruoyi.oss.service.impl.QcloudOssStrategy;
|
import com.ruoyi.oss.service.impl.QcloudOssStrategy;
|
||||||
@ -39,25 +38,15 @@ public enum OssEnumd {
|
|||||||
|
|
||||||
private final String value;
|
private final String value;
|
||||||
|
|
||||||
private final Class<?> serviceClass;
|
private final Class<?> beanClass;
|
||||||
|
|
||||||
public static Class<?> getServiceClass(String value) {
|
public static OssEnumd find(String value) {
|
||||||
for (OssEnumd clazz : values()) {
|
for (OssEnumd enumd : values()) {
|
||||||
if (clazz.getValue().equals(value)) {
|
if (enumd.getValue().equals(value)) {
|
||||||
return clazz.getServiceClass();
|
return enumd;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static String getServiceName(String value) {
|
|
||||||
for (OssEnumd clazz : values()) {
|
|
||||||
if (clazz.getValue().equals(value)) {
|
|
||||||
return StringUtils.uncapitalize(clazz.getServiceClass().getSimpleName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package com.ruoyi.oss.factory;
|
|||||||
import com.ruoyi.common.utils.JsonUtils;
|
import com.ruoyi.common.utils.JsonUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.common.utils.redis.RedisUtils;
|
import com.ruoyi.common.utils.redis.RedisUtils;
|
||||||
import com.ruoyi.common.utils.reflect.ReflectUtils;
|
import com.ruoyi.common.utils.spring.SpringUtils;
|
||||||
import com.ruoyi.oss.constant.OssConstant;
|
import com.ruoyi.oss.constant.OssConstant;
|
||||||
import com.ruoyi.oss.enumd.OssEnumd;
|
import com.ruoyi.oss.enumd.OssEnumd;
|
||||||
import com.ruoyi.oss.exception.OssException;
|
import com.ruoyi.oss.exception.OssException;
|
||||||
@ -12,9 +12,6 @@ import com.ruoyi.oss.service.IOssStrategy;
|
|||||||
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传Factory
|
* 文件上传Factory
|
||||||
*
|
*
|
||||||
@ -23,20 +20,16 @@ import java.util.concurrent.ConcurrentHashMap;
|
|||||||
@Slf4j
|
@Slf4j
|
||||||
public class OssFactory {
|
public class OssFactory {
|
||||||
|
|
||||||
/**
|
|
||||||
* 服务实例缓存
|
|
||||||
*/
|
|
||||||
private static final Map<String, IOssStrategy> SERVICES = new ConcurrentHashMap<>();
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 初始化工厂
|
* 初始化工厂
|
||||||
*/
|
*/
|
||||||
public static void init() {
|
public static void init() {
|
||||||
log.info("初始化OSS工厂");
|
log.info("初始化OSS工厂");
|
||||||
RedisUtils.subscribe(OssConstant.CACHE_CONFIG_KEY, String.class, type -> {
|
RedisUtils.subscribe(OssConstant.CACHE_CONFIG_KEY, String.class, type -> {
|
||||||
// 没有的实例不处理
|
AbstractOssStrategy strategy = getStrategy(type);
|
||||||
if (SERVICES.containsKey(type)) {
|
// 未初始化不处理
|
||||||
refreshService(type);
|
if (strategy.isInit) {
|
||||||
|
refresh(type);
|
||||||
log.info("订阅刷新OSS配置 => " + type);
|
log.info("订阅刷新OSS配置 => " + type);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -58,24 +51,29 @@ public class OssFactory {
|
|||||||
* 根据类型获取实例
|
* 根据类型获取实例
|
||||||
*/
|
*/
|
||||||
public static IOssStrategy instance(String type) {
|
public static IOssStrategy instance(String type) {
|
||||||
IOssStrategy service = SERVICES.get(type);
|
OssEnumd enumd = OssEnumd.find(type);
|
||||||
if (service == null) {
|
if (enumd == null) {
|
||||||
refreshService(type);
|
throw new OssException("文件存储服务类型无法找到!");
|
||||||
service = SERVICES.get(type);
|
|
||||||
}
|
}
|
||||||
return service;
|
AbstractOssStrategy strategy = getStrategy(type);
|
||||||
|
if (!strategy.isInit) {
|
||||||
|
refresh(type);
|
||||||
|
}
|
||||||
|
return strategy;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void refreshService(String type) {
|
private static void refresh(String type) {
|
||||||
Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + type);
|
Object json = RedisUtils.getCacheObject(OssConstant.SYS_OSS_KEY + type);
|
||||||
OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class);
|
OssProperties properties = JsonUtils.parseObject(json.toString(), OssProperties.class);
|
||||||
if (properties == null) {
|
if (properties == null) {
|
||||||
throw new OssException("系统异常, '" + type + "'配置信息不存在!");
|
throw new OssException("系统异常, '" + type + "'配置信息不存在!");
|
||||||
}
|
}
|
||||||
// 获取redis配置信息 创建对象 并缓存
|
getStrategy(type).init(properties);
|
||||||
IOssStrategy service = (IOssStrategy) ReflectUtils.newInstance(OssEnumd.getServiceClass(type));
|
}
|
||||||
((AbstractOssStrategy) service).init(properties);
|
|
||||||
SERVICES.put(type, service);
|
private static AbstractOssStrategy getStrategy(String type) {
|
||||||
|
OssEnumd enumd = OssEnumd.find(type);
|
||||||
|
return (AbstractOssStrategy) SpringUtils.getBean(enumd.getBeanClass());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package com.ruoyi.oss.service;
|
package com.ruoyi.oss.service;
|
||||||
|
|
||||||
import com.ruoyi.oss.entity.UploadResult;
|
import com.ruoyi.oss.entity.UploadResult;
|
||||||
|
import com.ruoyi.oss.enumd.OssEnumd;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
@ -15,8 +16,9 @@ public interface IOssStrategy {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取服务商类型
|
* 获取服务商类型
|
||||||
|
* @return
|
||||||
*/
|
*/
|
||||||
String getServiceType();
|
OssEnumd getServiceType();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 文件上传
|
* 文件上传
|
||||||
|
@ -5,6 +5,7 @@ import cn.hutool.core.util.IdUtil;
|
|||||||
import com.ruoyi.common.utils.DateUtils;
|
import com.ruoyi.common.utils.DateUtils;
|
||||||
import com.ruoyi.common.utils.StringUtils;
|
import com.ruoyi.common.utils.StringUtils;
|
||||||
import com.ruoyi.oss.entity.UploadResult;
|
import com.ruoyi.oss.entity.UploadResult;
|
||||||
|
import com.ruoyi.oss.enumd.OssEnumd;
|
||||||
import com.ruoyi.oss.properties.OssProperties;
|
import com.ruoyi.oss.properties.OssProperties;
|
||||||
import com.ruoyi.oss.service.IOssStrategy;
|
import com.ruoyi.oss.service.IOssStrategy;
|
||||||
|
|
||||||
@ -18,14 +19,17 @@ import java.io.InputStream;
|
|||||||
public abstract class AbstractOssStrategy implements IOssStrategy {
|
public abstract class AbstractOssStrategy implements IOssStrategy {
|
||||||
|
|
||||||
protected OssProperties properties;
|
protected OssProperties properties;
|
||||||
|
public boolean isInit = false;
|
||||||
|
|
||||||
public abstract void init(OssProperties properties);
|
public void init(OssProperties properties) {
|
||||||
|
this.properties = properties;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract void createBucket();
|
public abstract void createBucket();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public abstract String getServiceType();
|
public abstract OssEnumd getServiceType();
|
||||||
|
|
||||||
public String getPath(String prefix, String suffix) {
|
public String getPath(String prefix, String suffix) {
|
||||||
// 生成uuid
|
// 生成uuid
|
||||||
|
@ -13,6 +13,7 @@ import com.ruoyi.oss.enumd.OssEnumd;
|
|||||||
import com.ruoyi.oss.exception.OssException;
|
import com.ruoyi.oss.exception.OssException;
|
||||||
import com.ruoyi.oss.properties.OssProperties;
|
import com.ruoyi.oss.properties.OssProperties;
|
||||||
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -22,13 +23,14 @@ import java.io.InputStream;
|
|||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Component
|
||||||
public class AliyunOssStrategy extends AbstractOssStrategy {
|
public class AliyunOssStrategy extends AbstractOssStrategy {
|
||||||
|
|
||||||
private OSSClient client;
|
private OSSClient client;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(OssProperties cloudStorageProperties) {
|
public void init(OssProperties ossProperties) {
|
||||||
properties = cloudStorageProperties;
|
super.init(ossProperties);
|
||||||
try {
|
try {
|
||||||
ClientConfiguration configuration = new ClientConfiguration();
|
ClientConfiguration configuration = new ClientConfiguration();
|
||||||
DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
|
DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider(
|
||||||
@ -38,6 +40,7 @@ public class AliyunOssStrategy extends AbstractOssStrategy {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new OssException("阿里云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
throw new OssException("阿里云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||||
}
|
}
|
||||||
|
isInit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -56,8 +59,8 @@ public class AliyunOssStrategy extends AbstractOssStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getServiceType() {
|
public OssEnumd getServiceType() {
|
||||||
return OssEnumd.ALIYUN.getValue();
|
return OssEnumd.ALIYUN;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -9,6 +9,7 @@ import com.ruoyi.oss.properties.OssProperties;
|
|||||||
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
||||||
import io.minio.*;
|
import io.minio.*;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -18,13 +19,14 @@ import java.io.InputStream;
|
|||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Component
|
||||||
public class MinioOssStrategy extends AbstractOssStrategy {
|
public class MinioOssStrategy extends AbstractOssStrategy {
|
||||||
|
|
||||||
private MinioClient minioClient;
|
private MinioClient minioClient;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(OssProperties cloudStorageProperties) {
|
public void init(OssProperties ossProperties) {
|
||||||
properties = cloudStorageProperties;
|
super.init(ossProperties);
|
||||||
try {
|
try {
|
||||||
minioClient = MinioClient.builder()
|
minioClient = MinioClient.builder()
|
||||||
.endpoint(properties.getEndpoint())
|
.endpoint(properties.getEndpoint())
|
||||||
@ -34,6 +36,7 @@ public class MinioOssStrategy extends AbstractOssStrategy {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new OssException("Minio存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
throw new OssException("Minio存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||||
}
|
}
|
||||||
|
isInit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -56,8 +59,8 @@ public class MinioOssStrategy extends AbstractOssStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getServiceType() {
|
public OssEnumd getServiceType() {
|
||||||
return OssEnumd.MINIO.getValue();
|
return OssEnumd.MINIO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -11,8 +11,11 @@ import com.ruoyi.common.utils.StringUtils;
|
|||||||
import com.ruoyi.oss.entity.UploadResult;
|
import com.ruoyi.oss.entity.UploadResult;
|
||||||
import com.ruoyi.oss.enumd.OssEnumd;
|
import com.ruoyi.oss.enumd.OssEnumd;
|
||||||
import com.ruoyi.oss.exception.OssException;
|
import com.ruoyi.oss.exception.OssException;
|
||||||
|
import com.ruoyi.oss.factory.OssFactory;
|
||||||
import com.ruoyi.oss.properties.OssProperties;
|
import com.ruoyi.oss.properties.OssProperties;
|
||||||
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
@ -22,13 +25,14 @@ import java.io.InputStream;
|
|||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Component
|
||||||
public class QcloudOssStrategy extends AbstractOssStrategy {
|
public class QcloudOssStrategy extends AbstractOssStrategy {
|
||||||
|
|
||||||
private COSClient client;
|
private COSClient client;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(OssProperties cloudStorageProperties) {
|
public void init(OssProperties ossProperties) {
|
||||||
properties = cloudStorageProperties;
|
super.init(ossProperties);
|
||||||
try {
|
try {
|
||||||
COSCredentials credentials = new BasicCOSCredentials(
|
COSCredentials credentials = new BasicCOSCredentials(
|
||||||
properties.getAccessKey(), properties.getSecretKey());
|
properties.getAccessKey(), properties.getSecretKey());
|
||||||
@ -46,6 +50,7 @@ public class QcloudOssStrategy extends AbstractOssStrategy {
|
|||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
throw new OssException("腾讯云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||||
}
|
}
|
||||||
|
isInit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -64,8 +69,8 @@ public class QcloudOssStrategy extends AbstractOssStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getServiceType() {
|
public OssEnumd getServiceType() {
|
||||||
return OssEnumd.QCLOUD.getValue();
|
return OssEnumd.QCLOUD;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -10,8 +10,11 @@ import com.qiniu.util.Auth;
|
|||||||
import com.ruoyi.oss.entity.UploadResult;
|
import com.ruoyi.oss.entity.UploadResult;
|
||||||
import com.ruoyi.oss.enumd.OssEnumd;
|
import com.ruoyi.oss.enumd.OssEnumd;
|
||||||
import com.ruoyi.oss.exception.OssException;
|
import com.ruoyi.oss.exception.OssException;
|
||||||
|
import com.ruoyi.oss.factory.OssFactory;
|
||||||
import com.ruoyi.oss.properties.OssProperties;
|
import com.ruoyi.oss.properties.OssProperties;
|
||||||
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
import com.ruoyi.oss.service.abstractd.AbstractOssStrategy;
|
||||||
|
import org.springframework.beans.factory.InitializingBean;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
|
|
||||||
@ -20,15 +23,17 @@ import java.io.InputStream;
|
|||||||
*
|
*
|
||||||
* @author Lion Li
|
* @author Lion Li
|
||||||
*/
|
*/
|
||||||
|
@Component
|
||||||
public class QiniuOssStrategy extends AbstractOssStrategy {
|
public class QiniuOssStrategy extends AbstractOssStrategy {
|
||||||
|
|
||||||
private UploadManager uploadManager;
|
private UploadManager uploadManager;
|
||||||
private BucketManager bucketManager;
|
private BucketManager bucketManager;
|
||||||
private Auth auth;
|
private Auth auth;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(OssProperties cloudStorageProperties) {
|
public void init(OssProperties ossProperties) {
|
||||||
properties = cloudStorageProperties;
|
super.init(ossProperties);
|
||||||
try {
|
try {
|
||||||
Configuration config = new Configuration(getRegion(properties.getRegion()));
|
Configuration config = new Configuration(getRegion(properties.getRegion()));
|
||||||
// https设置
|
// https设置
|
||||||
@ -36,15 +41,12 @@ public class QiniuOssStrategy extends AbstractOssStrategy {
|
|||||||
config.useHttpsDomains = "Y".equals(properties.getIsHttps());
|
config.useHttpsDomains = "Y".equals(properties.getIsHttps());
|
||||||
uploadManager = new UploadManager(config);
|
uploadManager = new UploadManager(config);
|
||||||
auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
|
auth = Auth.create(properties.getAccessKey(), properties.getSecretKey());
|
||||||
String bucketName = properties.getBucketName();
|
|
||||||
bucketManager = new BucketManager(auth, config);
|
bucketManager = new BucketManager(auth, config);
|
||||||
|
createBucket();
|
||||||
if (!ArrayUtil.contains(bucketManager.buckets(), bucketName)) {
|
|
||||||
bucketManager.createBucket(bucketName, properties.getRegion());
|
|
||||||
}
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new OssException("七牛云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
throw new OssException("七牛云存储配置错误! 请检查系统配置:[" + e.getMessage() + "]");
|
||||||
}
|
}
|
||||||
|
isInit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -61,8 +63,8 @@ public class QiniuOssStrategy extends AbstractOssStrategy {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getServiceType() {
|
public OssEnumd getServiceType() {
|
||||||
return OssEnumd.QINIU.getValue();
|
return OssEnumd.QINIU;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -78,7 +78,7 @@ public class SysOssServiceImpl implements ISysOssService {
|
|||||||
.setFileSuffix(suffix)
|
.setFileSuffix(suffix)
|
||||||
.setFileName(uploadResult.getFilename())
|
.setFileName(uploadResult.getFilename())
|
||||||
.setOriginalName(originalfileName)
|
.setOriginalName(originalfileName)
|
||||||
.setService(storage.getServiceType());
|
.setService(storage.getServiceType().getValue());
|
||||||
baseMapper.insert(oss);
|
baseMapper.insert(oss);
|
||||||
return oss;
|
return oss;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user