package cn.reghao.tnb.file.app.config;

import cn.reghao.oss.sdk.OssConsoleClient;
import cn.reghao.oss.sdk.model.OssConsoleConfig;
import cn.reghao.tnb.file.app.model.po.StoreConfig;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2024-02-24 17:41:42
 */
@Component
public class OssConsoleClientFactory {
    private OssConsoleClient ossConsoleClient;
    private OssConsoleConfig ossConsoleConfig;

    public void init(StoreConfig storeConfig) {
        String endpoint = storeConfig.getEndpoint();
        String accessKeyId = storeConfig.getAccessKeyId();
        String accessKeySecret = storeConfig.getAccessKeySecret();
        this.ossConsoleConfig = new OssConsoleConfig(endpoint, accessKeyId, accessKeySecret);
    }

    public OssConsoleClient getOssConsoleClient() throws Exception {
        if (ossConsoleClient == null) {
            ossConsoleClient = get();
            if (ossConsoleClient == null) {
                throw new Exception("OssConsoleClient is null");
            }
        }

        return ossConsoleClient;
    }

    private OssConsoleClient get() {
        try {
            return new OssConsoleClient(ossConsoleConfig);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }
}
