/*
package com.cusc.nirvana.user.rnr.fp.common.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.*;
import com.cusc.nirvana.user.exception.CuscUserException;
import com.cusc.nirvana.user.rnr.fp.dto.OssDownloadRs;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.net.URL;
import java.util.Date;
import java.util.List;

@Data
public class OSSUtil {

    private static  String endpoint;
    private static  String accessKeyId;
    private static  String accessKeySecret;
    private static  String bucketName;
    private static  String prefix;

    private static OSS ossClient;

    public OSSUtil(String endpoint, String bucketName, String accessKey, String secretKey, String prefix) {
        OSSUtil.endpoint = endpoint;
        OSSUtil.bucketName = bucketName;
        OSSUtil.accessKeyId = accessKey;
        OSSUtil.accessKeySecret = secretKey;
        OSSUtil.prefix = prefix;
        getOSSClient();
    }

    */
/**
     * 获取oss客户端实例
     * @return
     *//*

    public static OSS getOSSClient(){
//        if(null == ossClient){
//            // 创建OSSClient实例。
//            ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
//        }
//        return  ossClient;
        return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
    }

    */
/**
     * 本地文件上传
     *
     * @param sourceFilePathName 本地文件路径
     * @param aimFilePathName    在阿里OSS中保存的可以包含路径的文件名
     * @return 返回上传后文件的访问路径
     * @throws FileNotFoundException
     *//*

    public static String upload(String bucketName, String sourceFilePathName, String aimFilePathName) throws FileNotFoundException {
        FileInputStream is = new FileInputStream(sourceFilePathName);

        if (aimFilePathName.startsWith("/")) {
            aimFilePathName = aimFilePathName.substring(1);
        }

//        如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
//        ObjectMetadata metadata = new ObjectMetadata();
//        int indexOfLastDot = aimFilePathName.lastIndexOf(".");
//        String suffix = aimFilePathName.substring(indexOfLastDot);
//        metadata.setContentType(getContentType(suffix));

        //避免文件覆盖
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, aimFilePathName, is);
        //避免访问时将图片下载下来
        //putObjectRequest.setMetadata(metadata);
        OSS ossClient = getOSSClient();

        ossClient.putObject(putObjectRequest);
        // 设置URL过期时间为100年
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
        URL url = ossClient.generatePresignedUrl(bucketName, aimFilePathName, expiration);
        ossClient.shutdown();

        return url.toString();
    }

    */
/**
     * 流上传文件
     *
     * @param is 文件流
     * @param aimFilePathName    在阿里OSS中保存的可以包含路径的文件名
     * @return 返回上传后文件的访问路径
     * @throws FileNotFoundException
     *//*

    public static String upload(String bucketName, InputStream is, String aimFilePathName) throws FileNotFoundException {
        if (aimFilePathName.startsWith("/")) {
            aimFilePathName = aimFilePathName.substring(1);
        }
        //避免文件覆盖
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, aimFilePathName, is);
        //避免访问时将图片下载下来
        //putObjectRequest.setMetadata(metadata);
        OSS ossClient = getOSSClient();

        ossClient.putObject(putObjectRequest);
        // 设置URL过期时间为100年
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
        URL url = ossClient.generatePresignedUrl(bucketName, aimFilePathName, expiration);
        ossClient.shutdown();

        return url.toString();
    }

    */
/**
     * 获取文件大小
     *
     * @param aimFilePathName    在阿里OSS中保存的可以包含路径的文件名
     * @return 返回上传后文件的访问路径
     * @throws FileNotFoundException
     *//*

    public static Double getFileSize(String bucketName, String aimFilePathName) throws FileNotFoundException {
        OSS ossClient = getOSSClient();
        boolean existFlag = ossClient.doesObjectExist(bucketName, aimFilePathName);
        if(!existFlag){
            throw new CuscUserException("500","文件不存在");
        }
        Double fileSize = 0D;
        ObjectMetadata objectMetadata = ossClient.getObjectMetadata(bucketName, aimFilePathName);
        fileSize = Double.valueOf(objectMetadata.getContentLength());
        ossClient.shutdown();
        return fileSize;
    }

    */
/**
     * multipartFile实现上传
     *
     * @param multipartFile
     * @return
     *//*

    public static String upload(String bucketName, MultipartFile multipartFile) throws IOException {
        // 获取上传的文件的输入流
        InputStream inputStream = multipartFile.getInputStream();
        // 获取文件名称
        String fileName = multipartFile.getOriginalFilename();

        // 拼接fileName
        fileName = prefix + fileName;

        // 如果需要上传时设置存储类型与访问权限
        //ObjectMetadata metadata = new ObjectMetadata();
        //metadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));

        // 上传文件到OSS时需要指定包含文件后缀在内的完整路径，例如abc/efg/123.jpg。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);
        //putObjectRequest.setMetadata(metadata);

        OSS ossClient = getOSSClient();
        ossClient.putObject(putObjectRequest);
        //文件访问路径
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
        URL url = ossClient.generatePresignedUrl(bucketName, fileName, expiration);

        // 关闭ossClient
        ossClient.shutdown();
        // 把上传到oss的路径返回
        return url.toString();
    }

    */
