package com.yunniu.farming.util;

import com.aliyun.oss.*;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

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

public class OssUtil {

    private static String endpoint = "oss-cn-hangzhou.aliyuncs.com";
    private static String accessKeyId = "LTAI5tAjz7s5946BNzARMyPB";
    private static String accessKeySecret = "aI7B55pXHNZrvm2LqCxDQyjGbytaca";
    private static String bucketName = "hwqflzx";


    public static void main(String[] args) {
        String s = uploadImgToOss("D:\\c389af50ddd9d334cea72d2e46ec342.jpg");
        System.out.println(s);
    }




    public static String uploadToOss(String path) {

        try {
            File file = new File(path);
            String originalFileName = file.getName();
            // 新的文件名称
            String objectName = UUIDUtils.getUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));
            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);

            // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
            // 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);
            // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
            Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 50);
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
            // 关闭OSSClient。
            ossClient.shutdown();
            //   file.delete();
            return url.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static String uploadImgToOss(String path) {

        try {
            File file = new File(path);
            String originalFileName = file.getName();
            // 新的文件名称
            String objectName = UUIDUtils.getUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);

            // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
            // 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);
            // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
            Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 50);
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
            // 关闭OSSClient。
            ossClient.shutdown();
            //file.delete();
            return url.toString();
        } catch (Exception e) {
            return null;
        }
    }


    public static String uploadVideoToOss(String path) {

        try {
            File file = new File(path);
            String originalFileName = file.getName();
            // 新的文件名称
            String objectName = UUIDUtils.getUUID() + originalFileName.substring(originalFileName.lastIndexOf("."));

            // 创建OSSClient实例。
            OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);

            // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
            // 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);
            // 生成以GET方法访问的签名URL，访客可以直接通过浏览器访问相关内容。
            Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 365 * 50);
            URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
            // 关闭OSSClient。
            ossClient.shutdown();
            file.delete();
            return url.toString();
        } catch (Exception e) {
            return null;
        }
    }

    public static void delOssImg(String str) {
        if (str != null && !"".equals(str)) {
            String arr[] = str.split(",");
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != null && !"".equals(arr[i])) {
                    delOssImgByName(getName(arr[i]));
                }
            }

        }
    }

    public static void delOssVideo(String str) {
        if (str != null && !"".equals(str)) {
            String arr[] = str.split(",");
            for (int i = 0; i < arr.length; i++) {
                if (arr[i] != null && !"".equals(arr[i])) {
                    delOssVideoByName(getName(arr[i]));
                }
            }

        }
    }

    public static void delOssImgByName(String objectName) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, objectName);
        ossClient.shutdown();
        System.out.println("删除成功" + objectName);

    }

    public static void delOssVideoByName(String objectName) {
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.deleteObject(bucketName, objectName);
        ossClient.shutdown();

    }

    public static void delOssImgByUrl(String urls) {
        if (urls != null && !"".equals(urls)) {
            String arr[] = urls.split(",");
            if (arr.length > 0) {
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] != null && !"".equals(arr[i])) {
                        String httpurl = arr[i];
                        if (arr[i].indexOf("?") > -1) {
                            httpurl = arr[i].substring(0, arr[i].indexOf("?"));
                        }
                        String objectName = httpurl.substring(httpurl.lastIndexOf("/") + 1);
                        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                        ossClient.deleteObject(bucketName, objectName);
                        ossClient.shutdown();
                    }
                }
            }
        }


    }

    public static void delOssVideoByUrl(String urls) {
        if (urls != null && !"".equals(urls)) {
            String arr[] = urls.split(",");
            if (arr.length > 0) {
                for (int i = 0; i < arr.length; i++) {
                    if (arr[i] != null && !"".equals(arr[i])) {
                        String httpurl = arr[i];
                        if (arr[i].indexOf("?") > -1) {
                            httpurl = arr[i].substring(0, arr[i].indexOf("?"));
                        }
                        String objectName = httpurl.substring(httpurl.lastIndexOf("/") + 1);
                        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
                        ossClient.deleteObject(bucketName, objectName);
                        ossClient.shutdown();
                    }
                }
            }
        }


    }

    public static String getName(String path) {
        int a = path.indexOf("?");
        if (a > 0) {
            path = path.substring(0, a);
        }
        return path.substring(path.lastIndexOf("/") + 1);
    }


}
