package com.imdroid.beidou.utils;

import com.imdroid.beidou.config.AliyunOssProperties;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.internal.OSSHeaders;
import com.aliyun.oss.model.CannedAccessControlList;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.StorageClass;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.nio.file.Files;

/**
 * 阿里云oss
 *
 * @author LiGang
 */
@Component
@RequiredArgsConstructor
public class AliyunOssUtils {

    private final AliyunOssProperties ossProperties;

    /**
     * 发送报告至oss静态资源保存
     *
     * @param file 文件
     * @param filename 保存后的文件名称
     * @param directory 保存的文件夹
     * @return  文件访问url
     */
    public String uploadFile(File file, String filename, String directory) throws Exception {
        //连接oss
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getAccessSecret());
        String objectName = directory + "/" + filename;
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucket(), objectName, new ByteArrayInputStream(
                Files.readAllBytes(file.toPath())));
        //设置公共读
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        metadata.setObjectAcl(CannedAccessControlList.PublicRead);
        putObjectRequest.setMetadata(metadata);
        //上传文件
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();

        return ossProperties.getPublicReadUrl() + "/" + directory + "/" + filename;
    }

    /**
     * 发送报告至oss静态资源保存
     *
     * @param multipartFile 文件
     * @param filename 保存后的文件名称
     * @param directory 保存的文件夹
     * @return 文件访问url
     */
    public String uploadFile(MultipartFile multipartFile, String filename, String directory) throws Exception {
        //连接oss
        OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getAccessSecret());
        String objectName = directory + "/" + filename;
        byte[] data = multipartFile.getBytes();
        PutObjectRequest putObjectRequest = new PutObjectRequest(ossProperties.getBucket(), objectName, new ByteArrayInputStream(data));
        //设置公共读
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        metadata.setObjectAcl(CannedAccessControlList.PublicRead);
        putObjectRequest.setMetadata(metadata);
        //上传文件
        ossClient.putObject(putObjectRequest);
        ossClient.shutdown();

        return ossProperties.getPublicReadUrl() + "/" + directory + "/" + filename;
    }

    /**
     * 删除文件
     *
     * @param url 图片访问链接
     */
    public void deleteFile(String url) {
        try {
            String prefixUrl = ossProperties.getPublicReadUrl() + "/";
            String key = url.replace(prefixUrl, "");
            OSS ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(), ossProperties.getAccessKey(), ossProperties.getAccessSecret());
            ossClient.deleteObject(ossProperties.getBucket(), key);
            ossClient.shutdown();
        } catch (Exception e) {
            throw new RuntimeException("删除文件失败，请检查配置信息:[" + e.getMessage() + "]");
        }
    }

}
