package org.ta.pddserver.cos;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;


public class Uploader {
    private static final String OSS_END_POINT = "oss-cn-qingdao.aliyuncs.com";
    private static final String OSS_ACCESS_KEY_ID = "LTAI5t8BWUq8KYFPaE6qoAt6";
    private static final String OSS_ACCESS_KEY_SECRET = "WxtupLLdjZLPBwtJ8SL91W5IYVGBdF";
    private static final String OSS_BUCKET_NAME = "tradeany-server";


    public String uploadOssFile(MultipartFile file) {
        String endpoint = OSS_END_POINT;
        String accessKeyId = OSS_ACCESS_KEY_ID;
        String accessKeySecret = OSS_ACCESS_KEY_SECRET;
        String bucketName = OSS_BUCKET_NAME;
        String url = null;
        try {
            //创建OSSClient实例。
            OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

            //获取上传文件输入流
            InputStream inputStream = file.getInputStream();
            //获取文件名称
            String fileName = file.getOriginalFilename();

            //保证文件名唯一，去掉uuid中的'-'
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;

            //把文件按日期分类，构建日期路径：avatar/2019/02/26/文件名
            //String datePath = new Date().toString("yyyy/MM/dd");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String time = formatter.format(new Date());
            String eName = fileName.substring(fileName.lastIndexOf("."));
            //拼接
            fileName = "files/" + time + "/" + uuid + eName;

            //调用oss方法上传到阿里云
            //第一个参数：Bucket名称
            //第二个参数：上传到oss文件路径和文件名称
            //第三个参数：上传文件输入流
            ossClient.putObject(bucketName, fileName, inputStream);

            //把上传后把文件url返回
            //https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
            url = "https://images.v2.tradeany.com/" + fileName;
            System.out.println("file:" + url);
            //关闭OSSClient
            ossClient.shutdown();
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }


    public static String uploadOssInputStream(InputStream inputStream, String fileName) {
        String endpoint = OSS_END_POINT;
        String accessKeyId = OSS_ACCESS_KEY_ID;
        String accessKeySecret = OSS_ACCESS_KEY_SECRET;
        String bucketName = OSS_BUCKET_NAME;
        String url = null;
        try {
            //创建OSSClient实例。
            OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

            //保证文件名唯一，去掉uuid中的'-'
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            fileName = uuid + fileName;

            //把文件按日期分类，构建日期路径：avatar/2019/02/26/文件名
            //String datePath = new Date().toString("yyyy/MM/dd");
            SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
            String time = formatter.format(new Date());
            String eName = fileName.substring(fileName.lastIndexOf("."));
            //拼接
            fileName = "files/" + time + "/" + uuid + eName;

            //调用oss方法上传到阿里云
            //第一个参数：Bucket名称
            //第二个参数：上传到oss文件路径和文件名称
            //第三个参数：上传文件输入流
            ossClient.putObject(bucketName, fileName, inputStream);

            //把上传后把文件url返回
            //https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
            url = "https://images.v2.tradeany.com/" + fileName;
            System.out.println("file:" + url);
            //关闭OSSClient
            ossClient.shutdown();
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }



    /**
     * 上传到阿里云
     *
     * @param stream 文件内容
     * @param filename 带路径文件名 postFiles/111.abc
     * @return 链接
     */
    public static String uploadOssStream(ByteArrayOutputStream stream, String filename) {

        String endpoint = OSS_END_POINT;
        String accessKeyId = OSS_ACCESS_KEY_ID;
        String accessKeySecret = OSS_ACCESS_KEY_SECRET;
        String bucketName = OSS_BUCKET_NAME;
        String url = null;
        try {
            //创建OSSClient实例。
            OSS ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

            //保证文件名唯一，去掉uuid中的'-'
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            filename = "pdfFiles/" +uuid +filename;

            //获取上传文件输入流
            ByteArrayInputStream inputStream = null;
            try {
                byte[] data = stream.toByteArray();
                inputStream = new ByteArrayInputStream(data);
            } catch (Exception e) {
                e.printStackTrace();
            }

            //调用oss方法上传到阿里云
            //第一个参数：Bucket名称
            //第二个参数：上传到oss文件路径和文件名称
            //第三个参数：上传文件输入流
            ossClient.putObject(bucketName, filename, inputStream);

            //把上传后把文件url返回
            //https://xppll.oss-cn-beijing.aliyuncs.com/01.jpg
            url = "https://images.v2.tradeany.com/" + filename;
            System.out.println("file:" + url);
            //关闭OSSClient
            ossClient.shutdown();
            return url;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return url;
    }

}
