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

import cn.reghao.oss.sdk.OssClient;
import cn.reghao.tnb.file.app.db.mapper.OssConfigMapper;
import cn.reghao.tnb.file.app.model.po.OssConfig;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2026-04-27 16:16:20
 */
@Component
public class OssClientManager {
    private final OssConfigMapper ossConfigMapper;
    private OssClient ossClient;

    public OssClientManager(OssConfigMapper ossConfigMapper) {
        this.ossConfigMapper = ossConfigMapper;
    }

    public OssClient getOssClient() {
        if (this.ossClient == null) {
            int ossType = 1;
            OssConfig ossConfig = ossConfigMapper.findByOssType(ossType);
            if (ossConfig == null) {
                throw new RuntimeException("OssClient not exist");
            }

            String endpoint = ossConfig.getEndpoint();
            String ak = ossConfig.getAccessKeyId();
            String sk = ossConfig.getAccessKeySecret();
            this.ossClient = new OssClient(endpoint, ak, sk);
        }

        return this.ossClient;
    }
}
