package com.diagbot.config;

import lombok.extern.slf4j.Slf4j;
import org.csource.fastdfs.ClientGlobal;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Properties;

/**
 * @Description:
 * @author: gaodm
 * @time: 2019/3/1 14:15
 */
@Configuration
@Slf4j
public class FastDFSConfigurer {
    @Value("${fastdfs.connect_timeout_in_seconds}")
    private String connectTimeout;
    @Value("${fastdfs.network_timeout_in_seconds}")
    private String networkTimeout;
    @Value("${fastdfs.charset}")
    private String charset;
    @Value("${fastdfs.http_tracker_http_port}")
    private String httpTrackerHttpPort;
    @Value("${fastdfs.http_anti_steal_token}")
    private String httpAntiStealToken;
    @Value("${fastdfs.tracker_servers}")
    private String trackerServers;

    @Bean
    public Integer fastDFSInit(){
        try {
            Properties props = new Properties();
            props.put(ClientGlobal.PROP_KEY_CONNECT_TIMEOUT_IN_SECONDS, connectTimeout);
            props.put(ClientGlobal.PROP_KEY_NETWORK_TIMEOUT_IN_SECONDS, networkTimeout);
            props.put(ClientGlobal.PROP_KEY_CHARSET, charset);
            props.put(ClientGlobal.PROP_KEY_HTTP_TRACKER_HTTP_PORT, httpTrackerHttpPort);
            props.put(ClientGlobal.PROP_KEY_HTTP_ANTI_STEAL_TOKEN, httpAntiStealToken);
            props.put(ClientGlobal.PROP_KEY_TRACKER_SERVERS, trackerServers);
            ClientGlobal.initByProperties(props);

        } catch (Exception e) {
            log.error("FastDFS Client Init Fail!", e);
        }
        return 1;
    }
}
