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