| | |
| | | import org.springblade.mdm.machinefile.entity.MachineFile; |
| | | import org.springblade.mdm.machinefile.service.FileSendRecordService; |
| | | import org.springblade.mdm.machinefile.service.MachineFileService; |
| | | import org.springblade.mdm.machinefile.service.ReceiveFileCheckService; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springblade.mdm.utils.ProgramFileNameParser; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.annotation.EnableScheduling; |
| | | import org.springframework.scheduling.annotation.Scheduled; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import java.io.File; |
| | | import java.io.IOException; |
| | |
| | | @Autowired |
| | | private ParamService paramService; |
| | | @Autowired |
| | | private FileSendRecordService fileSendRecordService; |
| | | private ReceiveFileCheckService receiveFileCheckService; |
| | | /** |
| | | * 文件默认保存小时数,0不限制 |
| | | */ |
| | |
| | | |
| | | for(MachineFile machineFile : recFiles){ |
| | | try { |
| | | checkFile(machineFile, recFiles,machine); |
| | | receiveFileCheckService.check(machineFile, recFiles,machine); |
| | | }catch(Exception e){ |
| | | log.error("检查rec文件失败:"+machineFile.getName(),e); |
| | | log.error("检查rec文件失败:{}",machineFile.getName(),e); |
| | | } |
| | | } |
| | | |
| | |
| | | |
| | | } |
| | | |
| | | /** |
| | | * 文件移动到temp |
| | | * @param machineFile |
| | | */ |
| | | private void checkFile(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()){ |
| | | return; |
| | | } |
| | | try(InputStream fileIns = Files.newInputStream(Paths.get(srcFilepath));) { |
| | | if(!FileContentUtil.isTextFile(fileIns)){ |
| | | //非文本 |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_TEXT); |
| | | }else{ |
| | | ProgramNameVO progNameVO = ProgramFileNameParser.parseProgramName(machineFile.getName()); |
| | | if(!progNameVO.isValidFilename()){ |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_BAD_FILENAME); |
| | | }else { |
| | | 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); |
| | | }else{ |
| | | //检查是否匹配下发记录的段数 |
| | | //正负3秒作为查询时间 |
| | | Date beginTime = new Date(machineFile.getFileCreateTime().getTime()-3000); |
| | | Date endTime = new Date(machineFile.getFileCreateTime().getTime()+3000); |
| | | Optional<FileSendRecord> optFile = fileSendRecordService.lambdaQuery() |
| | | .eq(FileSendRecord::getMachineCode,machineFile.getMachineCode()) |
| | | .likeRight(FileSendRecord::getName,prefix).between(FileSendRecord::getCreateTime,beginTime,endTime).oneOpt(); |
| | | |
| | | if(optFile.isPresent()){ |
| | | //确实下发过,比对总段数是否相同 |
| | | FileSendRecord sendFile = optFile.get(); |
| | | ProgramNameVO sendProgNameVO = ProgramFileNameParser.parseProgramName(sendFile.getName()); |
| | | if(progNameVO.getSegmentCount() != sendProgNameVO.getSegmentCount()){ |
| | | //段数不匹配 |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_NOT_MATCH_SEND); |
| | | }else{ |
| | | machineFile.setExceptionType(MachineFile.EXCEPTION_OK); |
| | | } |
| | | } |
| | | } |
| | | } |
| | | } |
| | | |
| | | machineFileService.updateById(machineFile); |
| | | } |
| | | } |
| | | } |