| | |
| | | import org.apache.commons.io.FileUtils; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | import org.springblade.core.log.exception.ServiceException; |
| | | import org.springblade.core.mp.base.BizEntity; |
| | | import org.springblade.core.mp.base.BizServiceImpl; |
| | | import org.springblade.core.redis.cache.BladeRedis; |
| | | import org.springblade.core.secure.utils.AuthUtil; |
| | | import org.springblade.core.tool.utils.Func; |
| | | import org.springblade.mdm.basesetting.machine.MachineService; |
| | | import org.springblade.mdm.basesetting.machine.service.MachineService; |
| | | import org.springblade.mdm.basesetting.machine.entity.Machine; |
| | | import org.springblade.mdm.machinefile.entity.FileSendRecord; |
| | | import org.springblade.mdm.machinefile.service.FileSendRecordService; |
| | | import org.springblade.mdm.program.entity.NcProgramExchange; |
| | | import org.springblade.mdm.program.mapper.NcProgramExchangeMapper; |
| | | import org.springblade.mdm.program.service.programannotation.AnnotationProperties; |
| | | import org.springblade.mdm.program.vo.MdmProgramImportVO; |
| | | import org.springblade.mdm.utils.FileContentUtil; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.scheduling.support.SimpleTriggerContext; |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | |
| | | @Autowired |
| | | private MachineService machineService; |
| | | |
| | | @Autowired |
| | | private FileSendRecordService fileSendRecordService; |
| | | |
| | | private String getFileKey(){ |
| | | return "mdmgkwimpfile-"+ AuthUtil.getUserId(); |
| | | } |
| | |
| | | |
| | | // 创建解压目标目录(在临时目录下创建一个唯一子目录) |
| | | Path tempZipFile = Files.createTempFile("mdmimpfile-"+System.currentTimeMillis(), ".zip"); |
| | | //Path extractDir = Files.createTempDirectory(tempPath, "unzip_"); |
| | | |
| | | file.transferTo(tempZipFile); |
| | | |
| | |
| | | extractZipToTempDir(tempZipFile,extractDir); |
| | | |
| | | //读取文件目录 |
| | | /* |
| | | BladeFile bfile = ossTemplate.putFile(file);//上传,供后续入库使用 |
| | | |
| | | //设置一个缓存,2小时过期 |
| | | InputStream zipFileInputStream = file.getInputStream();//test |
| | | |
| | | byte[] bytes = FileUtil.copyToByteArray(zipFileInputStream); |
| | | list = parseMdmZipFromByteArray(bytes); |
| | | */ |
| | | list = readTempDir(extractDir); |
| | | } catch (IOException e) { |
| | | log.error("导入涉密网摆渡文件失败",e); |
| | |
| | | Path tempPath = Paths.get(tempDir); |
| | | |
| | | // 创建解压目标目录(在临时目录下创建一个唯一子目录) |
| | | //Path extractDir = Files.createTempDirectory(tempPath, "unzip_"); |
| | | System.out.println("解压目录: " + extractDir.toString()); |
| | | //Files.newInputStream(Paths.get(zipFilePath)); |
| | | try (InputStream fis = Files.newInputStream(zipFilePath); |
| | | ZipInputStream zis = new ZipInputStream(fis)) { |
| | | |
| | |
| | | |
| | | return normalizePath; |
| | | } |
| | | /* |
| | | public static List<MdmProgramImportVO> parseMdmZipFromByteArray(byte[] zipData) throws IOException { |
| | | List<MdmProgramImportVO> list = new ArrayList<>(); |
| | | Map<String,String> fileMd5Map = new HashMap<>(); |
| | | Map<String,MdmProgramImportVO> fileDataMap = new HashMap<>(); |
| | | try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel(zipData); |
| | | ZipFile zipFile = new ZipFile(channel)) { |
| | | |
| | | ZipArchiveEntry entry; |
| | | Enumeration<ZipArchiveEntry> entries = zipFile.getEntries(); |
| | | while (entries.hasMoreElements()) { |
| | | entry = entries.nextElement(); |
| | | |
| | | String entryName = entry.getName(); |
| | | |
| | | if (!entry.isDirectory()) { |
| | | //直接解析程序的json文件 |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | //设置md5值 |
| | | fileDataMap.forEach((k,v)->{ |
| | | if(fileMd5Map.containsKey(k)){ |
| | | v.setMd5(fileMd5Map.get(k)); |
| | | } |
| | | }); |
| | | return list; |
| | | } |
| | | */ |
| | | |
| | | public List<MdmProgramImportVO> readTempDir(Path extractDir) throws IOException { |
| | | List<MdmProgramImportVO> list = new ArrayList<>(); |
| | | |
| | | List<Machine> machines = machineService.lambdaQuery().eq(Machine::getStatus,Machine.STATUS_ENABLE).list(); |
| | | //List<Machine> machines = machineService.getEnableMachines(); |
| | | //读取所有文件夹 |
| | | //List<Path> dirs = Files.list(extractDir).filter(Files::isDirectory).toList(); |
| | | try (DirectoryStream<Path> stream = Files.newDirectoryStream(extractDir)) { |
| | | for (Path path : stream) { |
| | | if (Files.isDirectory(path)) { |
| | | |
| | | |
| | | |
| | | // 如果是子目录,读取其中的文件 |
| | | try (DirectoryStream<Path> subStream = Files.newDirectoryStream(path)) { |
| | | for (Path subPath : subStream) { |
| | | if (Files.isRegularFile(subPath)) { |
| | | System.out.println("找到文件: " + subPath); |
| | | //files.add(subPath.getFileName().toString()); |
| | | // 这里可以添加对文件的处理逻辑 |
| | | } |
| | | } |
| | | } |
| | | //vo.setFiles(files); |
| | | |
| | | } else if (Files.isRegularFile(path)) { |
| | | System.out.println("找到文件2: " + path); |
| | | //这里 找到的文件不是 |
| | |
| | | byte[] buffer = new byte[2000]; |
| | | inputStream.read(buffer); |
| | | vo.setMd5(DigestUtils.md5Hex(buffer)); |
| | | |
| | | |
| | | } catch (IOException e) { |
| | | log.error("读取文件md5失败",e); |
| | | } |
| | |
| | | try (InputStream inputStream = Files.newInputStream(path, StandardOpenOption.READ)) { |
| | | // 使用输入流读取文件内容 |
| | | ByteArrayInputStream bas = new ByteArrayInputStream(inputStream.readAllBytes()); |
| | | String line1 = FileContentUtil.readLineAt(bas,1); |
| | | |
| | | AnnotationProperties defAnnoProperties = AnnotationProperties.getDefault(); |
| | | String sendPathLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getSendPathLineIndex()); |
| | | //bas.mark(0); |
| | | bas.reset(); |
| | | String line2 = FileContentUtil.readLineAt(bas,2); |
| | | System.out.println("line1="+line1); |
| | | Machine matchedMachine = null; |
| | | String statusLine = FileContentUtil.readLineAt(bas,defAnnoProperties.getStatusLineIndex()); |
| | | log.info("sendPathLine={}", sendPathLine); |
| | | |
| | | Machine matchedMachine = machineService.getMachineBySendPathAnnotation(sendPathLine); |
| | | /* |
| | | for (Machine machine : machines) { |
| | | if(Func.isNotBlank(machine.getProgSendDir()) && line1.contains(machine.getProgSendDir())){ |
| | | if(Func.isNotBlank(machine.getProgSendDir()) && sendPathLine.contains(machine.getProgSendDir())){ |
| | | matchedMachine = machine; |
| | | break; |
| | | } |
| | | |
| | | } |
| | | |
| | | */ |
| | | if (matchedMachine != null) { |
| | | vo.setName(parseProgramName(vo.getFilename())); |
| | | vo.setMachineCode(matchedMachine.getCode()); |
| | |
| | | vo.setFullPath(path.toString());//文件地址 |
| | | vo.setSendPath(matchedMachine.getProgSendDir()); |
| | | vo.setId(vo.getFullPath()); |
| | | vo.setProgramStatus(programAnnotationService.removeAnnotation(matchedMachine.getMachineGroupCode(),line2)); |
| | | vo.setProgramStatus(programAnnotationService.removeAnnotation(matchedMachine.getControlSystem(),statusLine)); |
| | | |
| | | list.add(vo); |
| | | } |
| | |
| | | |
| | | List<String> idList = Func.toStrList(ids); |
| | | |
| | | String ditStr = bladeRedis.get(getFileKey()); |
| | | Path extractDir = Paths.get(ditStr); |
| | | String dictStr = bladeRedis.get(getFileKey()); |
| | | Path extractDir = Paths.get(dictStr); |
| | | |
| | | List<MdmProgramImportVO> list = readTempDir(extractDir); |
| | | |
| | | String destFileFull; |
| | | for(String str : idList){ |
| | | System.out.println(str); |
| | | //System.out.println(str); |
| | | for(MdmProgramImportVO vo : list){ |
| | | if(StringUtils.equals(vo.getFullPath(),str)){ |
| | | destFileFull = vo.getSendPath()+File.separator+vo.getFilename(); |
| | | File destFile = new File(destFileFull); |
| | | FileUtils.forceMkdirParent(destFile); |
| | | FileUtils.copyFile(new File(str),destFile); |
| | | |
| | | FileSendRecord record = new FileSendRecord(); |
| | | record.setName(destFile.getName()); |
| | | Path destPath = Paths.get(destFileFull); |
| | | record.setMachineCode(vo.getMachineCode()); |
| | | record.setFileSize(Files.size(destPath)); |
| | | fileSendRecordService.save(record); |
| | | break; |
| | | } |
| | | } |