package com.sptg.gateway;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.sptg.common.core.util.JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FilenameUtils;
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.data.redis.core.RedisTemplate;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * title: FileUploadTest
 * package: com.sptg.gateway
 * description: 文件上传测试用例
 *
 *                  
 *                 
 */
@RunWith(SpringRunner.class)
@SpringBootTest
@Slf4j
public class FileUploadTest {

    @Autowired
    private FastFileStorageClient storageClient;

    private final static List<String> IMG_URL_LIST = new ArrayList<>(Arrays.asList(
//            "C:\\Users\\PC-WFT\\Downloads\\0f05e0117e2c01c42832de159e794d2bf605ba966de74bab5fbfced096939ab808aed3ac500136d3b8ff19eb51b8ca9b.mp4",
//            "C:\\Users\\PC-WFT\\Downloads\\6bd7c774739bc9fbf02117459390b860865b92f1a24e0bc4c0db58d20c8ef39ec92a22c80b8ffb3dc33e5e09441e7eb3.mp4",
//            "C:\\Users\\PC-WFT\\Downloads\\46119599a83fdaba4467746ac86fb2c93922420779485b8240a5d231ba324163b12b93ff12560a6eb61a6f40dba3d950.mp4",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj16_gmuAD48qAADRZ923PUo560.png",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj16_Y0-ARVhcAACNYarXvt028.jpeg",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj16_Y2-AA7E8AACphCCj0kw981.png",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj16_ZJ-ANvBHAAC8C8OOqS8395.png",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj17DQFCAWb2JAAAmTZT4AsI30.jpeg",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj17F-F-AIEsTAANiuRR1Qa0978.jpg",
            "C:\\Users\\PC-WFT\\Downloads\\agzNj17F-R6ANsEtAAMfvsyfsEA401.jpg"
        ));

    @Test
    public void testUpload() {
        long count = 0;
        while (true){
            count++;
            IMG_URL_LIST.forEach(filePath -> {
                try {
                    File file = new File(filePath);
                    FileInputStream inputStream = new FileInputStream(file);
                    MultipartFile multipartFile = new MockMultipartFile("file", inputStream);
                    this.storageClient.uploadFile(multipartFile.getInputStream(), multipartFile.getSize(), "jpg", null);
                }catch (Exception e){
                    e.printStackTrace();
                }
            });
            long number = count * IMG_URL_LIST.size();
            log.warn("count:\t" + number);
            if (number >= 50000){
                break;
            }
        }

    }

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    @Test
    public void test()throws Exception{

        final RedisTemplate<String,String> template = this.redisTemplate;

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                for (int j = 0; j < 10; j++) {
                    template.opsForValue().set("key_1", "hello_" + j,10, TimeUnit.SECONDS);
                }
                System.out.println(Thread.currentThread().getName() + " 结束了");
            }).start();
        }

        Thread.sleep(1000 * 60 * 5);
        System.err.println(this.redisTemplate.opsForValue().get("key_1"));
    }

}
