|  | @@ -2,6 +2,7 @@ package com.xunmei.system.service.impl;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  import cn.hutool.core.util.ObjectUtil;
 | 
	
		
			
				|  |  |  import cn.hutool.http.HttpException;
 | 
	
		
			
				|  |  | +import cn.hutool.http.HttpRequest;
 | 
	
		
			
				|  |  |  import cn.hutool.http.HttpResponse;
 | 
	
		
			
				|  |  |  import cn.hutool.http.HttpUtil;
 | 
	
		
			
				|  |  |  import com.alibaba.fastjson2.JSONArray;
 | 
	
	
		
			
				|  | @@ -10,19 +11,24 @@ import com.alibaba.fastjson2.TypeReference;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 | 
	
		
			
				|  |  |  import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.file.dto.FileDownloadDto;
 | 
	
		
			
				|  |  | +import com.xunmei.common.core.domain.file.dto.FileUploadByByteDto;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.thread.ThreadPoolConfig;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.util.BeanHelper;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.utils.DateUtils;
 | 
	
		
			
				|  |  |  import com.xunmei.common.core.web.page.TableDataInfo;
 | 
	
		
			
				|  |  |  import com.xunmei.common.security.utils.SecurityUtils;
 | 
	
		
			
				|  |  | +import com.xunmei.system.api.RemoteFileService;
 | 
	
		
			
				|  |  |  import com.xunmei.system.domain.SysServer;
 | 
	
		
			
				|  |  |  import com.xunmei.system.dto.server.SysServerEditDto;
 | 
	
		
			
				|  |  |  import com.xunmei.system.dto.server.SysServerPageDto;
 | 
	
		
			
				|  |  | +import com.xunmei.system.dto.server.TransferLogDto;
 | 
	
		
			
				|  |  |  import com.xunmei.system.mapper.SysServerMapper;
 | 
	
		
			
				|  |  |  import com.xunmei.system.service.ISysServerService;
 | 
	
		
			
				|  |  |  import com.xunmei.system.vo.server.CheckListVo;
 | 
	
		
			
				|  |  |  import com.xunmei.system.vo.server.FileInfoVo;
 | 
	
		
			
				|  |  |  import com.xunmei.system.vo.server.SysServerPageVo;
 | 
	
		
			
				|  |  | +import feign.Response;
 | 
	
		
			
				|  |  |  import org.springframework.beans.BeanUtils;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Autowired;
 | 
	
		
			
				|  |  |  import org.springframework.beans.factory.annotation.Qualifier;
 | 
	
	
		
			
				|  | @@ -30,13 +36,17 @@ import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
 | 
	
		
			
				|  |  |  import org.springframework.stereotype.Service;
 | 
	
		
			
				|  |  |  import org.springframework.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +import javax.servlet.http.HttpServletResponse;
 | 
	
		
			
				|  |  | +import java.io.*;
 | 
	
		
			
				|  |  |  import java.util.ArrayList;
 | 
	
		
			
				|  |  |  import java.util.Arrays;
 | 
	
		
			
				|  |  |  import java.util.List;
 | 
	
		
			
				|  |  | +import java.util.UUID;
 | 
	
		
			
				|  |  |  import java.util.concurrent.CompletableFuture;
 | 
	
		
			
				|  |  |  import java.util.concurrent.ExecutionException;
 | 
	
		
			
				|  |  |  import java.util.concurrent.TimeUnit;
 | 
	
		
			
				|  |  |  import java.util.concurrent.TimeoutException;
 | 
	
		
			
				|  |  | +import java.util.stream.Collectors;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  /**
 | 
	
		
			
				|  |  |   * 服务器维护Service业务层处理
 | 
	
	
		
			
				|  | @@ -49,7 +59,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      private SysServerMapper sysServerMapper;
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | +    @Autowired
 | 
	
		
			
				|  |  | +    private RemoteFileService fileService;
 | 
	
		
			
				|  |  |      @Autowired
 | 
	
		
			
				|  |  |      @Qualifier(ThreadPoolConfig.SOC_EXECUTOR)
 | 
	
		
			
				|  |  |      private ThreadPoolTaskExecutor threadPoolTaskExecutor;
 | 
	
	
		
			
				|  | @@ -93,9 +104,6 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
 | 
	
		
			
				|  |  |      public List<CheckListVo> getServerList(Long serverId) {
 | 
	
		
			
				|  |  |          try {
 | 
	
		
			
				|  |  |              SysServer server = this.selectSysServerById(serverId);
 | 
	
		
			
				|  |  | -            server.setServerToken("d033e22ae348aeb5660fc2140aec35850c4da997");
 | 
	
		
			
				|  |  | -            server.setServerIp("10.87.10.196");
 | 
	
		
			
				|  |  | -            server.setServerPort(8091);
 | 
	
		
			
				|  |  |              String baseUrl = String.format("http://%s:%d", server.getServerIp(), server.getServerPort());
 | 
	
		
			
				|  |  |              String getServicesApi = "/result/list?size=10000¤t=1";
 | 
	
		
			
				|  |  |              HttpResponse response = HttpUtil.createPost(baseUrl + getServicesApi)
 | 
	
	
		
			
				|  | @@ -112,13 +120,8 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |      @Override
 | 
	
		
			
				|  |  |      public Object getLogList(Long serverId, Long checkId) {
 | 
	
		
			
				|  |  | -        // http://10.87.10.196:8091/index1.html?token=d033e22ae348aeb5660fc2140aec35850c4da997
 | 
	
		
			
				|  |  |          SysServer server = this.selectSysServerById(serverId);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        server.setServerToken("d033e22ae348aeb5660fc2140aec35850c4da997");
 | 
	
		
			
				|  |  | -        server.setServerIp("10.87.10.196");
 | 
	
		
			
				|  |  | -        server.setServerPort(8091);
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |          String baseUrl = String.format("http://%s:%d", server.getServerIp(), server.getServerPort());
 | 
	
		
			
				|  |  |          String getLogs = "/result/fileList?checkId=" + checkId;
 | 
	
		
			
				|  |  |          //获取服务器列表
 | 
	
	
		
			
				|  | @@ -127,12 +130,117 @@ public class SysServerServiceImpl extends ServiceImpl<SysServerMapper, SysServer
 | 
	
		
			
				|  |  |                  .execute();
 | 
	
		
			
				|  |  |          String serverJson = response.body();
 | 
	
		
			
				|  |  |          List<FileInfoVo> checks = JSONArray.parseArray(serverJson, FileInfoVo.class);
 | 
	
		
			
				|  |  | +        final boolean match = checks.stream().anyMatch(file -> ObjectUtil.equal(2, file.getFileType()));
 | 
	
		
			
				|  |  | +        List<FileInfoVo> res = new ArrayList<>();
 | 
	
		
			
				|  |  | +        final List<String> exitFileNameList = fileService.checkFileExit(server.getServerIp());
 | 
	
		
			
				|  |  | +        if (match) {
 | 
	
		
			
				|  |  | +            final List<FileInfoVo> list = checks.stream().filter(file -> ObjectUtil.equal(2, file.getFileType())).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +            for (FileInfoVo fileInfoVo : list) {
 | 
	
		
			
				|  |  | +                res.add(fileInfoVo);
 | 
	
		
			
				|  |  | +                final List<FileInfoVo> list1 = checks.stream().filter(r -> ObjectUtil.equal(fileInfoVo.getId(), r.getParentId())).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +                checkExit(list1, server.getServerIp(), exitFileNameList);
 | 
	
		
			
				|  |  | +                fileInfoVo.setChildren(list1);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } else {
 | 
	
		
			
				|  |  | +            res.add(buildDir("info", checks, server.getServerIp(), exitFileNameList));
 | 
	
		
			
				|  |  | +            res.add(buildDir("error", checks, server.getServerIp(), exitFileNameList));
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +        return res;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void checkExit(List<FileInfoVo> checks, String serverIp, List<String> exitFileNameList) {
 | 
	
		
			
				|  |  | +        exitFileNameList = exitFileNameList.stream().map(r -> r.replace(File.separator, "/")).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +        for (FileInfoVo check : checks) {
 | 
	
		
			
				|  |  | +            final String fileName = serverIp + check.getParentPath().split("logs")[1] + File.separator + check.getFileName();
 | 
	
		
			
				|  |  | +            final String replace = fileName.replace(File.separator, "/");
 | 
	
		
			
				|  |  | +            final boolean match = exitFileNameList.stream().anyMatch(r -> r.contains(replace));
 | 
	
		
			
				|  |  | +            if (match) {
 | 
	
		
			
				|  |  | +                check.setExit(1);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private FileInfoVo buildDir(String dirName, List<FileInfoVo> list, String serverIp, List<String> exitFileNameList) {
 | 
	
		
			
				|  |  | +        FileInfoVo fileInfoVo = new FileInfoVo();
 | 
	
		
			
				|  |  | +        fileInfoVo.setFileName(dirName);
 | 
	
		
			
				|  |  | +        fileInfoVo.setFileSize("-");
 | 
	
		
			
				|  |  | +        fileInfoVo.setPath(dirName);
 | 
	
		
			
				|  |  | +        fileInfoVo.setFileType(2);
 | 
	
		
			
				|  |  | +        fileInfoVo.setId(UUID.randomUUID().toString());
 | 
	
		
			
				|  |  | +        final List<FileInfoVo> voList = list.stream().filter(r -> r.getFileName().contains(dirName)).collect(Collectors.toList());
 | 
	
		
			
				|  |  | +        checkExit(voList, serverIp, exitFileNameList);
 | 
	
		
			
				|  |  | +        fileInfoVo.setChildren(voList);
 | 
	
		
			
				|  |  | +        return fileInfoVo;
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void transferLog(TransferLogDto req) {
 | 
	
		
			
				|  |  | +        final SysServer sysServer = getById(req.getServerId());
 | 
	
		
			
				|  |  | +        if (ObjectUtil.isNull(sysServer)) {
 | 
	
		
			
				|  |  | +            throw new RuntimeException("服务器信息不存在");
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +        saveRemoteLogToLocal(sysServer, req.getPath(), req.getParentPath(), req.getRootDir());
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +    private void saveRemoteLogToLocal(SysServer server, String path, String parentPath, String rootDir) {
 | 
	
		
			
				|  |  | +        String downloadUrl = "http://" + server.getServerIp() + ":" + server.getServerPort() + "/result/fileDownload?path=" + path + "&parentPath=" + parentPath + "&rootDir=" + rootDir;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            final HttpRequest request = HttpUtil.createGet(downloadUrl);
 | 
	
		
			
				|  |  | +            HttpResponse response = request.execute();
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +            if (response.getStatus() == 200) {
 | 
	
		
			
				|  |  | +                FileUploadByByteDto fileUploadByByteDto = new FileUploadByByteDto();
 | 
	
		
			
				|  |  | +                fileUploadByByteDto.setFileByte(response.bodyBytes());
 | 
	
		
			
				|  |  | +                fileUploadByByteDto.setFileName(path);
 | 
	
		
			
				|  |  | +                fileUploadByByteDto.setFilePath(parentPath.split("logs")[1]);
 | 
	
		
			
				|  |  | +                fileUploadByByteDto.setServerName(server.getServerIp());
 | 
	
		
			
				|  |  | +                //远程调用文件服务上传流 到指定目录保持为文件
 | 
	
		
			
				|  |  | +                fileService.uploadFileByte(fileUploadByByteDto);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -        return checks;
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +        } catch (Exception e) {
 | 
	
		
			
				|  |  | +            e.printStackTrace();
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  |      }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +    @Override
 | 
	
		
			
				|  |  | +    public void downloadLogData(TransferLogDto req, HttpServletResponse response) {
 | 
	
		
			
				|  |  | +        final SysServer sysServer = getById(req.getServerId());
 | 
	
		
			
				|  |  | +        final String serverIp = sysServer.getServerIp();
 | 
	
		
			
				|  |  | +        FileDownloadDto dto=new FileDownloadDto();
 | 
	
		
			
				|  |  | +        dto.setServerName(serverIp);
 | 
	
		
			
				|  |  | +        dto.setFilePath(req.getParentPath().split("logs")[1]);
 | 
	
		
			
				|  |  | +        dto.setFileName(req.getPath());
 | 
	
		
			
				|  |  | +        final Response result = fileService.downloadLogData(dto);
 | 
	
		
			
				|  |  | +        InputStream inputStream = null;
 | 
	
		
			
				|  |  | +        BufferedInputStream bufferedInputStream = null;
 | 
	
		
			
				|  |  | +        BufferedOutputStream bufferedOutputStream = null;
 | 
	
		
			
				|  |  | +        try {
 | 
	
		
			
				|  |  | +            //把流返回到前端
 | 
	
		
			
				|  |  | +            Response.Body body = result.body();
 | 
	
		
			
				|  |  | +            inputStream = body.asInputStream();
 | 
	
		
			
				|  |  | +            bufferedInputStream = new BufferedInputStream(inputStream);
 | 
	
		
			
				|  |  | +            bufferedOutputStream = new BufferedOutputStream(response.getOutputStream());
 | 
	
		
			
				|  |  | +            response.setHeader("Content-Type", "application/octet-stream;charset=UTF-8");
 | 
	
		
			
				|  |  | +            response.addHeader("Content-Length", "" + body.length());
 | 
	
		
			
				|  |  | +            response.setHeader("Content-Disposition", result.headers().get("Content-Disposition").toString().replace("[", "").replace("]", ""));
 | 
	
		
			
				|  |  | +            int length = 0;
 | 
	
		
			
				|  |  | +            byte[] temp = new byte[1024 * 10];
 | 
	
		
			
				|  |  | +            while ((length = bufferedInputStream.read(temp)) != -1) {
 | 
	
		
			
				|  |  | +                bufferedOutputStream.write(temp, 0, length);
 | 
	
		
			
				|  |  | +            }
 | 
	
		
			
				|  |  | +            bufferedOutputStream.flush();
 | 
	
		
			
				|  |  | +            bufferedOutputStream.close();
 | 
	
		
			
				|  |  | +            bufferedInputStream.close();
 | 
	
		
			
				|  |  | +            inputStream.close();
 | 
	
		
			
				|  |  | +        } catch (IOException e) {
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +            throw new RuntimeException(e);
 | 
	
		
			
				|  |  | +        }
 | 
	
		
			
				|  |  | +    }
 | 
	
		
			
				|  |  |      /**
 | 
	
		
			
				|  |  |       * 查询服务器维护
 | 
	
		
			
				|  |  |       *
 |