package org.jeecg.modules.doc.service.impl; import cn.hutool.core.util.IdUtil; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import org.apache.commons.codec.digest.DigestUtils; import org.apache.commons.io.FileUtils; import org.apache.commons.io.FilenameUtils; import org.apache.commons.io.IOUtils; import org.jeecg.common.util.DateUtils; import org.jeecg.modules.doc.component.*; import org.jeecg.modules.doc.constant.Constant; import org.jeecg.modules.doc.entity.DocFile; import org.jeecg.modules.doc.entity.DocFilePath; import org.jeecg.modules.doc.mapper.DocFileMapper; import org.jeecg.modules.doc.mapper.DocFilePathMapper; import org.jeecg.modules.doc.service.IDocFileService; import org.jeecg.modules.doc.vo.CopyFile; import org.jeecg.modules.doc.vo.DownloadFile; import org.jeecg.modules.doc.exception.QiwenException; import org.jeecg.modules.doc.util.UFOPUtils; import org.jeecg.modules.doc.vo.QiwenFile; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import org.springframework.util.FileCopyUtils; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; import java.io.*; import java.util.*; /** * @Description: doc_file * @Author: jeecg-boot * @Date: 2022-07-04 * @Version: V1.0 */ @Service public class DocFileServiceImpl extends ServiceImpl implements IDocFileService { @Resource DocFilePathMapper docFilePathMapper; @Resource AsyncTaskComp asyncTaskComp; @Autowired DocFileDealComp fileDealComp; /** * 上传文件到目的根路径,先计算文件MD5值,查询已上传过同一文件,有相同文件返回文件id,否则上传新文件并返回id * * @param f 待上传文件 * @param identifier * @return */ @Override public DocFile saveFile(MultipartFile f, String identifier) throws IOException { String dfId; DocFile docFile = new DocFile(); dfId = IdUtil.getSnowflakeNextIdStr(); docFile.setFileId(dfId); String date = DateUtils.getDate("yyyyMMdd"); File savepath = new File(UFOPUtils.getStaticPath() + date); if (!savepath.exists()) { savepath.mkdirs();// 创建文件根目录 } int index = f.getOriginalFilename().lastIndexOf("."); String extend = index == -1?"":f.getOriginalFilename().substring(index+1); String fileUrl = UFOPUtils.getUploadFileUrl(identifier,extend); File saveFile = new File(UFOPUtils.getStaticPath() + fileUrl); try { f.transferTo(saveFile); } catch (IOException e) { throw new RuntimeException(e); } docFile.setFileUrl(fileUrl); docFile.setFileStatus(1); docFile.setStorageType(0); docFile.setFileSize(f.getSize()); docFile.setIdentifier(identifier); baseMapper.insert(docFile); // } return docFile; } @Override public DocFile saveFile(File f, String identifier) throws IOException { String dfId = null; String fileUrl = null; DocFile docFile = new DocFile(); List fileList = baseMapper.selectList(new LambdaQueryWrapper().eq(DocFile::getIdentifier,identifier)); if (fileList!=null && fileList.size()>0) { docFile = fileList.get(0); fileUrl = docFile.getFileUrl(); } else { dfId = IdUtil.getSnowflakeNextIdStr(); docFile.setFileId(dfId); String date = DateUtils.getDate("yyyyMMdd"); File savepath = new File(UFOPUtils.getStaticPath() + "/研发技术文档/" + date); if (!savepath.exists()) { savepath.mkdirs();// 创建文件根目录 } int index = f.getName().lastIndexOf("."); String extend = index == -1?"":f.getName().substring(index+1); fileUrl = UFOPUtils.getUploadFileUrl(identifier,extend); } String saveUrl = fileUrl.replace("研发技术文档", "//192.168.0.9/研发技术文档"); File saveFile = new File(saveUrl); try { FileCopyUtils.copy(f, saveFile); } catch (IOException e) { throw new RuntimeException(e); } docFile.setFileUrl(fileUrl.replace("研发技术文档", "upload")); docFile.setFileStatus(1); docFile.setStorageType(0); docFile.setFileSize(f.length()); docFile.setIdentifier(identifier); if (dfId != null) { baseMapper.insert(docFile); } else if (docFile.getFileId() != null) { baseMapper.update(null, new LambdaUpdateWrapper().set(DocFile::getFileStatus, 1).eq(DocFile::getFileId, docFile.getFileId())); } // } return docFile; } @Override public Long getFilePointCount(String fileId) { LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); lambdaQueryWrapper.eq(DocFilePath::getFileId, fileId); long count = docFilePathMapper.selectCount(lambdaQueryWrapper); return count; } @Override public void updateFileDetail(String userFileId, String identifier, long fileSize, String modifyUserId) { DocFilePath userFile = docFilePathMapper.selectById(userFileId); DocFile fileBean = new DocFile(); fileBean.setIdentifier(identifier); fileBean.setFileSize(fileSize); fileBean.setUpdateTime(DateUtils.getDate()); fileBean.setUpdateBy(modifyUserId); fileBean.setFileId(userFile.getFileId()); baseMapper.updateById(fileBean); userFile.setCreateTime(DateUtils.getDate()); docFilePathMapper.updateById(userFile); } /** * 解压文件 * @param userFileId * @param unzipMode * @param filePath */ @Override public void unzipFile(String userFileId, int unzipMode, String filePath) { DocFilePath userFile = docFilePathMapper.selectById(userFileId); DocFile fileBean = baseMapper.selectById(userFile.getFileId()); File destFile = new File(UFOPUtils.getStaticPath() + "temp" + File.separator + fileBean.getFileUrl()); Downloader downloader = new LocalStorageDownloader(); DownloadFile downloadFile = new DownloadFile(); downloadFile.setFileUrl(fileBean.getFileUrl()); InputStream inputStream = downloader.getInputStream(downloadFile); try { FileUtils.copyInputStreamToFile(inputStream, destFile); } catch (IOException e) { e.printStackTrace(); } String extendName = userFile.getExtendName(); String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + extendName, ""); List fileEntryNameList = new ArrayList<>(); try { fileEntryNameList = FileOperation.unzip(destFile, unzipUrl); } catch (Exception e) { e.printStackTrace(); log.error("解压失败" + e); throw new QiwenException(500001, "解压异常:" + e.getMessage()); } if (destFile.exists()) { destFile.delete(); } if (!fileEntryNameList.isEmpty() && unzipMode == 1) { DocFilePath parent = fileDealComp.getParent(userFile.getFilePath()); DocFilePath qiwenDir = PathFileUtil.getDir( userFile.getFilePath(), userFile.getFileName(), parent); docFilePathMapper.insert(qiwenDir); } fileEntryNameList.sort((a,b)->{ return a.length() - b.length(); }); for (int i = 0; i < fileEntryNameList.size(); i++){ String entryName = fileEntryNameList.get(i); //asyncTaskComp.saveUnzipFile(userFile, fileBean, unzipMode, entryName, filePath); this.saveUnzipFile(userFile, fileBean, unzipMode, entryName, filePath); } } void saveUnzipFile(DocFilePath userFile, DocFile fileBean, int unzipMode, String entryName, String filePath) { String unzipUrl = UFOPUtils.getTempFile(fileBean.getFileUrl()).getAbsolutePath().replace("." + userFile.getExtendName(), ""); String totalFileUrl = unzipUrl + entryName; File currentFile = new File(totalFileUrl); String fileId = null; if (!currentFile.isDirectory()) { FileInputStream fis = null; String md5Str = UUID.randomUUID().toString(); try { fis = new FileInputStream(currentFile); md5Str = DigestUtils.md5Hex(fis); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fis); } FileInputStream fileInputStream = null; try { Map param = new HashMap<>(); param.put("identifier", md5Str); List list = baseMapper.selectByMap(param); if (list != null && !list.isEmpty()) { //文件已存在 fileId = list.get(0).getFileId(); } else { //文件不存在 fileInputStream = new FileInputStream(currentFile); CopyFile createFile = new CopyFile(); System.out.println("totalFileUrl::" + totalFileUrl); createFile.setExtendName(FilenameUtils.getExtension(totalFileUrl)); String saveFileUrl =new LocalStorageCopier().copy(fileInputStream, createFile); DocFile tempFileBean = new DocFile(saveFileUrl, currentFile.length(), md5Str, userFile.getUpdateBy()); ; baseMapper.insert(tempFileBean); fileId = tempFileBean.getFileId(); } } catch (IOException e) { e.printStackTrace(); } finally { IOUtils.closeQuietly(fileInputStream); System.gc(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } currentFile.delete(); } } QiwenFile qiwenFile = null; if (unzipMode == 0) { qiwenFile = new QiwenFile(userFile.getFilePath(), entryName, currentFile.isDirectory()); } else if (unzipMode == 1) { qiwenFile = new QiwenFile(userFile.getFilePath() + "/" + userFile.getFileName(), entryName, currentFile.isDirectory()); } else if (unzipMode == 2) { qiwenFile = new QiwenFile(filePath, entryName, currentFile.isDirectory()); } System.out.println("qiwenFile:::" + qiwenFile.getPath()); DocFilePath saveUserFile = new DocFilePath(qiwenFile, userFile.getUpdateBy(), fileId); System.out.println("saveUserFile:::" + saveUserFile.toString()); DocFilePath parent = fileDealComp.getParent(saveUserFile.getFilePath()); if (parent != null) { System.out.println("parent::"+parent.toString()); saveUserFile.setParentId(parent.getPathId()); } String fileName = fileDealComp.getRepeatFileName(saveUserFile, saveUserFile.getFilePath()); saveUserFile.setFileName(fileName); if (saveUserFile.getIsDir() == Constant.IS_DIR && !fileName.equals(saveUserFile.getFileName())) { //如果是目录,而且重复,什么也不做 } else { System.out.println(saveUserFile.toString()); docFilePathMapper.insert(saveUserFile); } } }