package com.tax.file.test;

import com.tax.file.constants.CommonConstants;
import com.yxcheng.tax.fs.minio.service.MinioTemplate;
import com.yxcheng.token.utils.StringUtil;
import org.assertj.core.util.Lists;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.List;

public class TestMinio {
    MinioTemplate template = null;

    String url = "http://101.201.74.244:9000";
    String downUrl ="http://file.taxservices.cn";
    String accessKey ="JHJG2Y3HVTM48YUU2ZYW";
    String secretKey ="TAe9uLLOj+frn0s3xXr+3gPozqmJDHIWfBqXSOEq";

    @Before
    public void getTemplate(){


//        String url ="http://file.taxservices.cn";
//        String accessKey ="8X7R393JCYQMRQ1120SJ";
//        String secretKey ="7CR+iYTh8GOWmQ8OUIyOWUghkVQwXGFcrt0UpAeU";

//        String url ="http://localhost:9000";
//        String accessKey ="admin";
//        String secretKey ="admin123";
        template = new MinioTemplate(url,downUrl,accessKey,secretKey);
        try {
            template.afterPropertiesSet();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



    @Test
    public void upload(){
//        String path = "D:/test.jpg";
//        String fileKey = "test.jpg";
        String path = "D:\\changchen\\sms项目\\Looper_v1.0.6_2022-05-19.apk";
        String fileKey = "/smsApk/1.0.6/Looper_v1.0.6_2022-05-19.apk";
        System.out.println(fileKey);
        try {
            String bucketName = "fileq";
            template.putObject(bucketName, fileKey, new FileInputStream(path));
            String fileSysUrl = template.getObjectURL(bucketName, fileKey, CommonConstants.EXPIRES);
            System.out.println(fileSysUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void uploadTest(){
        String path = "D:/test.jpg";
        String fileKey = "test.jpg";
        System.out.println(fileKey);
        try {
            String bucketName = "fileq";
            template.putObject(bucketName, fileKey, new FileInputStream(path));
            String fileSysUrl = template.getObjectURL(bucketName, fileKey, CommonConstants.EXPIRES);
            if(StringUtil.isNotEmpty(fileSysUrl)) {
                fileSysUrl = fileSysUrl.split("\\?")[0];
                fileSysUrl = fileSysUrl.replace(template.getEndpoint(), template.getDownEndpoint());
            }
            System.out.println(fileSysUrl);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Test
    public void stat(){

        List<String> a = Lists.newArrayList();
        a.add("2c9083f57df665b6017dfe842f8b2d90.zip");

//        String fileKey = "/2022/01/30/qixiangyun/91512021MA66BYY378/2/03/20210131/2c90b2fc7ea94c0a017ea95b9ce73c7a.zip";
//        System.out.println(fileKey);
        for(String fileKey:a) {
            try {
                String bucketName = "fileq";
//            ObjectStat fileSysUrl = template.getObjectInfo(CommonConstants.BUCKET_NAME + "x", fileKey);
                String fileSysUrl = template.getObjectURL(bucketName, fileKey, CommonConstants.EXPIRES);
                if(StringUtil.isNotEmpty(fileSysUrl)) {
                    fileSysUrl = fileSysUrl.split("\\?")[0];
                }
                System.out.println(fileSysUrl);
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("==========");
            }
        }


    }
    @Test
    public void remove(){
        String fileKey = "/2022/01/30/qixiangyun/91512021MA66BYY378/2/03/20210131/2c90b2fc7ea94c0a017ea95b9ce73c7a.zip";
        System.out.println(fileKey);
        try {
            template.removeObject(CommonConstants.BUCKET_NAME + "X", fileKey);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}
