import { Singleton, Container, Scope, Inject } from "typescript-ioc";
import { Value, logger } from "mslib"
import { OSSClient } from './OSSClient'

/**
 * 
 * @date 2021-10-26 13:48:11
 * @author 吴锋霖
 */
@Singleton
class OSSFactory {

    private client: OSSClient;
    @Value('oss.region')
    region: string
    @Value('oss.accessKeyId')
    accessKeyId: string
    @Value('oss.accessKeySecret')
    accessKeySecret: string
    @Value('oss.bucket')
    bucket: string


    async init() {
        if (this.client) {
            return;
        }

        logger.info('准备初始化OSS客户端---------->region:%s,bucket:%s', this.region, this.bucket)
        this.client = new OSSClient(this.region, this.accessKeyId, this.accessKeySecret, this.bucket);

        Container.bind(OSSClient).factory(() => {
            let factory = Container.get(OSSFactory);
            return factory.get();
        }).scope(Scope.Singleton);
        logger.info('初始化OSS客户端完成---------->region:%s,bucket:%s', this.region, this.bucket)
    }

    get(): OSSClient {
        return this.client;
    }

}

export { OSSFactory }