package com.css.simulation.resource.scheduler.app.service;

import api.common.pojo.common.ResponseBodyVO;
import api.common.util.FileUtil;
import com.css.simulation.resource.scheduler.infra.fs.minio.MinioUtil;
import io.minio.MinioClient;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.InputStream;

@Service
public class AlgorithmApplicationService {


    @Resource
    MinioClient minioClient;
    @Value("${minio.bucket-name}")
    String bucketName;
    @Value("${scheduler.linux-path.temp}")
    String linuxTempPath;

    @SneakyThrows
    public ResponseBodyVO<String> check(String minioPath) { 

        String[] split = minioPath.split("\\.");
        int length = split.length;
        String s = split[length - 1];   //todo 必须是.tar文件，.tar.gz会报错

        //1 根据 minio 路径获取输入流
        InputStream inputStream = MinioUtil.downloadToStream(minioClient, bucketName, minioPath);
//        FileUtil.writeInputStreamToLocalFile();
        //2 判断压缩包中是否有 docker-entrypoint.sh
        if (FileUtil.scan(inputStream, "." + s, ".dockerenv")){
            return new ResponseBodyVO<>(ResponseBodyVO.Response.SUCCESS, "算法可用！", null);
        } else {
            return new ResponseBodyVO<>(ResponseBodyVO.Response.CLIENT_FAILURE, "算法不可用！", null);
        }
    }
}
