package com.jay.monitor.data.server.componts;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectResult;
import com.jay.monitor.data.server.config.props.OSSProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

/**
 * @Module TODO
 * @Description TODO
 * @Author liukaixiong
 * @Date 2020/12/15 15:43
 */
public class OOSInvoke {
    private Logger logger = LoggerFactory.getLogger(getClass());
    private OSS oss;
    private OSSProperties ossProperties;
    private String defaultEndpointPath;
    private String defaultBucketName;
    private String defaultBucketPath;

    public OOSInvoke(OSS oss, OSSProperties ossProperties) {
        this.oss = oss;
        this.ossProperties = ossProperties;
        this.defaultEndpointPath = ossProperties.getEndpoint().replace("https://", "");
        this.defaultBucketName = ossProperties.getDefaultBucketName();
        this.defaultBucketPath = ossProperties.getDefaultBucketPath();
    }

    //https://linux-data.oss-cn-shanghai.aliyuncs.com/alert/image/abc.txt

    /**
     * 上传文件
     *
     * @param bucketName 桶名称 参考oss控制台
     * @param path       文件路径
     * @param fileName   文件名称
     * @param in         文件流
     * @return
     */
    public String uploadFile(String bucketName, String path, String fileName, InputStream in) {
        Assert.notNull(bucketName, " bucketName is not null");
        Assert.notNull(path, " path is not null");
        Assert.notNull(fileName, " fileName is not null");
        Assert.notNull(in, " inputStream is not null");
        try {
            if (path.startsWith("/")) {
                path = path.replace("/", "");
            }

            String file = path + "/" + fileName;
            logger.debug(" upload oos fileName : " + file);
            PutObjectResult putObjectResult = this.oss.putObject(bucketName, file, in);
            logger.info("上传结果 : " + JSON.toJSONString(putObjectResult));
            if (putObjectResult.getETag() != null) {
                // 如果根据url查找不到则请去oss控制台得到这个bucket下面将读写权限设置为可读
                return "https://" + bucketName + "." + this.defaultEndpointPath + "/" + file;
            } else {
                logger.warn("上传失败");
            }
        } catch (Exception e) {
            logger.error("oss 上传失败", e);
        }
        return null;
    }

    /**
     * 上传文件
     *
     * @param fileName
     * @param file
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, File file) throws Exception {
        return uploadFile(this.defaultBucketName, this.defaultEndpointPath, fileName, new FileInputStream(file));
    }

    /**
     * 上传文件
     *
     * @param fileName
     * @param in
     * @return
     * @throws Exception
     */
    public String uploadFile(String fileName, InputStream in) {
        return uploadFile(this.defaultBucketName, this.defaultEndpointPath, fileName, in);
    }

    public String uploadFile(String bucketName, String path, String fileName, File file) throws Exception {
        return uploadFile(bucketName, path, fileName, new FileInputStream(file));
    }


}
