CadFilePreviewImpl.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package cn.keking.service.impl;
  2. import cn.keking.config.ConfigConstants;
  3. import cn.keking.model.FileAttribute;
  4. import cn.keking.model.ReturnResponse;
  5. import cn.keking.service.FilePreview;
  6. import cn.keking.utils.DownloadUtils;
  7. import cn.keking.service.FileHandlerService;
  8. import cn.keking.utils.KkFileUtils;
  9. import cn.keking.web.filter.BaseUrlFilter;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.ui.Model;
  12. import org.springframework.util.StringUtils;
  13. import java.util.List;
  14. import static cn.keking.service.impl.OfficeFilePreviewImpl.getPreviewType;
  15. /**
  16. * @author chenjh
  17. * @since 2019/11/21 14:28
  18. */
  19. @Service
  20. public class CadFilePreviewImpl implements FilePreview {
  21. private static final String OFFICE_PREVIEW_TYPE_IMAGE = "image";
  22. private static final String OFFICE_PREVIEW_TYPE_ALL_IMAGES = "allImages";
  23. private static final String FILE_DIR = ConfigConstants.getFileDir();
  24. private final FileHandlerService fileHandlerService;
  25. private final OtherFilePreviewImpl otherFilePreview;
  26. public CadFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
  27. this.fileHandlerService = fileHandlerService;
  28. this.otherFilePreview = otherFilePreview;
  29. }
  30. @Override
  31. public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
  32. // 预览Type,参数传了就取参数的,没传取系统默认
  33. String officePreviewType = fileAttribute.getOfficePreviewType() == null ? ConfigConstants.getOfficePreviewType() : fileAttribute.getOfficePreviewType();
  34. String baseUrl = BaseUrlFilter.getBaseUrl();
  35. boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
  36. String fileName = fileAttribute.getName();
  37. String suffix = fileAttribute.getSuffix();
  38. String pdfName = fileName.substring(0, fileName.lastIndexOf(".")) + suffix +"." + "pdf" ; //生成文件添加类型后缀 防止同名文件
  39. String outFilePath = FILE_DIR + pdfName;
  40. // 判断之前是否已转换过,如果转换过,直接返回,否则执行转换
  41. if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
  42. String filePath;
  43. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, null);
  44. if (response.isFailure()) {
  45. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  46. }
  47. filePath = response.getContent();
  48. String imageUrls = null;
  49. if (StringUtils.hasText(outFilePath)) {
  50. try {
  51. imageUrls = fileHandlerService.cadToPdf(filePath, outFilePath);
  52. } catch (Exception e) {
  53. e.printStackTrace();
  54. }
  55. if (imageUrls == null ) {
  56. return otherFilePreview.notSupportedFile(model, fileAttribute, "office转图片异常,请联系管理员");
  57. }
  58. //是否保留CAD源文件
  59. if( ConfigConstants.getDeleteSourceFile()) {
  60. KkFileUtils.deleteFileByPath(filePath);
  61. }
  62. if (ConfigConstants.isCacheEnabled()) {
  63. // 加入缓存
  64. fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
  65. }
  66. }
  67. }
  68. if (baseUrl != null && (OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType))) {
  69. return getPreviewType(model, fileAttribute, officePreviewType, baseUrl, pdfName, outFilePath, fileHandlerService, OFFICE_PREVIEW_TYPE_IMAGE,otherFilePreview);
  70. }
  71. model.addAttribute("pdfUrl", pdfName);
  72. return PDF_FILE_PREVIEW_PAGE;
  73. }
  74. }