package org.jeecg.modules.doc.service.impl; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.jeecg.common.util.MD5Util; import org.jeecg.modules.doc.component.PathFileUtil; import org.jeecg.modules.doc.component.UfopConfig; import org.jeecg.modules.doc.constant.Constant; import org.jeecg.modules.doc.dto.UploadFileDTO; import org.jeecg.modules.doc.entity.DocFile; import org.jeecg.modules.doc.entity.DocFilePath; import org.jeecg.modules.doc.entity.LbDocEntity; import org.jeecg.modules.doc.mapper.LbDocMapper; import org.jeecg.modules.doc.service.IDocFilePathService; import org.jeecg.modules.doc.service.IDocFileService; import org.jeecg.modules.doc.service.ILbDocService; import org.jeecg.modules.doc.util.MD5; import org.jeecg.modules.doc.util.UFOPUtils; import org.jeecg.modules.doc.vo.CompareFileVo; import org.jeecg.modules.doc.vo.DocTree; import org.jeecg.modules.doc.vo.QiwenFile; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import java.io.*; import java.text.SimpleDateFormat; import java.util.*; /** * 测试Service */ @Service public class LbDocServiceImpl extends ServiceImpl implements ILbDocService { @Override public String hello() { return "hello jeecg!"; } @Autowired private UfopConfig ufopConfig; @Autowired private IDocFilePathService filePathService; @Autowired private IDocFileService docFileService; @Autowired private RestTemplate restTemplate; @Override public DocTree genDocTree(DocTree docTree) { int level = Integer.valueOf(docTree.getKey().substring(0, docTree.getKey().indexOf("-"))); level++; File file = new File(docTree.getPath()); if (!file.exists()) { System.err.println("文件不存在!!"); } docTree.setTitle(file.getName()); File[] files = file.listFiles(); if (files.length != 0) { List docs = new ArrayList<>(); DocTree dt = null; for (int i = 0; i < files.length; i++) { File f = files[i]; dt = new DocTree(); dt.setKey(level + "-" + i); dt.setTitle(f.getName()); dt.setPath(f.getPath()); if (f.isDirectory()) { // genDocTree(f.getAbsolutePath(), level,dt); } else { dt.setLeaf(true); } docs.add(dt); } docTree.setChildren(docs); } return docTree; } public void initDocSize(Map map, String releasePath, DocFilePath parent) { // System.out.println("文件:" + fsize +" 文件夹:" + dsize); File file = new File(releasePath); if (!file.exists()) { System.err.println("文件不存在!!"); } File[] files = file.listFiles(); DocFilePath filePath = null; if (files != null && files.length != 0) { for (int i = 0; i < files.length; i++) { File f = files[i]; if (!"#recycle".equals(f.getName()) && !"$RECYCLE.BIN".equals(f.getName()) && !"System Volume Information".equals(f.getName())) { if (f.isDirectory()) { map.put("dsize", map.get("dsize")+1); initDocSize(map,f.getPath(), filePath); } else { map.put("fsize", map.get("fsize")+1); } } } } } public void initDir(Map paths, List filePathList, String releasePath, DocFilePath parent) { File file = new File(releasePath); if (!file.exists()) { System.err.println("文件不存在!!"); } File[] files = file.listFiles(); DocFilePath filePath = null; // System.out.println("当前文件夹:" + file.getPath()); if (files != null && files.length != 0) { for (int i = 0; i < files.length; i++) { File f = files[i]; // System.out.println(f.getPath()); // System.out.println("r\\/"+f.getPath().replace("\\","/")); // System.out.println("ddd" + f.getPath().replace("\\","/").replace(ufopConfig.getReleasePath(),"/07 开发三部")); String filepath = f.getPath().replace("\\","/").replace(ufopConfig.getReleasePath(),""); //String filepath = QiwenFile.separator+QiwenFile.formatPath(f.getPath().replace("\\","/")).replace(ufopConfig.getReleasePath(),"/07 开发三部"); filepath = QiwenFile.separator+(filepath.substring(0,filepath.lastIndexOf("/"))); if (!"#recycle".equals(f.getName()) && !"$RECYCLE.BIN".equals(f.getName()) && !"System Volume Information".equals(f.getName())) { if (f.isDirectory()) { //System.out.println("文件:" + f.getName()); filePath = PathFileUtil.getDir(filepath,f.getName(), parent); filePath.setIsDir(Constant.IS_DIR); String s = paths.get(filePath.getFilePath() + filePath.getFileName() + (filePath.getExtendName()==null?"":filePath.getExtendName()) + filePath.getIsDir()); if (s == null) { filePathList.add(filePath); } else { filePath.setPathId(s); } initDir(paths, filePathList,f.getPath(), filePath); // if (s != null) { // continue; // } } else { continue; } } } } } @Override public void initDoc(Map paths, String releasePath, DocFilePath parent) { File file = new File(releasePath); if (!file.exists()) { System.err.println("文件不存在!!"); } File[] files = file.listFiles(); DocFilePath filePath = null; DocFile docFile = null; List filePathList = new ArrayList<>(); if (files != null && files.length != 0) { for (int i = 0; i < files.length; i++) { File f = files[i]; String filepath = QiwenFile.separator+QiwenFile.formatPath(f.getPath().replace("\\","/")).replaceFirst(ufopConfig.getReleasePath(),""); filepath = QiwenFile.separator+(filepath.substring(0,filepath.lastIndexOf("/"))); if (!"#recycle".equals(f.getName()) && !"$RECYCLE.BIN".equals(f.getName()) && !"System Volume Information".equals(f.getName()) // && !"档案资料区".equals(f.getName()) && !"02 研发一部".equals(f.getName()) && !"03 研发二部".equals(f.getName()) && !"04 研发三部".equals(f.getName()) && !"05 开发一部".equals(f.getName()) && !"06 开发二部".equals(f.getName()) && !"07 开发三部".equals(f.getName()) && !"08 工业设计部".equals(f.getName()) && !"09 研发管理部".equals(f.getName()) && !"10 兰浦公司".equals(f.getName()) && !"01 公共区域".equals(f.getName()) ) { if (f.isDirectory()) { filePath = PathFileUtil.getDir(filepath,f.getName(), parent); filePath.setIsDir(Constant.IS_DIR); initDoc(paths, f.getPath(), filePath); } else { int lastIndex = f.getName().lastIndexOf("."); filePath = PathFileUtil.getDir(filepath,f.getName().substring(0,lastIndex>0?lastIndex:0), parent); //System.out.println("同步文件:" + filePath.getFileName()); filePath.setIsDir(Constant.IS_NOT_DIR); filePath.setExtendName(f.getName().substring(f.getName().lastIndexOf(".") + 1)); String key = filePath.getFilePath().replace("/192.168.0.9/研发中心", "") + filePath.getFileName() + (filePath.getExtendName() == null ? "" : filePath.getExtendName()) + filePath.getIsDir(); String s = paths.get(key); if (s != null) { //System.out.println("该文件已存在"); docFile = new DocFile(); continue; // docFile.setFileId(s); } else { long length = f.length(); if (length > 1000000000l) { log.error("小文件:[" + f.getPath() +"]>>>["+f.length()+ "]"); continue; } // String dfId; String md5 = MD5.getMD5(f); // 查询是否已上传过 List fileList = docFileService.list(new LambdaQueryWrapper().eq(DocFile::getIdentifier,md5)); if (fileList!=null && fileList.size()>0) { docFile = new DocFile(); // dfId = fileList.get(0).getFileId(); docFile.setFileId(fileList.get(0).getFileId()); } else { // String url = UFOPUtils.ENCRYPT_PATH + "/fileTransfer/encryptUpload"; try { // 1、封装请求头 // HttpHeaders headers = new HttpHeaders(); // MediaType type = MediaType.parseMediaType("multipart/form-data"); // headers.setContentType(type); // headers.setContentLength(length); // headers.setContentDispositionFormData("media", f.getName()); // // 2、封装请求体 // MultiValueMap param = new LinkedMultiValueMap<>(); // FileSystemResource resource = new FileSystemResource(f); // param.add("file", resource); // param.add("identifier", md5); // // 3、封装整个请求报文 // HttpEntity> formEntity = new HttpEntity<>(param, headers); // // 4、发送请求 // ResponseEntity data = restTemplate.postForEntity(url, formEntity, DocFile.class); // // 5、请求结果处理 // docFile = data.getBody(); docFile = docFileService.saveFile(f, md5); } catch (Exception e) { log.error("异常文件:PATH=" + f.getPath() + "[md5]=" + md5); log.error(e.getMessage()); //e.printStackTrace(); continue; } } } filePath.setFileId(docFile.getFileId()); } String s = paths.get(filePath.getFilePath().replace("/192.168.0.9/研发中心","") + filePath.getFileName() + (filePath.getExtendName()==null?"":filePath.getExtendName()) + filePath.getIsDir()); if (s != null) { continue; } //filePath.setPathId(s); // try { // filePathService.saveOrUpdate(filePath); // }catch (Exception e) { // log.error("获取相同文件:::"+filePath.getFilePath() + filePath.getFileName() + filePath.getExtendName() + filePath.getIsDir()); // log.error("保存文件失败:" + filePath.getFilePath() + ">>" + filePath.getFileName() + ">>>" + filePath.getExtendName()); // log.error(e.getMessage()); // } filePathList.add(filePath); } } System.out.println("当前文件夹:" + file.getPath()); System.out.println("保存" + filePathList.size() +"个文件:" + System.currentTimeMillis()); try { filePathService.saveOrUpdateBatch(filePathList); } catch (Exception e) { log.error("保存失败"); log.error(e.getMessage()); } System.out.println("保存完成:" +System.currentTimeMillis()); } } private static void copyFileUsingStream(File source, File dest) throws IOException { InputStream is = null; OutputStream os = null; try { is = new FileInputStream(source); os = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = is.read(buffer)) > 0) { os.write(buffer, 0, length); } } finally { is.close(); os.close(); } } @Override public CompareFileVo compareFile(UploadFileDTO uploadFileDto) { CompareFileVo compareFileVo = new CompareFileVo(); compareFileVo.setFileName(uploadFileDto.getRelativePath()); compareFileVo.setIdentifier(uploadFileDto.getIdentifier()); String relativePath = uploadFileDto.getRelativePath().substring(uploadFileDto.getRelativePath().indexOf("/")); String filePath = (uploadFileDto.getFilePath() + relativePath).replace("/" + uploadFileDto.getFilename(), ""); int lastIndex = uploadFileDto.getFilename().lastIndexOf("."); String fileName = uploadFileDto.getFilename().substring(0, lastIndex > 0 ? lastIndex : 0); String extendName = uploadFileDto.getFilename().substring(lastIndex + 1); List list = filePathService.list(new LambdaQueryWrapper() .eq(DocFilePath::getFilePath, filePath) .eq(DocFilePath::getFileName, fileName) .eq(DocFilePath::getExtendName, extendName) .eq(DocFilePath::getDeleteFlag, 0) .eq(DocFilePath::getIsDir, 0) ); if (list != null && list.size() == 1) { DocFile byId = docFileService.getById(list.get(0).getFileId()); File file = new File(UFOPUtils.getStaticPath() + byId.getFileUrl()); String md5 = MD5.getMD5(file); compareFileVo.setComIdentifier(md5); compareFileVo.setComFileSize(file.length()); } return compareFileVo; } @Override public void checkFile(String filePath) { File file = new File(filePath); if (!file.exists()) { System.err.println("文件不存在!!"); } File[] files = file.listFiles(); if (files != null && files.length != 0) { for (int i = 0; i < files.length; i++) { File f = files[i]; if (f.isDirectory()) { System.out.println(f.getPath()); checkFile(f.getPath()); } else { try { // long l = f.lastModified(); // String dat = "2023-10-23"; // Date parse = new SimpleDateFormat("yyyy-MM-dd").parse(dat); // Date date = new Date(l); // if (date.after(parse)) { // continue; // } // String md5 = MD5.getMD5(f); // String fileName = ""; // int lastIndex = f.getName().lastIndexOf("."); // if (lastIndex <0) { //// fileName = f.getName().substring(0,f.getName().length()-1); // fileName = f.getName().replace("\uF029",""); // } else { // fileName = f.getName().substring(0, lastIndex); // } // if (!fileName.equals(md5)) { // // System.out.println("原MD5:" + fileName); // System.out.println("现MD5:" + md5); // docFileService.update(new LambdaUpdateWrapper().set(DocFile::getFileStatus, 0).eq(DocFile::getIdentifier, fileName)); // f.delete(); // } }catch (Exception e) { System.out.println("文件名:【" + f.getName()+"】"); e.printStackTrace(); } } System.out.println("完成!"); } } } }