| | |
| | | */ |
| | | @Transactional(rollbackFor = Exception.class) |
| | | public void check(MachineFile machineFile,List<MachineFile> allFilesInDir,Machine machine) throws IOException { |
| | | |
| | | String srcFilepath = MachineFileService.getBasePath(machine,machineFile.getDirType())+ File.separator+machineFile.getName(); |
| | | Path source = Paths.get(srcFilepath); |
| | | if(!source.toFile().exists()){ |
| | | log.info("开始检查文件{}",srcFilepath); |
| | | Path checkFilePath = Paths.get(srcFilepath); |
| | | if(!checkFilePath.toFile().exists()){ |
| | | return; |
| | | } |
| | | try(InputStream fileIns = Files.newInputStream(Paths.get(srcFilepath));) { |
| | | try(InputStream fileIns = Files.newInputStream(checkFilePath);) { |
| | | int excepType = MachineFile.EXCEPTION_OK; |
| | | if(!FileContentUtil.isTextFile(fileIns)){ |
| | | //非文本 |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_TEXT); |
| | | excepType = MachineFile.EXCEPTION_NOT_TEXT; |
| | | }else{ |
| | | Matcher matcher = RegExpConstants.PROGRAM_FILE_PATTERN.matcher(machineFile.getName()); |
| | | |
| | | boolean filenameValid = matcher.find(); |
| | | if(!filenameValid){ |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_BAD_FILENAME); |
| | | excepType = MachineFile.EXCEPTION_BAD_FILENAME; |
| | | }else { |
| | | ProgramNameVO progNameVO = ProgramFileNameParser.parseProgramName(machineFile.getName()); |
| | | String prefix = progNameVO.logicProgramName()+"-"; |
| | | long matchCount = allFilesInDir.stream().filter(file -> file.getName().startsWith(prefix)).count(); |
| | | if(matchCount != progNameVO.getSegmentCount()){//文件段数缺失 |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_LOST_FILES); |
| | | excepType = MachineFile.EXCEPTION_LOST_FILES; |
| | | }else{ |
| | | //检查是否匹配下发记录的段数 |
| | | //正负3秒作为查询时间 |
| | |
| | | ProgramNameVO sendProgNameVO = ProgramFileNameParser.parseProgramName(sendFile.getName()); |
| | | if(progNameVO.getSegmentCount() != sendProgNameVO.getSegmentCount()){ |
| | | //段数不匹配 |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_MATCH_SEND); |
| | | }else{ |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_OK); |
| | | excepType = MachineFile.EXCEPTION_NOT_MATCH_SEND; |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | machineFile.setExceptionType(excepType); |
| | | log.info("检查文件结束{}",srcFilepath); |
| | | machineFileService.updateById(machineFile); |
| | | } |
| | | } |