| | |
| | | |
| | | package org.springblade.mdm.flow.service; |
| | | |
| | | import jakarta.servlet.ServletOutputStream; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import lombok.AllArgsConstructor; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.apache.commons.io.IOUtils; |
| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import java.io.ByteArrayInputStream; |
| | | import java.io.IOException; |
| | | import java.io.InputStream; |
| | | import java.nio.charset.StandardCharsets; |
| | |
| | | if(file.getSize() == 0){ |
| | | throw new ServiceException("程序文件不可为空文件"); |
| | | } |
| | | ProgramFileNameCheckUtil.checkFilename(file.getOriginalFilename(),progProps); |
| | | |
| | | String programName = NcNodeService.genProgramName(progProps.getDrawingNo(),progProps.getProcessNo()); |
| | | if(FlowProgramFile.TYPE_PROGRAM.equals(uploadVO.getFileType())) { |
| | | ProgramFileNameCheckUtil.checkFilename(file.getOriginalFilename(), progProps); |
| | | } |
| | | |
| | | BladeFile bfile = ossTemplate.putFile(file); |
| | | |
| | |
| | | progFile.setName(file.getOriginalFilename()); |
| | | progFile.setOssName(bfile.getName()); |
| | | progFile.setProcessInstanceId(uploadVO.getProcessInstanceId()); |
| | | progFile.setProgramName(programName); |
| | | progFile.setProgramName(NcNodeService.genProgramName(progProps.getDrawingNo(),progProps.getProcessNo())); |
| | | progFile.setFileType(uploadVO.getFileType()); |
| | | save(progFile); |
| | | |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 检查文件名合法性 |
| | | * @param filename 文件名 |
| | | * @param programProperties 程序属性,发起时填写的 |
| | | */ |
| | | /* |
| | | void checkFilename(String filename,FlowProgramProperties programProperties){ |
| | | //程序名称:零件号加工序号,文件名应该以此开头 |
| | | String programNamePrefix = NcNodeService.genProgramName(programProperties.getDrawingNo(),programProperties.getProcessNo()) + "-"+programProperties.getProcessEdition(); |
| | | if(!StringUtils.startsWith(filename,programNamePrefix)){ |
| | | |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序文件名不合法,应为:"+programNamePrefix+"-[段数]-[段号].[文件扩展名]"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 1; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | } |
| | | |
| | | //截取后面的段数和第几段 |
| | | String endPart = StringUtils.removeStart(filename,programNamePrefix+"-"); |
| | | |
| | | //去掉扩展名 |
| | | if(endPart.contains(".")){ |
| | | endPart = endPart.substring(0,endPart.indexOf(".")); |
| | | } |
| | | |
| | | int sepCount = StringUtils.countMatches(endPart,"-"); |
| | | if(sepCount != 1){//- 号应该是1个 |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序文件名不合法,应为:"+programNamePrefix+"-[段数]-[段号].[文件扩展名]"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 2; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | } |
| | | |
| | | String[] arr = StringUtils.split(endPart,"-"); |
| | | if(!StringUtils.isNumeric(arr[0]) || Func.toInt(arr[0]) >99 || Func.toInt(arr[0]) < 1){ |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序段数不合法,应为两位以内整数"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 3; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | } |
| | | int segCount = Func.toInt(arr[0]); |
| | | |
| | | if(!StringUtils.isNumeric(arr[1]) || Func.toInt(arr[1]) < 1 || Func.toInt(arr[1]) > segCount){ |
| | | IResultCode rc = new IResultCode() { |
| | | @Override |
| | | public String getMessage() { |
| | | return "程序段号不合法,应为两位以内整数且小于等于段数。"; |
| | | } |
| | | |
| | | @Override |
| | | public int getCode() { |
| | | return 4; |
| | | } |
| | | }; |
| | | throw new ServiceException(rc); |
| | | } |
| | | } |
| | | */ |
| | | /** |
| | | * 获取文件内容 |
| | | * @param id 文件id |
| | |
| | | String result =""; |
| | | |
| | | FlowProgramFile programFile = this.getById(id); |
| | | /* |
| | | String fileName = programFile.getOssName(); |
| | | try (InputStream inputStream = ossTemplate.statFileStream(fileName)) { |
| | | result = IOUtils.toString(inputStream, StandardCharsets.UTF_8); |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | | */ |
| | | |
| | | String fileName = programFile.getOssName(); |
| | | try (InputStream inputStream = ossTemplate.statFileStream(fileName)) { |
| | | result = FileContentUtil.getContentFromStream(inputStream); |
| | | ByteArrayInputStream bos = new ByteArrayInputStream(inputStream.readAllBytes()); |
| | | boolean isText = StringUtils.endsWithIgnoreCase(fileName,".txt") || StringUtils.endsWithIgnoreCase(fileName,".nc")|| StringUtils.endsWithIgnoreCase(fileName,".xml"); |
| | | if(!isText){ |
| | | isText= FileContentUtil.isTextFile(bos); |
| | | } |
| | | if(isText){ |
| | | bos.reset(); |
| | | result = FileContentUtil.getContentFromStream(bos); |
| | | }else{ |
| | | result = "<非文本文件>"; |
| | | } |
| | | } catch (IOException e) { |
| | | throw new RuntimeException(e); |
| | | } |
| | |
| | | */ |
| | | public void checkProgramFiles(String processInstanceId,boolean isPass) { |
| | | List<FlowProgramFile> flowPrograms = this.lambdaQuery().eq(FlowProgramFile::getProcessInstanceId, processInstanceId).orderByAsc(FlowProgramFile::getCreateTime).list(); |
| | | if(isPass && flowPrograms.isEmpty()){ |
| | | List<FlowProgramFile> purePrograms = flowPrograms.stream().filter(FlowProgramFile::isProgram).toList(); |
| | | if(isPass && purePrograms.isEmpty()){ |
| | | throw new ServiceException("请上传程序文件"); |
| | | } |
| | | int totalSeg = 0; |
| | | if(!flowPrograms.isEmpty()){ |
| | | FlowProgramFile progFile = flowPrograms.get(0); |
| | | |
| | | if(!purePrograms.isEmpty()){ |
| | | FlowProgramFile progFile = purePrograms.get(0); |
| | | totalSeg = ProgramFileNameCheckUtil.getProgramSegCount(progFile.getName()); |
| | | } |
| | | |
| | | if(totalSeg != flowPrograms.size()){ |
| | | if(totalSeg != purePrograms.size()){ |
| | | throw new ServiceException("应上传"+totalSeg+"段程序,实际上传"+flowPrograms.size()+"段"); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | public void download(Long id, HttpServletResponse response) throws IOException { |
| | | FlowProgramFile flowFile = this.getById(id); |
| | | response.setHeader("Content-Disposition", "attachment; filename="+flowFile.getName()); |
| | | response.setContentType("application/octet-stream"); |
| | | try(InputStream ins = ossTemplate.statFileStream(flowFile.getOssName());){ |
| | | IOUtils.copy(ins,response.getOutputStream()); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 获取关于流程的所有文件 |
| | | * @param processInstanceId 流程实例id |
| | | * @return 流程关联的文件列表 |
| | | */ |
| | | public List<FlowProgramFile> listByProcessInstanceId(String processInstanceId){ |
| | | return lambdaQuery() |
| | | .eq(FlowProgramFile::getProcessInstanceId, processInstanceId).list(); |
| | | } |
| | | } |