疯狂的狮子li
2021-08-04 b65856227f9b0862bbf65c1890b11fdf343d15dd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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.StringUtils;
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 java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
 
/**
 * 文件上传Factory
 *
 * @author Lion Li
 */
public class OssFactory {
 
    private static RedisCache redisCache;
 
    static {
        OssFactory.redisCache = SpringUtils.getBean(RedisCache.class);
    }
 
    private static final Map<String, ICloudStorageService> SERVICES = new ConcurrentHashMap<>();
 
    public static ICloudStorageService instance() {
        String type = Convert.toStr(redisCache.getCacheObject(Constants.SYS_CONFIG_KEY + CloudConstant.CLOUD_STORAGE_CONFIG_KEY));
        if (StringUtils.isEmpty(type)) {
            throw new OssException("文件存储服务类型无法找到!");
        }
        return instance(type);
    }
 
    public static ICloudStorageService instance(String type) {
        ICloudStorageService service = SERVICES.get(type);
        if (service == null) {
            service = (ICloudStorageService) SpringUtils.getBean(CloudServiceEnumd.getServiceClass(type));
        }
        return service;
    }
 
    public static void register(String type, ICloudStorageService iCloudStorageService) {
        Assert.notNull(type, "type can't be null");
        SERVICES.put(type, iCloudStorageService);
    }
}