package com.ycwl.basic.utils;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.PutObjectRequest;
import com.ycwl.basic.config.OssConfig;
import com.ycwl.basic.enums.BizCodeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.InputStream;

@Slf4j
@Component
public class OssUtil {

    @Autowired
    private OssConfig ossConfig;

    /**
     * 上传文件到oss
     * @param inputStream 文件数据流
     * @param filename 文件全路径名称
     * @return
     */
    public String uploadFile(InputStream inputStream, String filename) {
        String uploadFileName = ossConfig.getObjectName() + filename;
        OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        try {
            PutObjectRequest putObjectRequest = new PutObjectRequest(ossConfig.getBucketName(), uploadFileName, inputStream);
            ossClient.putObject(putObjectRequest);
            String fileUrl = ossConfig.getUrl() + uploadFileName;
            return fileUrl;
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason."
                    + " \n Error Message:" + oe.getErrorMessage()
                    + " \n Error Code:" + oe.getErrorCode()
                    + " \n Request ID:" + oe.getRequestId()
                    + " \n Host ID:" + oe.getHostId()
            );

        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network."
                    + "Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }

        return BizCodeEnum.UPLOAD_FAILED.getMessage();
    }


    public boolean deleteFile(String filename) {
        // 填写文件完整路径。文件完整路径中不能包含Bucket名称。
        String objectName = filename;

        OSS ossClient = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccessKeyId(), ossConfig.getAccessKeySecret());
        try {
            // 删除文件或目录。如果要删除目录,目录必须为空。
            ossClient.deleteObject(ossConfig.getBucketName(), objectName);
            return true;
        } catch (OSSException oe) {
            log.error("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason."
                    + " \n Error Message:" + oe.getErrorMessage()
                    + " \n Error Code:" + oe.getErrorCode()
                    + " \n Request ID:" + oe.getRequestId()
                    + " \n Host ID:" + oe.getHostId()
            );
        } catch (ClientException ce) {
            log.error("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network."
                    + "Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return false;
    }

}