package com.persagy.dmp.file.config;

import com.persagy.dmp.file.service.IFileStorageService;
import com.persagy.dmp.file.service.impl.MinioStorageServiceImpl;
import io.minio.MinioClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * MinIO配置
 * @author Charlie Yu
 * @date 2021-05-15
 */
@Configuration
public class MinioConfig {

    /** 文件服务器url */
    @Value("${persagy.common.file.url}")
    private String url;
    /** 文件服务器accessKey */
    @Value("${persagy.common.file.accessKey}")
    private String accessKey;
    /** 文件服务器secretKey */
    @Value("${persagy.common.file.secretKey}")
    private String secretKey;
    /** 文件服务器secretKey */
    @Value("${persagy.common.file.minio.region:GM+8}")
    private String minioRegion;

    @Bean
    @ConditionalOnProperty(value = "persagy.common.file.storage", havingValue = "0",matchIfMissing = true)
    public MinioClient minioClient() {
        return MinioClient.builder().endpoint(url).credentials(accessKey, secretKey).region(minioRegion).build();
    }

    @Bean
    @ConditionalOnProperty(value = "persagy.common.file.storage", havingValue = "0",matchIfMissing = true)
    public IFileStorageService minioStorageServiceImpl() {
        return new MinioStorageServiceImpl(minioClient());
    }
}
