| | |
| | | <groupId>com.ruoyi</groupId> |
| | | <artifactId>ruoyi-common</artifactId> |
| | | </dependency> |
| | | <dependency> |
| | | <groupId>com.ruoyi</groupId> |
| | | <artifactId>ruoyi-system</artifactId> |
| | | </dependency> |
| | | |
| | | <dependency> |
| | | <groupId>com.qiniu</groupId> |
| | |
| | | package com.ruoyi.oss.constant; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | |
| | | /** |
| | | * äºåå¨å¸¸é |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class CloudConstant { |
| | | |
| | | /** |
| | | * äºåå¨é
ç½®KEY |
| | | */ |
| | | public final static String CLOUD_STORAGE_CONFIG_KEY = "sys.oss.cloud-storage"; |
| | | public final static String CLOUD_STORAGE_CONFIG_KEY = "sys.oss.cloudStorageService"; |
| | | |
| | | /** |
| | | * äºæå¡å |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum CloudService { |
| | | |
| | | /** |
| | | * ä¸çäº |
| | | */ |
| | | QINIU(1), |
| | | |
| | | /** |
| | | * é¿éäº |
| | | */ |
| | | ALIYUN(2), |
| | | |
| | | /** |
| | | * è
¾è®¯äº |
| | | */ |
| | | QCLOUD(3), |
| | | |
| | | /** |
| | | * minio |
| | | */ |
| | | MINIO(4); |
| | | |
| | | private final int value; |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.enumd; |
| | | |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.Getter; |
| | | |
| | | /** |
| | | * äºå卿å¡åæä¸¾ |
| | | * |
| | | * @author LionLi |
| | | */ |
| | | @Getter |
| | | @AllArgsConstructor |
| | | public enum CloudServiceEnumd { |
| | | |
| | | /** |
| | | * ä¸çäº |
| | | */ |
| | | QINIU("qiniu"), |
| | | |
| | | /** |
| | | * é¿éäº |
| | | */ |
| | | ALIYUN("aliyun"), |
| | | |
| | | /** |
| | | * è
¾è®¯äº |
| | | */ |
| | | QCLOUD("qcloud"), |
| | | |
| | | /** |
| | | * minio |
| | | */ |
| | | MINIO("minio"); |
| | | |
| | | private final String value; |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.factory; |
| | | |
| | | import cn.hutool.core.lang.Assert; |
| | | import com.ruoyi.common.utils.spring.SpringUtils; |
| | | import com.ruoyi.oss.constant.CloudConstant; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import com.ruoyi.system.service.ISysConfigService; |
| | | |
| | | import java.util.Map; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ Factory |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class OssFactory { |
| | | |
| | | private static ISysConfigService sysConfigService; |
| | | |
| | | static { |
| | | OssFactory.sysConfigService = SpringUtils.getBean(ISysConfigService.class); |
| | | } |
| | | |
| | | private static final Map<String, ICloudStorageService> SERVICES = new ConcurrentHashMap<>(); |
| | | |
| | | public static ICloudStorageService instance() { |
| | | String type = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY); |
| | | return SERVICES.get(type); |
| | | } |
| | | |
| | | public static ICloudStorageService instance(String type) { |
| | | return SERVICES.get(type); |
| | | } |
| | | |
| | | public static void register(String type, ICloudStorageService iCloudStorageService) { |
| | | Assert.notNull(type, "type can't be null"); |
| | | SERVICES.put(type, iCloudStorageService); |
| | | } |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | /** |
| | | * é¿éäº é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class AliyunProperties { |
| | | |
| | | /** |
| | | * é¿éäºç»å®çåå |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * é¿éäºè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * é¿éäºAccessKeyId |
| | | */ |
| | | private String accessKeyId; |
| | | |
| | | /** |
| | | * é¿éäºAccessKeySecret |
| | | */ |
| | | private String accessKeySecret; |
| | | |
| | | /** |
| | | * é¿éäºBucketName |
| | | */ |
| | | private String bucketName; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | 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 QiniuProperties qiniu; |
| | | |
| | | private AliyunProperties aliyun; |
| | | |
| | | private QcloudProperties qcloud; |
| | | |
| | | private MinioProperties minio; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | /** |
| | | * Minio é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class MinioProperties { |
| | | |
| | | /** |
| | | * ä¸çç»å®çåå |
| | | */ |
| | | private String endpoint; |
| | | |
| | | /** |
| | | * ä¸çè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * ä¸çACCESS_KEY |
| | | */ |
| | | private String accessKey; |
| | | |
| | | /** |
| | | * ä¸çSECRET_KEY |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * ä¸çåå¨ç©ºé´å |
| | | */ |
| | | private String bucketName; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | /** |
| | | * è
¾è®¯äºCOS é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class QcloudProperties { |
| | | |
| | | /** |
| | | * è
¾è®¯äºç»å®çåå |
| | | */ |
| | | private String domain; |
| | | |
| | | /** |
| | | * è
¾è®¯äºè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * è
¾è®¯äºSecretId |
| | | */ |
| | | private String secretId; |
| | | |
| | | /** |
| | | * è
¾è®¯äºSecretKey |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * è
¾è®¯äºBucketName |
| | | */ |
| | | private String bucketName; |
| | | |
| | | /** |
| | | * è
¾è®¯äºCOSæå±å°åº |
| | | */ |
| | | private String region; |
| | | |
| | | } |
¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.ruoyi.oss.properties; |
| | | |
| | | import lombok.Data; |
| | | import lombok.NoArgsConstructor; |
| | | |
| | | /** |
| | | * ä¸çäº é
ç½®å±æ§ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @NoArgsConstructor |
| | | public class QiniuProperties { |
| | | |
| | | /** |
| | | * ä¸çç»å®çåå |
| | | */ |
| | | private String domain; |
| | | |
| | | /** |
| | | * ä¸çè·¯å¾åç¼ |
| | | */ |
| | | private String prefix; |
| | | |
| | | /** |
| | | * ä¸çACCESS_KEY |
| | | */ |
| | | private String accessKey; |
| | | |
| | | /** |
| | | * ä¸çSECRET_KEY |
| | | */ |
| | | private String secretKey; |
| | | |
| | | /** |
| | | * ä¸çåå¨ç©ºé´å |
| | | */ |
| | | private String bucketName; |
| | | |
| | | } |
| | |
| | | |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * äºå卿塿¥å£ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public interface ICloudStorageService { |
| | | |
| | | /** |
| | | * è·åæå¡åç±»å |
| | | */ |
| | | String getServiceType(); |
| | | |
| | | /** |
| | | * æä»¶è·¯å¾ |
| | | * |
| | | * @param prefix åç¼ |
| | |
| | | package com.ruoyi.oss.service.abstractd; |
| | | |
| | | import cn.hutool.core.date.DateUtil; |
| | | import cn.hutool.core.io.IoUtil; |
| | | import cn.hutool.core.util.IdUtil; |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.ruoyi.oss.config.CloudStorageConfig; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import com.ruoyi.oss.utils.DateUtils; |
| | | import org.springframework.beans.factory.InitializingBean; |
| | | |
| | | import java.util.UUID; |
| | | import java.io.InputStream; |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * äºåå¨(æ¯æä¸çãé¿éäºãè
¾è®¯äºãminio) |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public abstract class AbstractCloudStorageService implements ICloudStorageService { |
| | | public abstract class AbstractCloudStorageService implements ICloudStorageService, InitializingBean { |
| | | |
| | | /** |
| | | * äºåå¨é
ç½®ä¿¡æ¯ |
| | | */ |
| | | protected CloudStorageConfig config; |
| | | |
| | | public int getServiceType() { |
| | | return config.getType(); |
| | | } |
| | | @Override |
| | | public abstract String getServiceType(); |
| | | |
| | | @Override |
| | | public String getPath(String prefix, String suffix) { |
| | | // çæuuid |
| | | String uuid = UUID.randomUUID().toString().replaceAll("-", ""); |
| | | String uuid = IdUtil.fastSimpleUUID(); |
| | | // æä»¶è·¯å¾ |
| | | String path = DateUtils.dateTime() + "/" + uuid; |
| | | String path = DateUtil.format(new Date(), "yyyyMMdd") + "/" + uuid; |
| | | if (StrUtil.isNotBlank(prefix)) { |
| | | path = prefix + "/" + path; |
| | | } |
| | | return path + suffix; |
| | | } |
| | | |
| | | @Override |
| | | public abstract String upload(byte[] data, String path); |
| | | |
| | | @Override |
| | | public abstract void delete(String path); |
| | | |
| | | @Override |
| | | public String upload(InputStream inputStream, String path) { |
| | | byte[] data = IoUtil.readBytes(inputStream); |
| | | return this.upload(data, path); |
| | | } |
| | | |
| | | @Override |
| | | public abstract String uploadSuffix(byte[] data, String suffix); |
| | | |
| | | @Override |
| | | public abstract String uploadSuffix(InputStream inputStream, String suffix); |
| | | |
| | | @Override |
| | | public abstract void afterPropertiesSet() throws Exception; |
| | | } |
| | |
| | | package com.ruoyi.oss.service.impl; |
| | | |
| | | import com.aliyun.oss.ClientConfiguration; |
| | | import com.aliyun.oss.OSSClient; |
| | | import com.ruoyi.oss.config.CloudStorageConfig; |
| | | import com.aliyun.oss.common.auth.DefaultCredentialProvider; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.exception.OssException; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.properties.AliyunProperties; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | 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 java.io.ByteArrayInputStream; |
| | | import java.io.InputStream; |
| | | |
| | | /** |
| | | * é¿éäºåå¨ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService { |
| | | @Lazy |
| | | @Service |
| | | public class AliyunCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | |
| | | private OSSClient client; |
| | | private final OSSClient client; |
| | | private final AliyunProperties properties; |
| | | |
| | | public AliyunCloudStorageServiceImpl(CloudStorageConfig config) { |
| | | this.config = config; |
| | | // åå§å |
| | | init(); |
| | | @Autowired |
| | | public AliyunCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getAliyun(); |
| | | ClientConfiguration configuration = new ClientConfiguration(); |
| | | DefaultCredentialProvider credentialProvider = new DefaultCredentialProvider( |
| | | this.properties.getAccessKeyId(), |
| | | this.properties.getAccessKeySecret()); |
| | | client = new OSSClient(this.properties.getEndpoint(), credentialProvider, configuration); |
| | | } |
| | | |
| | | private void init() { |
| | | client = new OSSClient(config.getDomain(), config.getAccessKey(), config.getSecretKey()); |
| | | @Override |
| | | public String getServiceType() { |
| | | return CloudServiceEnumd.ALIYUN.getValue(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | @Override |
| | | public String upload(InputStream inputStream, String path) { |
| | | try { |
| | | client.putObject(config.getBucketName(), path, inputStream); |
| | | client.putObject(this.properties.getBucketName(), path, inputStream); |
| | | } catch (Exception e) { |
| | | throw new OssException("ä¸ä¼ æä»¶å¤±è´¥ï¼è¯·æ£æ¥é
置信æ¯"); |
| | | } |
| | | return config.getDomain() + "/" + path; |
| | | return this.properties.getEndpoint() + "/" + path; |
| | | } |
| | | |
| | | @Override |
| | | public void delete(String path) { |
| | | path = path.replace(config.getDomain() + "/", ""); |
| | | path = path.replace(this.properties.getEndpoint() + "/", ""); |
| | | try { |
| | | client.deleteObject(config.getBucketName(), path); |
| | | client.deleteObject(this.properties.getBucketName(), path); |
| | | } catch (Exception e) { |
| | | throw new OssException("ä¸ä¼ æä»¶å¤±è´¥ï¼è¯·æ£æ¥é
置信æ¯"); |
| | | } |
| | |
| | | |
| | | @Override |
| | | public String uploadSuffix(byte[] data, String suffix) { |
| | | return upload(data, getPath(config.getPrefix(), suffix)); |
| | | return upload(data, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(InputStream inputStream, String suffix) { |
| | | return upload(inputStream, getPath(config.getPrefix(), suffix)); |
| | | return upload(inputStream, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(),this); |
| | | } |
| | | } |
| | |
| | | package com.ruoyi.oss.service.impl; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import com.ruoyi.oss.config.CloudStorageConfig; |
| | | 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.MinioProperties; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService; |
| | | import io.minio.MinioClient; |
| | | 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 java.io.InputStream; |
| | | |
| | | /** |
| | | * minioåå¨ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService { |
| | | @Lazy |
| | | @Service |
| | | public class MinioCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | |
| | | private MinioClient minioClient; |
| | | private final MinioClient minioClient; |
| | | private final MinioProperties properties; |
| | | |
| | | public MinioCloudStorageServiceImpl(CloudStorageConfig config) { |
| | | this.config = config; |
| | | // åå§å |
| | | init(); |
| | | @Autowired |
| | | public MinioCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getMinio(); |
| | | minioClient = MinioClient.builder() |
| | | .endpoint(this.properties.getEndpoint()) |
| | | .credentials(this.properties.getAccessKey(), this.properties.getSecretKey()) |
| | | .build(); |
| | | } |
| | | |
| | | private void init() { |
| | | minioClient = MinioClient.builder() |
| | | .endpoint(config.getDomain()) |
| | | .credentials(config.getAccessKey(), config.getSecretKey()) |
| | | .build(); |
| | | @Override |
| | | public String getServiceType() { |
| | | return CloudServiceEnumd.MINIO.getValue(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } catch (Exception e) { |
| | | throw new OssException("ä¸ä¼ æä»¶å¤±è´¥ï¼è¯·æ ¸å¯¹Minioé
置信æ¯"); |
| | | } |
| | | return config.getDomain() + "/" + path; |
| | | return this.properties.getEndpoint() + "/" + path; |
| | | } |
| | | |
| | | @Override |
| | |
| | | } |
| | | |
| | | @Override |
| | | public String upload(InputStream inputStream, String path) { |
| | | byte[] data = IoUtil.readBytes(inputStream); |
| | | return this.upload(data, path); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(byte[] data, String suffix) { |
| | | return upload(data, getPath(config.getPrefix(), suffix)); |
| | | return upload(data, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(InputStream inputStream, String suffix) { |
| | | return upload(inputStream, getPath(config.getPrefix(), suffix)); |
| | | return upload(inputStream, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(),this); |
| | | } |
| | | } |
| | |
| | | package com.ruoyi.oss.service.impl; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import com.qcloud.cos.COSClient; |
| | | import com.ruoyi.oss.config.CloudStorageConfig; |
| | | import com.qcloud.cos.ClientConfig; |
| | | import com.qcloud.cos.auth.BasicCOSCredentials; |
| | | import com.qcloud.cos.auth.COSCredentials; |
| | | import com.qcloud.cos.region.Region; |
| | | import com.ruoyi.oss.enumd.CloudServiceEnumd; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.properties.CloudStorageProperties; |
| | | import com.ruoyi.oss.properties.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 java.io.InputStream; |
| | | |
| | | /** |
| | | * è
¾è®¯äºåå¨ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService { |
| | | @Lazy |
| | | @Service |
| | | public class QcloudCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | |
| | | private COSClient client; |
| | | private final COSClient client; |
| | | private final QcloudProperties properties; |
| | | |
| | | public QcloudCloudStorageServiceImpl(CloudStorageConfig config) { |
| | | this.config = config; |
| | | // åå§å |
| | | init(); |
| | | @Autowired |
| | | public QcloudCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getQcloud(); |
| | | COSCredentials credentials = new BasicCOSCredentials( |
| | | this.properties.getSecretId(), |
| | | this.properties.getSecretKey()); |
| | | // åå§å客æ·ç«¯é
ç½® |
| | | ClientConfig clientConfig = new ClientConfig(); |
| | | // 设置bucketæå¨çåºåï¼ååï¼gz ååï¼tj åä¸ï¼sh |
| | | clientConfig.setRegion(new Region(this.properties.getRegion())); |
| | | client = new COSClient(credentials, clientConfig); |
| | | } |
| | | |
| | | private void init() { |
| | | // Credentials credentials = new Credentials(config.getQcloudAppId(), config.getQcloudSecretId(), |
| | | // config.getQcloudSecretKey()); |
| | | // åå§å客æ·ç«¯é
ç½® |
| | | // ClientConfig clientConfig = new ClientConfig(); |
| | | // // 设置bucketæå¨çåºåï¼ååï¼gz ååï¼tj åä¸ï¼sh |
| | | // clientConfig.setRegion(config.getQcloudRegion()); |
| | | // client = new COSClient(clientConfig, credentials); |
| | | @Override |
| | | public String getServiceType() { |
| | | return CloudServiceEnumd.QCLOUD.getValue(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | // if (Convert.toInt(jsonObject.get("code")) != 0) { |
| | | // throw new OssException("æä»¶ä¸ä¼ 失败ï¼" + Convert.toStr(jsonObject.get("message"))); |
| | | // } |
| | | return config.getDomain() + path; |
| | | return this.properties.getDomain() + path; |
| | | } |
| | | |
| | | @Override |
| | |
| | | // } |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public String upload(InputStream inputStream, String path) { |
| | | byte[] data = IoUtil.readBytes(inputStream); |
| | | return this.upload(data, path); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(byte[] data, String suffix) { |
| | | return upload(data, getPath(config.getPrefix(), suffix)); |
| | | return upload(data, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(InputStream inputStream, String suffix) { |
| | | return upload(inputStream, getPath(config.getPrefix(), suffix)); |
| | | return upload(inputStream, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(),this); |
| | | } |
| | | } |
| | |
| | | package com.ruoyi.oss.service.impl; |
| | | |
| | | import cn.hutool.core.io.IoUtil; |
| | | import com.qiniu.http.Response; |
| | | import com.qiniu.storage.BucketManager; |
| | | import com.qiniu.storage.Configuration; |
| | | import com.qiniu.storage.Region; |
| | | import com.qiniu.storage.UploadManager; |
| | | import com.qiniu.util.Auth; |
| | | import com.ruoyi.oss.config.CloudStorageConfig; |
| | | 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.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 java.io.InputStream; |
| | | |
| | | /** |
| | | * ä¸çäºåå¨ |
| | | * |
| | | * @author Lion Li |
| | | */ |
| | | public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService { |
| | | @Lazy |
| | | @Service |
| | | public class QiniuCloudStorageServiceImpl extends AbstractCloudStorageService implements InitializingBean { |
| | | |
| | | private UploadManager uploadManager; |
| | | private BucketManager bucketManager; |
| | | private String token; |
| | | private final UploadManager uploadManager; |
| | | private final BucketManager bucketManager; |
| | | private final String token; |
| | | private final QiniuProperties properties; |
| | | |
| | | public QiniuCloudStorageServiceImpl(CloudStorageConfig config) { |
| | | this.config = config; |
| | | // åå§å |
| | | init(); |
| | | } |
| | | |
| | | private void init() { |
| | | @Autowired |
| | | public QiniuCloudStorageServiceImpl(CloudStorageProperties properties) { |
| | | this.properties = properties.getQiniu(); |
| | | // z0 z1 z2 |
| | | Configuration config = new Configuration(Region.autoRegion()); |
| | | // é»è®¤ä¸ä½¿ç¨https |
| | | config.useHttpsDomains = false; |
| | | uploadManager = new UploadManager(config); |
| | | Auth auth = Auth.create(this.config.getAccessKey(), this.config.getSecretKey()); |
| | | token = auth.uploadToken(this.config.getBucketName()); |
| | | Auth auth = Auth.create( |
| | | this.properties.getAccessKey(), |
| | | this.properties.getSecretKey()); |
| | | token = auth.uploadToken(this.properties.getBucketName()); |
| | | bucketManager = new BucketManager(auth, config); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public String getServiceType() { |
| | | return CloudServiceEnumd.QINIU.getValue(); |
| | | } |
| | | |
| | | @Override |
| | |
| | | } catch (Exception e) { |
| | | throw new OssException("ä¸ä¼ æä»¶å¤±è´¥ï¼è¯·æ ¸å¯¹ä¸çé
置信æ¯"); |
| | | } |
| | | return config.getDomain() + "/" + path; |
| | | return this.properties.getDomain() + "/" + path; |
| | | } |
| | | |
| | | @Override |
| | | public void delete(String path) { |
| | | try { |
| | | path = path.replace(config.getDomain() + "/", ""); |
| | | Response res = bucketManager.delete(config.getBucketName(), path); |
| | | path = path.replace(this.properties.getDomain() + "/", ""); |
| | | Response res = bucketManager.delete(this.properties.getBucketName(), path); |
| | | if (!res.isOK()) { |
| | | throw new RuntimeException("å é¤ä¸çæä»¶åºéï¼" + res.toString()); |
| | | } |
| | |
| | | } |
| | | |
| | | @Override |
| | | public String upload(InputStream inputStream, String path) { |
| | | byte[] data = IoUtil.readBytes(inputStream); |
| | | return this.upload(data, path); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(byte[] data, String suffix) { |
| | | return upload(data, getPath(config.getPrefix(), suffix)); |
| | | return upload(data, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public String uploadSuffix(InputStream inputStream, String suffix) { |
| | | return upload(inputStream, getPath(config.getPrefix(), suffix)); |
| | | return upload(inputStream, getPath(this.properties.getPrefix(), suffix)); |
| | | } |
| | | |
| | | @Override |
| | | public void afterPropertiesSet() throws Exception { |
| | | OssFactory.register(getServiceType(),this); |
| | | } |
| | | |
| | | } |
| | |
| | | package com.ruoyi.system.controller; |
| | | |
| | | |
| | | import com.aliyun.oss.ServiceException; |
| | | import com.ruoyi.common.annotation.Log; |
| | | import com.ruoyi.common.annotation.RepeatSubmit; |
| | | import com.ruoyi.common.core.controller.BaseController; |
| | | import com.ruoyi.common.core.domain.AjaxResult; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.common.utils.poi.ExcelUtil; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.service.ISysOssService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Controller; |
| | | import org.springframework.ui.ModelMap; |
| | | import org.springframework.security.access.prepost.PreAuthorize; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.List; |
| | | import javax.validation.constraints.NotEmpty; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ æ§å¶å± |
| | | * |
| | | * @author chkj |
| | | * @date 2019-07-15 |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @RequiredArgsConstructor(onConstructor_ = @Autowired) |
| | | @RestController |
| | | @RequestMapping("/system/oss") |
| | | public class SysOssController extends BaseController { |
| | | |
| | | @Autowired |
| | | private ISysOssService iSysOssService; |
| | | private final ISysOssService iSysOssService; |
| | | |
| | | /** |
| | | * æ¥è¯¢æä»¶ä¸ä¼ å表 |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('system:oss:list')") |
| | | @GetMapping("/list") |
| | | public TableDataInfo list(SysOss sysOss) { |
| | | List<SysOss> list = iSysOssService.list(sysOss); |
| | | return PageUtils.buildDataInfo(list); |
| | | public TableDataInfo<SysOss> list(SysOss sysOss) { |
| | | return iSysOssService.queryPageList(sysOss); |
| | | } |
| | | |
| | | /** |
| | | * ä¸ä¼ å¾ç |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('system:oss:upload')") |
| | | @Log(title = "OSSäºåå¨", businessType = BusinessType.INSERT) |
| | | @RepeatSubmit |
| | | @PostMapping("/upload") |
| | | public AjaxResult upload(@RequestParam("file") MultipartFile file) { |
| | | public AjaxResult<Map<String, String>> upload(@RequestParam("file") MultipartFile file) { |
| | | if (file.isEmpty()) { |
| | | throw new CustomException("ä¸ä¼ æä»¶ä¸è½ä¸ºç©º"); |
| | | } |
| | | Map<String, String> json = iSysOssService.upload(file); |
| | | return AjaxResult.success(json); |
| | | SysOss oss = iSysOssService.upload(file); |
| | | Map<String, String> map = new HashMap<>(2); |
| | | map.put("url", oss.getUrl()); |
| | | map.put("fileName", oss.getFileName()); |
| | | return AjaxResult.success(map); |
| | | } |
| | | |
| | | /** |
| | | * å é¤OSSäºåå¨ |
| | | */ |
| | | @PreAuthorize("@ss.hasPermi('system:oss:remove')") |
| | | @Log(title = "OSSäºåå¨" , businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{ossIds}") |
| | | public AjaxResult<Void> remove(@NotEmpty(message = "主é®ä¸è½ä¸ºç©º") |
| | | @PathVariable Long[] ossIds) { |
| | | return toAjax(iSysOssService.deleteByIds(Arrays.asList(ossIds)) ? 1 : 0); |
| | | } |
| | | |
| | | } |
| | |
| | | import java.util.Date; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ 表 sys_oss |
| | | * OSSäºåå¨å¯¹è±¡ |
| | | * |
| | | * @author chkj |
| | | * @date 2019-07-15 |
| | | * @author Lion Li |
| | | */ |
| | | @Data |
| | | @Accessors(chain = true) |
| | | @NoArgsConstructor |
| | | @Accessors(chain = true) |
| | | @TableName("sys_oss") |
| | | public class SysOss implements Serializable { |
| | | |
| | | private static final long serialVersionUID = 1L; |
| | | |
| | | @TableId(value = "id", type = IdType.AUTO) |
| | | private Long id; |
| | | |
| | | /** |
| | | * äºåå¨ä¸»é® |
| | | */ |
| | | @TableId(value = "oss_id") |
| | | private Long ossId; |
| | | |
| | | /** |
| | | * æä»¶å |
| | |
| | | private String createBy; |
| | | |
| | | /** |
| | | * æ´æ°æ¶é´ |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT_UPDATE) |
| | | private Date updateTime; |
| | | |
| | | /** |
| | | * æ´æ°äºº |
| | | */ |
| | | @TableField(fill = FieldFill.INSERT_UPDATE) |
| | | private String updateBy; |
| | | |
| | | /** |
| | | * æå¡å |
| | | */ |
| | | private Integer service; |
| | | private String service; |
| | | |
| | | } |
| | |
| | | /** |
| | | * æä»¶ä¸ä¼ æ°æ®å± |
| | | * |
| | | * @author chkj |
| | | * @date 2019-07-15 |
| | | * @author Lion Li |
| | | */ |
| | | public interface SysOssMapper extends BaseMapper<SysOss> { |
| | | } |
| | |
| | | package com.ruoyi.system.service; |
| | | |
| | | import com.baomidou.mybatisplus.extension.service.IService; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Collection; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ æå¡å± |
| | | * |
| | | * @author chkj |
| | | * @date 2019-07-15 |
| | | * @author Lion Li |
| | | */ |
| | | public interface ISysOssService extends IService<SysOss> { |
| | | /** |
| | | * å表æ¥è¯¢ |
| | | */ |
| | | List<SysOss> list(SysOss sysOss); |
| | | |
| | | Map<String, String> upload(MultipartFile file); |
| | | TableDataInfo<SysOss> queryPageList(SysOss sysOss); |
| | | |
| | | SysOss upload(MultipartFile file); |
| | | |
| | | Boolean deleteByIds(Collection<Long> ids); |
| | | } |
| | |
| | | package com.ruoyi.system.service.impl; |
| | | |
| | | import cn.hutool.core.util.StrUtil; |
| | | import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| | | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| | | import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
| | | import com.ruoyi.common.core.page.TableDataInfo; |
| | | import com.ruoyi.common.exception.CustomException; |
| | | import com.ruoyi.oss.config.CloudStorageConfig; |
| | | import com.ruoyi.oss.service.abstractd.AbstractCloudStorageService; |
| | | import com.ruoyi.common.utils.PageUtils; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.oss.service.ICloudStorageService; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.factory.OSSFactory; |
| | | import com.ruoyi.system.mapper.SysOssMapper; |
| | | import com.ruoyi.system.service.ISysOssService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.IOException; |
| | | import java.util.HashMap; |
| | | import java.util.Collection; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | |
| | | /** |
| | | * æä»¶ä¸ä¼ æå¡å±å®ç° |
| | | * |
| | | * @author chkj |
| | | * @date 2019-07-15 |
| | | * @author Lion Li |
| | | */ |
| | | @Slf4j |
| | | @Service |
| | | public class SysOssServiceImpl extends ServiceImpl<SysOssMapper, SysOss> implements ISysOssService { |
| | | |
| | | @Autowired |
| | | private CloudStorageConfig config; |
| | | |
| | | @Override |
| | | public List<SysOss> list(SysOss sysOss) { |
| | | LambdaQueryWrapper<SysOss> wrapper = new LambdaQueryWrapper<>(); |
| | | return baseMapper.selectList(wrapper); |
| | | public TableDataInfo<SysOss> queryPageList(SysOss sysOss) { |
| | | LambdaQueryWrapper<SysOss> lqw = Wrappers.lambdaQuery(); |
| | | lqw.like(StrUtil.isNotBlank(sysOss.getFileName()), SysOss::getFileName, sysOss.getFileName()); |
| | | lqw.like(StrUtil.isNotBlank(sysOss.getFileSuffix()), SysOss::getFileSuffix, sysOss.getFileSuffix()); |
| | | lqw.like(StrUtil.isNotBlank(sysOss.getUrl()), SysOss::getUrl, sysOss.getUrl()); |
| | | lqw.like(StrUtil.isNotBlank(sysOss.getService()), SysOss::getService, sysOss.getService()); |
| | | return PageUtils.buildDataInfo(page(PageUtils.buildPage(), lqw)); |
| | | } |
| | | |
| | | @Override |
| | | public Map<String, String> upload(MultipartFile file) { |
| | | public SysOss upload(MultipartFile file) { |
| | | String originalfileName = file.getOriginalFilename(); |
| | | String suffix = originalfileName.substring(originalfileName.lastIndexOf(".")); |
| | | String suffix = StrUtil.sub(originalfileName, originalfileName.lastIndexOf("."), originalfileName.length()); |
| | | try { |
| | | AbstractCloudStorageService storage = OSSFactory.build(); |
| | | ICloudStorageService storage = OssFactory.instance(); |
| | | String url = storage.uploadSuffix(file.getBytes(), suffix); |
| | | // ä¿åæä»¶ä¿¡æ¯ |
| | | SysOss ossEntity = new SysOss() |
| | | SysOss oss = new SysOss() |
| | | .setUrl(url).setFileSuffix(suffix) |
| | | .setFileName(originalfileName) |
| | | .setService(storage.getServiceType()); |
| | | save(ossEntity); |
| | | Map<String, String> map = new HashMap<>(2); |
| | | map.put("url", ossEntity.getUrl()); |
| | | map.put("fileName", ossEntity.getFileName()); |
| | | return map; |
| | | save(oss); |
| | | return oss; |
| | | } catch (IOException e) { |
| | | throw new CustomException("æä»¶è¯»åå¼å¸¸!!!", e); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public Boolean deleteByIds(Collection<Long> ids) { |
| | | List<SysOss> list = listByIds(ids); |
| | | for (SysOss sysOss : list) { |
| | | ICloudStorageService storage = OssFactory.instance(sysOss.getService()); |
| | | storage.delete(sysOss.getUrl()); |
| | | } |
| | | return removeByIds(ids); |
| | | } |
| | | |
| | | } |
| | |
| | | <?xml version="1.0" encoding="UTF-8" ?> |
| | | <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> |
| | | <mapper namespace="com.chkj.cloudStorage.mapper.SysOssMapper"> |
| | | <mapper namespace="com.ruoyi.system.mapper.SysOssMapper"> |
| | | |
| | | <!-- éç¨æ¥è¯¢æ å°ç»æ --> |
| | | <resultMap id="BaseResultMap" type="com.chkj.cloudStorage.domain.SysOss"> |
| | | <id column="id" property="id"/> |
| | | <result column="file_name" property="fileName"/> |
| | | <result column="file_suffix" property="fileSuffix"/> |
| | | <result column="url" property="url"/> |
| | | <result column="create_time" property="createTime"/> |
| | | <result column="create_by" property="createBy"/> |
| | | <result column="service" property="service"/> |
| | | <resultMap type="com.ruoyi.system.domain.SysOss" id="SysOssResult"> |
| | | <result property="ossId" column="oss_id"/> |
| | | <result property="fileName" column="file_name"/> |
| | | <result property="fileSuffix" column="file_suffix"/> |
| | | <result property="url" column="url"/> |
| | | <result property="createTime" column="create_time"/> |
| | | <result property="createBy" column="create_by"/> |
| | | <result property="updateTime" column="update_time"/> |
| | | <result property="updateBy" column="update_by"/> |
| | | <result property="service" column="service"/> |
| | | </resultMap> |
| | | |
| | | <!-- éç¨æ¥è¯¢ç»æå --> |
| | | <sql id="Base_Column_List"> |
| | | id, file_name, file_suffix, url, create_time, create_by, service |
| | | </sql> |
| | | |
| | | </mapper> |
¶Ô±ÈÐÂÎļþ |
| | |
| | | -- ---------------------------- |
| | | -- OSSäºåå¨è¡¨ |
| | | -- ---------------------------- |
| | | drop table if exists sys_oss; |
| | | create table sys_oss ( |
| | | oss_id bigint(20) not null auto_increment comment 'äºåå¨ä¸»é®', |
| | | file_name varchar(64) not null default '' comment 'æä»¶å', |
| | | file_suffix varchar(10) not null default '' comment 'æä»¶åç¼å', |
| | | url varchar(200) not null comment 'URLå°å', |
| | | create_time datetime default null comment 'å建æ¶é´', |
| | | create_by varchar(64) not null default '' comment 'ä¸ä¼ 人', |
| | | update_time datetime default null comment 'æ´æ°æ¶é´', |
| | | update_by varchar(64) not null default '' comment 'æ´æ°äºº', |
| | | service varchar(10) not null default 'minio' comment 'æå¡å', |
| | | primary key (oss_id) |
| | | ) engine=innodb comment ='OSSäºåå¨è¡¨'; |
| | | |
| | | insert into sys_config values(10, 'OSSäºå卿å¡å', 'sys.oss.cloudStorageService', 'minio', 'Y', 'admin', sysdate(), '', null, 'OSSäºå卿å¡å(qiniu:ä¸çäº, aliyun:é¿éäº, qcloud:è
¾è®¯äº, minio: Minio)'); |
| | | |
| | | insert into sys_menu values('118', 'æä»¶ç®¡ç', '1', '10', 'oss', 'system/oss/index', 1, 0, 'C', '0', '0', 'system:oss:list', 'oss', 'admin', sysdate(), '', null, 'æä»¶ç®¡çèå'); |
| | | |
| | | insert into sys_menu values('1600', 'æä»¶æ¥è¯¢', '118', '1', '#', '', 1, 0, 'F', '0', '0', 'system:oss:query', '#', 'admin', sysdate(), '', null, ''); |
| | | insert into sys_menu values('1601', 'æä»¶ä¸ä¼ ', '118', '2', '#', '', 1, 0, 'F', '0', '0', 'system:oss:upload', '#', 'admin', sysdate(), '', null, ''); |
| | | insert into sys_menu values('1602', 'æä»¶å é¤', '118', '3', '#', '', 1, 0, 'F', '0', '0', 'system:oss:remove', '#', 'admin', sysdate(), '', null, ''); |