package com.upload.config;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;

/**
 * <p>
 * 配置bean注入
 * </p>
 *
 * @author wcz
 * @since 2019/12/3
 */
@Slf4j
@Data
@Configuration
@ConfigurationProperties(prefix = "aliyun.oss")
public class SystemConfiguration {

    private String endpoint;

    private String bucketName;

    private String accessKeySecret;

    private String accessKeyId;

    @PostConstruct
    private void print() {
        log.info("Hint: the Configuration for Aliyun OSS Client.");
        log.info("============================================================");
        log.info("###| endpoint       : {}", endpoint);
        log.info("###| accessKeyId    : {}", accessKeyId);
        log.info("###| accessKeySecret: {}", accessKeySecret);
        log.info("###| bucketName     : {}", bucketName);
        log.info("============================================================");
    }

    /**
     * 创建aliyun OSS bean,每次使用完连接释放
     */
    public AliyunOSS build() {
        if (StrUtil.hasEmpty(endpoint, accessKeyId, accessKeySecret, bucketName)) {
            return AliyunOSS.me();
        }
        return AliyunOSS.me(endpoint, accessKeyId, accessKeySecret, bucketName);
    }
}
