package io.renren.minio;

import io.minio.errors.*;
import io.renren.RenrenApplication;
import io.renren.modules.minio.pojo.MinioAlgorithm;
import io.renren.modules.minio.service.impl.MinioAlgorithmServiceImpl;
import io.renren.modules.minio.utils.MinIoUtils;
import io.renren.modules.minio.pojo.MinioFile;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = RenrenApplication.class)
public class uploadFile {
    @Autowired
    MinioAlgorithmServiceImpl minioAlgorithmService;

    @Test
    public void contextLoads() {
        System.out.println("123");
    }

    @Test
    public void uploadFile() {
        String bucketName = "publishedalgorithm";
        String prefix = "algorithm61";
        String destination = "/home/chenrj/gitrep";
        try {
            List<MinioFile> fileList = MinIoUtils.listFiles(bucketName, prefix);
            File folder = new File(destination + "/" + prefix);
            if (!folder.exists() && !folder.isDirectory()) {
                folder.mkdirs();
                System.out.println("创建文件夹");
            } else {
                System.out.println("文件夹已存在");
            }
            for (MinioFile file : fileList) {
                String fileName = file.getFilename();
                System.out.println(fileName);
                File localFile = new File(folder.getAbsolutePath() + "/" + fileName);
                if (!localFile.exists()) {
                    try {
                        localFile.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    System.out.println("文件已创建");
                } else {
                    System.out.println("文件已存在");
                }
                //从minio中获得目标文件的输入流
                InputStream inputStream = MinIoUtils.getFileInputStream(bucketName, prefix + "/" + fileName);
                MinIoUtils.writeToLocal(folder.getAbsolutePath() + "/" + fileName, inputStream);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        } catch (InvalidResponseException e) {
            e.printStackTrace();
        } catch (InsufficientDataException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InternalException e) {
            e.printStackTrace();
        } catch (XmlParserException e) {
            e.printStackTrace();
        } catch (InvalidBucketNameException e) {
            e.printStackTrace();
        } catch (ErrorResponseException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void getAlgorithmList() {

        List<MinioAlgorithm> minioAlgorithmList = minioAlgorithmService.getMinioAlgorithmList();
        for (MinioAlgorithm minioAlgorithm : minioAlgorithmList) {
            System.out.println(minioAlgorithm);
        }
    }

}