/**
     * 判断文件是否存在,文件名需要完整路径，
     * 如rnr_test/1.jpg
     *
     * @param objectName
     * @return
     *//*

    public static boolean isFileExist(String bucketName, String objectName) {
        OSS ossClient = getOSSClient();
        boolean res = ossClient.doesObjectExist(bucketName, objectName);
        return res;
    }

    */
/**
     * 根据文件名获取文件路径
     *
     * @param bucketName  要下载的文件名
     * @param fileName    文件完整名称
     * *//*

    public static String getFileUrl(String bucketName, String fileName) {
        OSS ossClient = getOSSClient();

        //文件访问路径
        Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 100);
        URL url = ossClient.generatePresignedUrl(bucketName, fileName, expiration);
        // 关闭ossClient
        ossClient.shutdown();

        return url.toString();
    }

    */
/**
     * 通过文件名下载文件到本地
     *
     * @param objectName    要下载的文件名
     * @param localFileName 本地要创建的文件名
     *//*

    public static void downloadFile(String bucketName, String objectName, String localFileName) {
        OSS ossClient = getOSSClient();

        // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
        ossClient.getObject(new GetObjectRequest(bucketName, objectName), new File(localFileName));
        // 关闭OSSClient。
        ossClient.shutdown();
    }

    */
/**
     * 通过文件名获取下载流
     *
     * @param objectName    要下载的文件名
     *//*

    public static OssDownloadRs downloadFile(String bucketName, String objectName) {
        OSS ossClient = getOSSClient();
        // 下载OSS文件到本地文件。如果指定的本地文件存在会覆盖，不存在则新建。
        OSSObject object = ossClient.getObject(new GetObjectRequest(bucketName, objectName));
        InputStream inputStream = object.getObjectContent();
        OssDownloadRs ossDownloadRs = new OssDownloadRs();
        ossDownloadRs.setOss(ossClient);
        ossDownloadRs.setInputStream(inputStream);
        // 关闭OSSClient。
//        ossClient.shutdown();
        return ossDownloadRs;
    }

    */
/**
     * 删除文件
     *
     * @param objectName
     *//*

    public static void deleteFile(String bucketName, String objectName) {
        OSS ossClient = getOSSClient();

        ossClient.deleteObject(bucketName, objectName);
        ossClient.shutdown();
    }

    */
/**
     * 批量删除文件或目录
     *
     * @param keys
     *//*

    public static void deleteFiles(String bucketName, List<String> keys) {
        OSS ossClient = getOSSClient();

        // 删除文件。
        DeleteObjectsResult deleteObjectsResult = ossClient.deleteObjects(new DeleteObjectsRequest(bucketName).withKeys(keys));
        java.util.List<String> deletedObjects = deleteObjectsResult.getDeletedObjects();

        ossClient.shutdown();
    }

    */
/**
     * 创建文件夹
     *
     * @param folder
     * @return
     *//*

    public static String createFolder(String bucketName, String folder) {
        // 文件夹名
        String fileName = folder;
        OSS ossClient = getOSSClient();

        // 判断文件夹是否存在，不存在则创建
        if (!ossClient.doesObjectExist(bucketName, fileName)) {
            // 创建文件夹
            ossClient.putObject(bucketName, fileName, new ByteArrayInputStream(new byte[0]));
            // 得到文件夹名
            OSSObject object = ossClient.getObject(bucketName, fileName);
            return object.getKey();
        }
        ossClient.shutdown();

        return fileName;
    }


    */
/**
     * @return String
     * @MethodName: contentType
     * @Description: 获取文件类型
     *//*

    private static String contentType(String fileType) {
        fileType = fileType.toLowerCase();
        String contentType = "";
        if (fileType.equals("bmp")) {
            contentType = "image/bmp";

        } else if (fileType.equals("gif")) {
            contentType = "image/gif";

        } else if (fileType.equals("png") || fileType.equals("jpeg") || fileType.equals("jpg")) {
            contentType = "image/jpeg";

        } else if (fileType.equals("html")) {
            contentType = "text/html";

        } else if (fileType.equals("txt")) {
            contentType = "text/plain";

        } else if (fileType.equals("vsd")) {
            contentType = "application/vnd.visio";

        } else if (fileType.equals("ppt") || fileType.equals("pptx")) {
            contentType = "application/vnd.ms-powerpoint";

        } else if (fileType.equals("doc") || fileType.equals("docx")) {
            contentType = "application/msword";

        } else if (fileType.equals("xml")) {
            contentType = "text/xml";

        } else if (fileType.equals("mp4")) {
            contentType = "video/mp4";

        } else {
            contentType = "application/octet-stream";

        }
        return contentType;
    }
}*/
