package com.fs.system.oss;

import com.alibaba.fastjson.JSON;
import com.fs.common.utils.spring.SpringUtils;
import com.fs.system.service.ISysConfigService;


/**
 * 文件上传Factory
 */
public final class OSSFactory {
    private static ISysConfigService sysConfigService;
    private static volatile CloudStorageService instance;

    static {
        OSSFactory.sysConfigService = SpringUtils.getBean(ISysConfigService.class);
    }

    public static CloudStorageService build() {
        if (instance == null) {
            synchronized (OSSFactory.class) {
                if (instance == null) {
                    String jsonconfig = sysConfigService.selectConfigByKey(CloudConstant.CLOUD_STORAGE_CONFIG_KEY);
                    CloudStorageConfig config = JSON.parseObject(jsonconfig, CloudStorageConfig.class);

                    if (config.getType() == CloudConstant.CloudService.QINIU.getValue()) {
                        instance = new QiniuCloudStorageService(config);
                    }
                    else if (config.getType() == CloudConstant.CloudService.ALIYUN.getValue()) {
                        instance = new AliyunCloudStorageService(config);
                    }
                    else if (config.getType() == CloudConstant.CloudService.QCLOUD.getValue()) {
                        instance = new QcloudCloudStorageService(config);
                    }
                }
            }
        }
        return instance;
    }
}
