package com.dcsoft.file.utils;

import com.dcsoft.common.core.exception.file.FileNameLengthLimitExceededException;
import com.dcsoft.common.core.exception.file.FileSizeLimitExceededException;
import com.dcsoft.common.core.exception.file.InvalidExtensionException;
import com.dcsoft.common.core.utils.DateUtils;
import com.dcsoft.common.core.utils.SpringUtils;
import com.dcsoft.common.core.utils.StringUtils;
import com.dcsoft.common.core.utils.file.FileTypeUtils;
import com.dcsoft.common.core.utils.file.MimeTypeUtils;
import com.dcsoft.common.core.utils.uuid.Seq;
import io.minio.*;
import io.minio.http.Method;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.BASE64Encoder;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.Objects;

/**
 * 文件上传工具类
 *
 * @author dcsoft
 */
/** 初始化了连接方法,及桶权限获取*/
public class MinioUtil {
    /**
     * @param bucketName 桶名称
     * @param fileName
     * @throws IOException
     */
    public static String uploadFile(String bucketName,String fileName,MultipartFile multipartFile) throws IOException
    {
        String url = "";
        MinioClient minioClient = SpringUtils.getBean(MinioClient.class);
        try (InputStream inputStream = multipartFile.getInputStream()) {
            boolean found = minioClient.bucketExists(BucketExistsArgs.builder().bucket(bucketName).build());
            if (!found)
            {
                minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
                /**
                 * bucket权限-读写
                 */
                String READ_WRITE = "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:GetBucketLocation\",\"s3:ListBucket\",\"s3:ListBucketMultipartUploads\"],\"Resource\":[\"arn:aws:s3:::" + bucketName + "\"]},{\"Effect\":\"Allow\",\"Principal\":{\"AWS\":[\"*\"]},\"Action\":[\"s3:DeleteObject\",\"s3:GetObject\",\"s3:ListMultipartUploadParts\",\"s3:PutObject\",\"s3:AbortMultipartUpload\"],\"Resource\":[\"arn:aws:s3:::" + bucketName + "/*\"]}]}";
                minioClient.setBucketPolicy(SetBucketPolicyArgs.builder().bucket(bucketName).config(READ_WRITE).build());
            }
            minioClient.putObject(PutObjectArgs.builder().bucket(bucketName)
                    .object(fileName)
                    .stream(inputStream, multipartFile.getSize(), -1)
                    .contentType(multipartFile.getContentType()).build()
            );
            //路径获取
            url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).
                    method(Method.GET).build());
            url = url.substring(0,url.indexOf('?'));
            //常规访问路径获取
            return url;
        } catch (Exception e) {
            throw new IOException(e.getMessage(), e);
        }
    }

    public static String getBase64String(MultipartFile multiPartFile) throws IOException {
        String baseStr = null;
        BASE64Encoder encoder = new BASE64Encoder();
        baseStr= encoder.encode(multiPartFile.getBytes());
        baseStr = baseStr.replaceAll("\r\n", "");
        return baseStr;
    }

}
