package com.ruoyi.common.utils.file;

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.aliyun.oss.model.PutObjectResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.config.AliOssConfig;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;


public class AliOssUtils {


    public static String create(MultipartFile file) {
        // RAM用户的访问密钥（AccessKey ID和AccessKey Secret）。
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(AliOssConfig.getEndpointExternal(), AliOssConfig.getAccessKeyId(), AliOssConfig.getAccessKeySecret());
        try {
            InputStream inputStream = file.getInputStream();
            String orFileName = file.getOriginalFilename();
            String fileName = UUID.randomUUID() + orFileName;

            String time = DateUtils.parseDateToStr("yyyyMMdd", new Date());
            fileName = AliOssConfig.getPrefix_dir() + "/" + time + "/" + fileName;

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(AliOssConfig.getBucketName(), fileName, inputStream);


            // 如果需要上传时设置存储类型和访问权限，请参考以下示例代码。
            // ObjectMetadata metadata = new ObjectMetadata();
            // metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
            // metadata.setObjectAcl(CannedAccessControlList.Private);
            // putObjectRequest.setMetadata(metadata);

            // 上传字符串。
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            String url = AliOssConfig.getEndpointExternal().split("//")[0] + "//" + AliOssConfig.getBucketName() + "." + AliOssConfig.getEndpointExternal().split("//")[1] + "/" + fileName;
            return url;
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("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.");
            System.out.println("Error Message:" + ce.getMessage());
        } catch (IOException ce) {

        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return null;
    }
}
