update 重写 OSS 模块相关实现 支持动态配置(页面配置)
| | |
| | | @Log(title = "äºåå¨é
ç½®", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{ossConfigIds}") |
| | | public AjaxResult<Void> remove(@NotEmpty(message = "主é®ä¸è½ä¸ºç©º") |
| | | @PathVariable Integer[] ossConfigIds) { |
| | | @PathVariable Long[] ossConfigIds) { |
| | | return toAjax(iSysOssConfigService.deleteWithValidByIds(Arrays.asList(ossConfigIds), true) ? 1 : 0); |
| | | } |
| | | |
| | |
| | | package com.ruoyi.oss.constant; |
| | | |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | |
| | | import java.util.Arrays; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * äºåå¨å¸¸é |
| | | * |
| | |
| | | public class CloudConstant { |
| | | |
| | | /** |
| | | * OSS模åKEY |
| | | */ |
| | | public static final String SYS_OSS_KEY = "sys_oss:"; |
| | | |
| | | /** |
| | | * äºåå¨é
ç½®KEY |
| | | */ |
| | | public final static String CLOUD_STORAGE_CONFIG_KEY = "sys.oss.cloudStorageService"; |
| | | public static final String CLOUD_STORAGE_CONFIG_KEY = "CloudStorageConfig"; |
| | | |
| | | /** |
| | | * ç¼åé
ç½®KEY |
| | | */ |
| | | public static final String CACHE_CONFIG_KEY = SYS_OSS_KEY + CLOUD_STORAGE_CONFIG_KEY; |
| | | |
| | | /** |
| | | * é¢è§åè¡¨èµæºå¼å
³Key |
| | | */ |
| | | public final static String PEREVIEW_LIST_RESOURCE_KEY = "sys.oss.previewListResource"; |
| | | public static final String PEREVIEW_LIST_RESOURCE_KEY = "sys.oss.previewListResource"; |
| | | |
| | | /** |
| | | * ç³»ç»æ°æ®ids |
| | | */ |
| | | public static final List<Integer> SYSTEM_DATA_IDS = Arrays.asList(1, 2, 3, 4); |
| | | |
| | | public static String getPropertiesName(String key) { |
| | | return key + CloudStorageProperties.class.getSimpleName(); |
| | | } |
| | | |
| | | } |
| | |
| | | package com.ruoyi.oss.enumd; |
| | | |
| | | import com.ruoyi.oss.service.impl.AliyunCloudStorageServiceImpl; |
| | | import com.ruoyi.oss.service.impl.MinioCloudStorageServiceImpl; |
| | | import com.ruoyi.oss.service.impl.QcloudCloudStorageServiceImpl; |
| | | import com.ruoyi.oss.service.impl.QiniuCloudStorageServiceImpl; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.service.impl.AliyunCloudStorageStrategy; |
| | | import com.ruoyi.oss.service.impl.MinioCloudStorageStrategy; |
| | | import com.ruoyi.oss.service.impl.QcloudCloudStorageStrategy; |
| | | import com.ruoyi.oss.service.impl.QiniuCloudStorageStrategy; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | |
| | | /** |
| | | * ä¸çäº |
| | | */ |
| | | QINIU("qiniu", QiniuCloudStorageServiceImpl.class), |
| | | QINIU("qiniu", QiniuCloudStorageStrategy.class), |
| | | |
| | | /** |
| | | * é¿éäº |
| | | */ |
| | | ALIYUN("aliyun", AliyunCloudStorageServiceImpl.class), |
| | | ALIYUN("aliyun", AliyunCloudStorageStrategy.class), |
| | | |
| | | /** |
| | | * è
¾è®¯äº |
| | | */ |
| | | QCLOUD("qcloud", QcloudCloudStorageServiceImpl.class), |
| | | QCLOUD("qcloud", QcloudCloudStorageStrategy.class), |
| | | |
| | | /** |
| | | * minio |
| | | */ |
| | | MINIO("minio", MinioCloudStorageServiceImpl.class); |
| | | MINIO("minio", MinioCloudStorageStrategy.class); |
| | | |
| | | private final String value; |
| | | |
| | |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | public static String getServiceName(String value) { |
| | | for (CloudServiceEnumd clazz : values()) { |
| | | if (clazz.getValue().equals(value)) { |
| | | return StringUtils.uncapitalize(clazz.getServiceClass().getSimpleName()); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | |
| | | |
| | | } |
| | |
| | | package com.ruoyi.oss.factory; |
| | | |
| | | import cn.hutool.core.convert.Convert; |
| | | import cn.hutool.core.lang.Assert; |
| | | import com.ruoyi.common.constant.Constants; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.utils.JsonUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.common.utils.reflect.ReflectUtils; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.oss.constant.CloudConstant; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.service.ICloudStorageStrategy; |
| | | |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | |
| | | OssFactory.redisCache = SpringUtils.getBean(RedisCache.class); |
| | | } |
| | | |
| | | private static final Map<String, ICloudStorageService> SERVICES = new ConcurrentHashMap<>(); |
| | | private static final Map<String, ICloudStorageStrategy> SERVICES = new ConcurrentHashMap<>(); |
| | | |
| | | public static ICloudStorageService instance() { |
| | | String type = Convert.toStr(redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + CloudConstant.CLOUD_STORAGE_CONFIG_KEY)); |
| | | public static ICloudStorageStrategy instance() { |
| | | String type = Convert.toStr(redisCache.getCacheObject(CloudConstant.CACHE_CONFIG_KEY)); |
| | | if (StringUtils.isEmpty(type)) { |
| | | throw new OssException("æä»¶å卿å¡ç±»åæ æ³æ¾å°!"); |
| | | } |
| | | return instance(type); |
| | | } |
| | | |
| | | public static ICloudStorageService instance(String type) { |
| | | ICloudStorageService service = SERVICES.get(type); |
| | | public static ICloudStorageStrategy instance(String type) { |
| | | ICloudStorageStrategy service = SERVICES.get(type); |
| | | if (service == null) { |
| | | service = (ICloudStorageService) SpringUtils.getBean(CloudServiceEnumd.getServiceClass(type)); |
| | | Object json = redisCache.getCacheObject(CloudConstant.SYS_OSS_KEY + type); |
| | | CloudStorageProperties properties = JsonUtils.parseObject(json.toString(), CloudStorageProperties.class); |
| | | String beanName = CloudServiceEnumd.getServiceName(type); |
| | | ICloudStorageStrategy bean = (ICloudStorageStrategy) ReflectUtils.newInstance(CloudServiceEnumd.getServiceClass(type), properties); |
| | | SpringUtils.registerBean(beanName, bean); |
| | | service = SpringUtils.getBean(beanName); |
| | | SERVICES.put(type, bean); |
| | | } |
| | | return service; |
| | | } |
| | | |
| | | public static void register(String type, ICloudStorageService iCloudStorageService) { |
| | | Assert.notNull(type, "type can't be null"); |
| | | SERVICES.put(type, iCloudStorageService); |
| | | public static void destroy(String type) { |
| | | ICloudStorageStrategy service = SERVICES.get(type); |
| | | if (service == null) { |
| | | return; |
| | | } |
| | | SpringUtils.unregisterBean(CloudServiceEnumd.getServiceName(type)); |
| | | SERVICES.remove(type); |
| | | } |
| | | |
| | | } |
| | |
| | | package com.ruoyi.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | import org.springframework.boot.context.properties.ConfigurationProperties; |
| | | import org.springframework.stereotype.Component; |
| | | |
| | | /** |
| | | * OSSäºåå¨ é
ç½®å±æ§ |
| | |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @Component |
| | | @ConfigurationProperties(prefix = "cloud-storage") |
| | | public class CloudStorageProperties { |
| | | |
| | | private Boolean previewListImage; |
| | | |
| | | private QiniuProperties qiniu; |
| | | |
| | | private AliyunProperties aliyun; |
| | | |
| | | private QcloudProperties qcloud; |
| | | |
| | | private MinioProperties minio; |
| | | /** |
| | | * åå |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * é¿éäº é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | * åç¼ |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public static class AliyunProperties { |
| | | |
| | | /** |
| | | * é¿éäºç»å®çåå |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * é¿éäºè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * é¿éäºAccessKeyId |
| | | */ |
| | | private String accessKeyId; |
| | | |
| | | /** |
| | | * é¿éäºAccessKeySecret |
| | | */ |
| | | private String accessKeySecret; |
| | | |
| | | /** |
| | | * é¿éäºBucketName |
| | | */ |
| | | private String bucketName; |
| | | |
| | | } |
| | | private String prefix; |
| | | |
| | | /** |
| | | * Minio é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | * ACCESS_KEY |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public static class MinioProperties { |
| | | |
| | | /** |
| | | * minioåå |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * minio ACCESS_KEY |
| | | */ |
| | | private String accessKey; |
| | | |
| | | /** |
| | | * minio SECRET_KEY |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * minio åå¨ç©ºé´å |
| | | */ |
| | | private String bucketName; |
| | | |
| | | } |
| | | private String accessKey; |
| | | |
| | | /** |
| | | * è
¾è®¯äºCOS é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | * SECRET_KEY |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public static class QcloudProperties { |
| | | |
| | | /** |
| | | * è
¾è®¯äºç»å®çåå |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * è
¾è®¯äºè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * è
¾è®¯äºSecretId |
| | | */ |
| | | private String secretId; |
| | | |
| | | /** |
| | | * è
¾è®¯äºSecretKey |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * è
¾è®¯äºBucketName |
| | | */ |
| | | private String bucketName; |
| | | |
| | | /** |
| | | * ä¸çæ¯å¦ä½¿ç¨https |
| | | */ |
| | | private Boolean isHttps; |
| | | |
| | | /** |
| | | * è
¾è®¯äºCOSæå±å°åº |
| | | */ |
| | | private String region; |
| | | |
| | | } |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * ä¸çäº é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | * åå¨ç©ºé´å |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public static class QiniuProperties { |
| | | private String bucketName; |
| | | |
| | | /** |
| | | * ä¸çç»å®çåå |
| | | */ |
| | | private String domain; |
| | | /** |
| | | * åå¨åºå |
| | | */ |
| | | private String region; |
| | | |
| | | /** |
| | | * ä¸çè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * ä¸çACCESS_KEY |
| | | */ |
| | | private String accessKey; |
| | | |
| | | /** |
| | | * ä¸çSECRET_KEY |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * ä¸çåå¨ç©ºé´å |
| | | */ |
| | | private String bucketName; |
| | | |
| | | /** |
| | | * ä¸çåå¨åºå |
| | | */ |
| | | private String region; |
| | | |
| | | /** |
| | | * ä¸çæ¯å¦ä½¿ç¨https |
| | | */ |
| | | private Boolean isHttps; |
| | | |
| | | } |
| | | /** |
| | | * æ¯å¦httpsï¼Y=æ¯,N=å¦ï¼ |
| | | */ |
| | | private String isHttps; |
| | | |
| | | } |
ÎļþÃû´Ó ruoyi-oss/src/main/java/com/ruoyi/oss/service/ICloudStorageService.java ÐÞ¸Ä |
| | |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * äºå卿塿¥å£ |
| | | * äºåå¨çç¥ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface ICloudStorageService { |
| | | public interface ICloudStorageStrategy { |
| | | |
| | | void createBucket(); |
| | | |
ÎļþÃû´Ó ruoyi-oss/src/main/java/com/ruoyi/oss/service/abstractd/AbstractCloudStorageService.java ÐÞ¸Ä |
| | |
| | | import cn.hutool.core.util.IdUtil; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.service.ICloudStorageStrategy; |
| | | |
| | | import java.io.InputStream; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * äºåå¨(æ¯æä¸çãé¿éäºãè
¾è®¯äºãminio) |
| | | * äºåå¨çç¥(æ¯æä¸çãé¿éäºãè
¾è®¯äºãminio) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public abstract class AbstractCloudStorageService implements ICloudStorageService, InitializingBean { |
| | | public abstract class AbstractCloudStorageStrategy implements ICloudStorageStrategy { |
| | | |
| | | protected CloudStorageProperties properties; |
| | | |
| | | @Override |
| | | public abstract void createBucket(); |
| | |
| | | |
| | | @Override |
| | | public abstract UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType); |
| | | |
| | | @Override |
| | | public abstract void afterPropertiesSet() throws Exception; |
| | | |
| | | @Override |
| | | public abstract String getEndpointLink(); |
ÎļþÃû´Ó ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/AliyunCloudStorageServiceImpl.java ÐÞ¸Ä |
| | |
| | | package com.ruoyi.oss.service.impl; |
| | | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.aliyun.oss.ClientConfiguration; |
| | | import com.aliyun.oss.OSSClient; |
| | | import com.aliyun.oss.common.auth.DefaultCredentialProvider; |
| | |
| | | import com.aliyun.oss.model.CreateBucketRequest; |
| | | import com.aliyun.oss.model.ObjectMetadata; |
| | | import com.aliyun.oss.model.PutObjectRequest; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties.AliyunProperties; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * é¿éäºåå¨ |
| | | * é¿éäºåå¨çç¥ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Lazy |
| | | @Service |
| | | public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | public class AliyunCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final OSSClient client; |
| | | private final AliyunProperties properties; |
| | | |
| | | @Autowired |
| | | public AliyunCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getAliyun(); |
| | | public AliyunCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | ClientConfiguration configuration = new ClientConfiguration(); |
| | | DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider( |
| | | this.properties.getAccessKeyId(), |
| | | this.properties.getAccessKeySecret()); |
| | | client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration); |
| | | properties.getAccessKey(), properties.getSecretKey()); |
| | | client = new OSSClient(properties.getEndpoint(), credentialProvider, configuration); |
| | | createBucket(); |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("é¿éäºåå¨é
ç½®é误! è¯·æ£æ¥ç³»ç»é
ç½®!"); |
| | |
| | | @Override |
| | | public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { |
| | | return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(), this); |
| | | } |
| | | |
| | | @Override |
ÎļþÃû´Ó ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/MinioCloudStorageServiceImpl.java ÐÞ¸Ä |
| | |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.enumd.PolicyType; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties.MinioProperties; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy; |
| | | import io.minio.*; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.stereotype.Service; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * minioåå¨ |
| | | * minioåå¨çç¥ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Lazy |
| | | @Service |
| | | public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | public class MinioCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final MinioClient minioClient; |
| | | private final MinioProperties properties; |
| | | |
| | | @Autowired |
| | | public MinioCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getMinio(); |
| | | public MinioCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | minioClient = MinioClient.builder() |
| | | .endpoint(this.properties.getEndpoint()) |
| | | .credentials(this.properties.getAccessKey(), this.properties.getSecretKey()) |
| | | .endpoint(properties.getEndpoint()) |
| | | .credentials(properties.getAccessKey(), properties.getSecretKey()) |
| | | .build(); |
| | | createBucket(); |
| | | } catch (Exception e) { |
| | |
| | | @Override |
| | | public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { |
| | | return upload(inputStream, getPath("", suffix), contentType); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(), this); |
| | | } |
| | | |
| | | @Override |
ÎļþÃû´Ó ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QcloudCloudStorageServiceImpl.java ÐÞ¸Ä |
| | |
| | | package com.ruoyi.oss.service.impl; |
| | | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.qcloud.cos.COSClient; |
| | | import com.qcloud.cos.ClientConfig; |
| | | import com.qcloud.cos.auth.BasicCOSCredentials; |
| | |
| | | import com.qcloud.cos.http.HttpProtocol; |
| | | import com.qcloud.cos.model.*; |
| | | import com.qcloud.cos.region.Region; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties.QcloudProperties; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * è
¾è®¯äºåå¨ |
| | | * è
¾è®¯äºåå¨çç¥ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Lazy |
| | | @Service |
| | | public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | public class QcloudCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final COSClient client; |
| | | private final QcloudProperties properties; |
| | | |
| | | @Autowired |
| | | public QcloudCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getQcloud(); |
| | | public QcloudCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | COSCredentials credentials = new BasicCOSCredentials( |
| | | this.properties.getSecretId(), |
| | | this.properties.getSecretKey()); |
| | | properties.getAccessKey(), properties.getSecretKey()); |
| | | // åå§å客æ·ç«¯é
ç½® |
| | | ClientConfig clientConfig = new ClientConfig(); |
| | | // 设置bucketæå¨çåºåï¼ååï¼gz ååï¼tj åä¸ï¼sh |
| | | clientConfig.setRegion(new Region(this.properties.getRegion())); |
| | | if (this.properties.getIsHttps()) { |
| | | clientConfig.setRegion(new Region(properties.getRegion())); |
| | | if ("Y".equals(properties.getIsHttps())) { |
| | | clientConfig.setHttpProtocol(HttpProtocol.https); |
| | | } else { |
| | | clientConfig.setHttpProtocol(HttpProtocol.http); |
| | |
| | | @Override |
| | | public UploadResult uploadSuffix(InputStream inputStream, String suffix, String contentType) { |
| | | return upload(inputStream, getPath(properties.getPrefix(), suffix), contentType); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(),this); |
| | | } |
| | | |
| | | @Override |
ÎļþÃû´Ó ruoyi-oss/src/main/java/com/ruoyi/oss/service/impl/QiniuCloudStorageServiceImpl.java ÐÞ¸Ä |
| | |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties.QiniuProperties; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.annotation.Lazy; |
| | | import org.springframework.stereotype.Service; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageStrategy; |
| | | |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * ä¸çäºåå¨ |
| | | * ä¸çäºåå¨çç¥ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Lazy |
| | | @Service |
| | | public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | public class QiniuCloudStorageStrategy extends AbstractCloudStorageStrategy { |
| | | |
| | | private final UploadManager uploadManager; |
| | | private final BucketManager bucketManager; |
| | | private final String token; |
| | | private final QiniuProperties properties; |
| | | |
| | | @Autowired |
| | | public QiniuCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getQiniu(); |
| | | public QiniuCloudStorageStrategy(CloudStorageProperties cloudStorageProperties) { |
| | | properties = cloudStorageProperties; |
| | | try { |
| | | Configuration config = new Configuration(getRegion(this.properties.getRegion())); |
| | | Configuration config = new Configuration(getRegion(properties.getRegion())); |
| | | // https设置 |
| | | config.useHttpsDomains = false; |
| | | if (this.properties.getIsHttps() != null) { |
| | | config.useHttpsDomains = this.properties.getIsHttps(); |
| | | } |
| | | config.useHttpsDomains = "Y".equals(properties.getIsHttps()); |
| | | uploadManager = new UploadManager(config); |
| | | Auth auth = Auth.create( |
| | | this.properties.getAccessKey(), |
| | | this.properties.getSecretKey()); |
| | | String bucketName = this.properties.getBucketName(); |
| | | Auth auth = Auth.create(properties.getAccessKey(), properties.getSecretKey()); |
| | | String bucketName = properties.getBucketName(); |
| | | token = auth.uploadToken(bucketName); |
| | | bucketManager = new BucketManager(auth, config); |
| | | |
| | | if (!ArrayUtil.contains(bucketManager.buckets(), bucketName)) { |
| | | bucketManager.createBucket(bucketName, this.properties.getRegion()); |
| | | bucketManager.createBucket(bucketName, properties.getRegion()); |
| | | } |
| | | } catch (Exception e) { |
| | | throw new IllegalArgumentException("ä¸çäºåå¨é
ç½®é误! è¯·æ£æ¥ç³»ç»é
ç½®!"); |
| | |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(),this); |
| | | } |
| | | |
| | | @Override |
| | | public String getEndpointLink() { |
| | | return properties.getDomain(); |
| | | return properties.getEndpoint(); |
| | | } |
| | | |
| | | private Region getRegion(String region) { |
| | |
| | | * @param isValid æ¯å¦æ ¡éª,true-å é¤åæ ¡éª,false-䏿 ¡éª |
| | | * @return |
| | | */ |
| | | Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid); |
| | | Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid); |
| | | |
| | | /** |
| | | * å¯ç¨åç¨ç¶æ |
| | |
| | | |
| | | import cn.hutool.core.bean.BeanUtil; |
| | | import cn.hutool.core.collection.CollUtil; |
| | | import cn.hutool.core.util.ObjectUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | |
| | | import com.ruoyi.common.core.mybatisplus.core.ServicePlusImpl; |
| | | import com.ruoyi.common.core.page.PagePlus; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.core.redis.RedisCache; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.common.utils.JsonUtils; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.constant.CloudConstant; |
| | | import com.ruoyi.system.domain.SysConfig; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.system.domain.SysOssConfig; |
| | | import com.ruoyi.system.domain.bo.SysOssConfigBo; |
| | | import com.ruoyi.system.domain.vo.SysOssConfigVo; |
| | | import com.ruoyi.system.mapper.SysOssConfigMapper; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | import com.ruoyi.system.service.ISysOssConfigService; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.PostConstruct; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * äºåå¨é
ç½®Serviceä¸å¡å±å¤ç |
| | |
| | | @Service |
| | | public class SysOssConfigServiceImpl extends ServicePlusImpl<SysOssConfigMapper, SysOssConfig, SysOssConfigVo> implements ISysOssConfigService { |
| | | |
| | | private final ISysConfigService iSysConfigService; |
| | | private final RedisCache redisCache; |
| | | |
| | | /** |
| | | * 项ç®å¯å¨æ¶ï¼åå§ååæ°å°ç¼åï¼å è½½é
置类 |
| | | */ |
| | | @PostConstruct |
| | | public void init() { |
| | | List<SysOssConfig> list = list(); |
| | | for (SysOssConfig config : list) { |
| | | String configKey = config.getConfigKey(); |
| | | if ("0".equals(config.getStatus())) { |
| | | redisCache.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, configKey); |
| | | } |
| | | redisCache.setCacheObject(getCacheKey(configKey), JsonUtils.toJsonString(config)); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public SysOssConfigVo queryById(Integer ossConfigId){ |
| | |
| | | |
| | | @Override |
| | | public Boolean insertByBo(SysOssConfigBo bo) { |
| | | SysOssConfig add = BeanUtil.toBean(bo, SysOssConfig.class); |
| | | validEntityBeforeSave(add); |
| | | return save(add); |
| | | SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class); |
| | | validEntityBeforeSave(config); |
| | | boolean flag = save(config); |
| | | if (flag) { |
| | | redisCache.setCacheObject( |
| | | getCacheKey(config.getConfigKey()), |
| | | JsonUtils.toJsonString(config)); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | @Override |
| | | public Boolean updateByBo(SysOssConfigBo bo) { |
| | | SysOssConfig update = BeanUtil.toBean(bo, SysOssConfig.class); |
| | | validEntityBeforeSave(update); |
| | | return updateById(update); |
| | | SysOssConfig config = BeanUtil.toBean(bo, SysOssConfig.class); |
| | | validEntityBeforeSave(config); |
| | | boolean flag = updateById(config); |
| | | if (flag) { |
| | | OssFactory.destroy(config.getConfigKey()); |
| | | redisCache.setCacheObject( |
| | | getCacheKey(config.getConfigKey()), |
| | | JsonUtils.toJsonString(config)); |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | |
| | | } |
| | | |
| | | @Override |
| | | public Boolean deleteWithValidByIds(Collection<Integer> ids, Boolean isValid) { |
| | | public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) { |
| | | if(isValid) { |
| | | if (CollUtil.containsAny(ids, CollUtil.newArrayList(1, 2, 3, 4))) { |
| | | if (CollUtil.containsAny(ids, CloudConstant.SYSTEM_DATA_IDS)) { |
| | | throw new CustomException("ç³»ç»å
ç½®, ä¸å¯å é¤!"); |
| | | } |
| | | } |
| | | return removeByIds(ids); |
| | | boolean flag = removeByIds(ids); |
| | | if (flag) { |
| | | for (Long configId : ids) { |
| | | SysOssConfig config = getById(configId); |
| | | OssFactory.destroy(config.getConfigKey()); |
| | | redisCache.deleteObject(getCacheKey(config.getConfigKey())); |
| | | } |
| | | } |
| | | return flag; |
| | | } |
| | | |
| | | /** |
| | |
| | | @Override |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public int updateOssConfigStatus(SysOssConfigBo bo) { |
| | | SysConfig sysConfig = iSysConfigService.getOne(new LambdaQueryWrapper<SysConfig>() |
| | | .eq(SysConfig::getConfigKey, CloudConstant.CLOUD_STORAGE_CONFIG_KEY)); |
| | | if(ObjectUtil.isNotNull(sysConfig)){ |
| | | sysConfig.setConfigValue(bo.getConfigKey()); |
| | | iSysConfigService.updateConfig(sysConfig); |
| | | } else { |
| | | throw new CustomException("缺å°'äºåå¨é
ç½®KEY'åæ°!"); |
| | | } |
| | | SysOssConfig sysOssConfig = BeanUtil.toBean(bo, SysOssConfig.class); |
| | | baseMapper.update(null, new LambdaUpdateWrapper<SysOssConfig>() |
| | | int row = baseMapper.update(null, new LambdaUpdateWrapper<SysOssConfig>() |
| | | .set(SysOssConfig::getStatus, "1")); |
| | | return baseMapper.updateById(sysOssConfig); |
| | | row += baseMapper.updateById(sysOssConfig); |
| | | if (row > 0) { |
| | | redisCache.setCacheObject(CloudConstant.CACHE_CONFIG_KEY, sysOssConfig.getConfigKey()); |
| | | } |
| | | return row; |
| | | } |
| | | |
| | | /** |
| | | * 设置cache key |
| | | * |
| | | * @param configKey åæ°é® |
| | | * @return ç¼åé®key |
| | | */ |
| | | private String getCacheKey(String configKey) { |
| | | return CloudConstant.SYS_OSS_KEY + configKey; |
| | | } |
| | | } |
| | |
| | | import com.ruoyi.common.utils.StringUtils; |
| | | import com.ruoyi.oss.entity.UploadResult; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import com.ruoyi.oss.service.ICloudStorageStrategy; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.domain.bo.SysOssBo; |
| | | import com.ruoyi.system.domain.vo.SysOssVo; |
| | |
| | | public SysOss upload(MultipartFile file) { |
| | | String originalfileName = file.getOriginalFilename(); |
| | | String suffix = StringUtils.substring(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); |
| | | ICloudStorageService storage = OssFactory.instance(); |
| | | ICloudStorageStrategy storage = OssFactory.instance(); |
| | | UploadResult uploadResult; |
| | | try { |
| | | uploadResult = storage.uploadSuffix(file.getBytes(), suffix, file.getContentType()); |
| | |
| | | } |
| | | List<SysOss> list = listByIds(ids); |
| | | for (SysOss sysOss : list) { |
| | | ICloudStorageService storage = OssFactory.instance(sysOss.getService()); |
| | | ICloudStorageStrategy storage = OssFactory.instance(sysOss.getService()); |
| | | storage.delete(sysOss.getUrl()); |
| | | } |
| | | return removeByIds(ids); |
| | |
| | | <el-form-item label="å" prop="region"> |
| | | <el-input v-model="form.region" placeholder="请è¾å
¥å" /> |
| | | </el-form-item> |
| | | <el-form-item label="ç¶æ"> |
| | | <el-radio-group v-model="form.status"> |
| | | <el-radio |
| | | v-for="dict in statusOptions" |
| | | :key="dict.dictValue" |
| | | :label="dict.dictValue" |
| | | >{{dict.dictLabel}}</el-radio> |
| | | </el-radio-group> |
| | | </el-form-item> |
| | | <el-form-item label="夿³¨" prop="remark"> |
| | | <el-input v-model="form.remark" type="textarea" placeholder="请è¾å
¥å
容" /> |
| | | </el-form-item> |
| | |
| | | primary key (oss_config_id) |
| | | ) engine=innodb comment='äºåå¨é
置表'; |
| | | |
| | | insert into sys_config values(10, 'OSSäºå卿å¡å', 'sys.oss.cloudStorageService', 'minio', 'Y', 'admin', sysdate(), '', null, 'OSSäºå卿å¡å(qiniu:ä¸çäº, aliyun:é¿éäº, qcloud:è
¾è®¯äº, minio: Minio)'); |
| | | insert into sys_config values(11, 'OSSé¢è§åè¡¨èµæºå¼å
³', 'sys.oss.previewListResource', 'true', 'Y', 'admin', sysdate(), '', null, 'true:å¼å¯, false:å
³é'); |
| | | |
| | | insert into sys_menu values('118', 'æä»¶ç®¡ç', '1', '10', 'oss', 'system/oss/index', 1, 0, 'C', '0', '0', 'system:oss:list', 'upload', 'admin', sysdate(), '', null, 'æä»¶ç®¡çèå'); |