package com.icloud.server.utils.oss;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.PutObjectResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

@Component
public class BlogsOss {
//url https://waterchat.oss-cn-beijing.aliyuncs.com/on
//    public static final String endpoint = "http://oss-cn-beijing.aliyuncs.com";
    public static final String endpoint = "http://oss-cn-shanghai.aliyuncs.com";
    // 阿里云主账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维，请登录 https://ram.console.aliyun.com 创建RAM账号。
    public static final String accessKeyId = "LTAI5tAxqejiEXdjST8FWLpJ";
    public static final String accessKeySecret = "dSMS4LSukweIAuiLF0mcHUgTG1jUJC";
        //传一个文件过来,返回uuid+日期+秒
    public String up(MultipartFile mFile) throws IOException {
        detection(mFile);
        //生成专有文件名
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String dateDir = sdf.format(new Date());
        String destfileName="";
        String baseDir="";
        String absolutePath="";
        String filename="";
        //存
        try {
            if(mFile != null||!mFile.isEmpty()){
                baseDir = "/etc/yang/";
                baseDir = "/yang/test/";
                File uploadDir = new File(baseDir + dateDir);
                if (!uploadDir.exists()) {
                    uploadDir.mkdirs();
                }
                String srcFileName = mFile.getOriginalFilename().replace(" ", "");
                String baseName = FilenameUtils.getBaseName(srcFileName);
                destfileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(srcFileName);
                File dest = new File(uploadDir, destfileName);
                try {
                    mFile.transferTo(dest);
                } catch (IOException e) {
                    e.printStackTrace();
                    throw new RuntimeException("请上传正确的图片1");
                }
                absolutePath = dest.getAbsolutePath();
                ///etc/yang/2018/08/31/c7aa7d53-1c2d-4624-a98e-72cdaa8c2a48.png
                //OSS
                OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
                filename=dateDir+UUID.randomUUID().toString()+"_"+baseName+"."+FilenameUtils.getExtension(srcFileName);
                //oss up
               File ossfile = new File(absolutePath);
                PutObjectResult waterchat = ossClient.putObject("oc-market", filename,ossfile );
                //如果不需要File文件可删除 ,此处是spring框架bug,spring为文件生成一个临时文件夹,文件不能直接转换成getInputStream()
                if(ossfile.exists()){
                    del(ossfile);
                }
                if(!waterchat.getETag().isEmpty()){
//                    return "https://waterchat.oss-cn-beijing.aliyuncs.com/"+name;
                    return "http://oc-market.finezb.com/"+filename;
                }
            }
        } catch (Exception e) {
        }
        return "";
    }
    public String savafile(MultipartFile[] M) throws IOException {

        //图片数组长度
        int length = M.length;
        StringBuffer str = new StringBuffer();
        for (int i = 0; i < length; i++) {
            if (str.length() != 0) {
                str.append(",");
            }
            String save = sig(M[i]);
            str.append(save);
        }
        return str.toString();
    }


    private String sig(MultipartFile mFile) throws IOException {
        String up = up(mFile);
        return up;
    }
    @Async( "taskExecutor" )
    public void del(File f){
        f.delete();
    }
    //涉黄检查 ,若检查不合格,直接跑出异常
    public void detection(MultipartFile mFile){


    }
    }