TiffFilePreviewImpl.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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.FileHandlerService;
  6. import cn.keking.service.FilePreview;
  7. import cn.keking.utils.ConvertPicUtil;
  8. import cn.keking.utils.DownloadUtils;
  9. import cn.keking.utils.KkFileUtils;
  10. import org.springframework.stereotype.Service;
  11. import org.springframework.ui.Model;
  12. import org.springframework.util.ObjectUtils;
  13. import java.io.IOException;
  14. import java.util.List;
  15. /**
  16. * tiff 图片文件处理
  17. *
  18. * @author kl (http://kailing.pub)
  19. * @since 2021/2/8
  20. */
  21. @Service
  22. public class TiffFilePreviewImpl implements FilePreview {
  23. private final FileHandlerService fileHandlerService;
  24. private final OtherFilePreviewImpl otherFilePreview;
  25. public TiffFilePreviewImpl(FileHandlerService fileHandlerService,OtherFilePreviewImpl otherFilePreview) {
  26. this.fileHandlerService = fileHandlerService;
  27. this.otherFilePreview = otherFilePreview;
  28. }
  29. @Override
  30. public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
  31. String fileName = fileAttribute.getName();
  32. String tifPreviewType = ConfigConstants.getTifPreviewType();
  33. String cacheName = fileAttribute.getCacheName();
  34. String outFilePath = fileAttribute.getOutFilePath();
  35. String fileKey = fileAttribute.getFileKey(); //判断是否压缩包
  36. boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
  37. if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
  38. if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(cacheName) || !ConfigConstants.isCacheEnabled()) {
  39. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
  40. if (response.isFailure()) {
  41. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  42. }
  43. String filePath = response.getContent();
  44. if ("pdf".equalsIgnoreCase(tifPreviewType)) {
  45. try {
  46. ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath);
  47. } catch (Exception e) {
  48. if (e.getMessage().contains("Bad endianness tag (not 0x4949 or 0x4d4d)") ) {
  49. model.addAttribute("imgUrls", url);
  50. model.addAttribute("currentUrl", url);
  51. return PICTURE_FILE_PREVIEW_PAGE;
  52. }else {
  53. return otherFilePreview.notSupportedFile(model, fileAttribute, "TIF转pdf异常,请联系系统管理员!" );
  54. }
  55. }
  56. //是否保留TIFF源文件
  57. if (ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
  58. // KkFileUtils.deleteFileByPath(filePath);
  59. }
  60. if (ConfigConstants.isCacheEnabled()) {
  61. // 加入缓存
  62. fileHandlerService.addConvertedFile(cacheName, fileHandlerService.getRelativePath(outFilePath));
  63. }
  64. model.addAttribute("pdfUrl", cacheName);
  65. return PDF_FILE_PREVIEW_PAGE;
  66. }else {
  67. // 将tif转换为jpg,返回转换后的文件路径、文件名的list
  68. List<String> listPic2Jpg;
  69. try {
  70. listPic2Jpg = ConvertPicUtil.convertTif2Jpg(filePath, outFilePath,forceUpdatedCache);
  71. } catch (Exception e) {
  72. if (e.getMessage().contains("Bad endianness tag (not 0x4949 or 0x4d4d)") ) {
  73. model.addAttribute("imgUrls", url);
  74. model.addAttribute("currentUrl", url);
  75. return PICTURE_FILE_PREVIEW_PAGE;
  76. }else {
  77. return otherFilePreview.notSupportedFile(model, fileAttribute, "TIF转JPG异常,请联系系统管理员!" );
  78. }
  79. }
  80. //是否保留源文件,转换失败保留源文件,转换成功删除源文件
  81. if(ObjectUtils.isEmpty(fileKey) && ConfigConstants.getDeleteSourceFile()) {
  82. KkFileUtils.deleteFileByPath(filePath);
  83. }
  84. if (ConfigConstants.isCacheEnabled()) {
  85. // 加入缓存
  86. fileHandlerService.putImgCache(cacheName, listPic2Jpg);
  87. fileHandlerService.addConvertedFile(cacheName, fileHandlerService.getRelativePath(outFilePath));
  88. }
  89. model.addAttribute("imgUrls", listPic2Jpg);
  90. model.addAttribute("currentUrl", listPic2Jpg.get(0));
  91. return PICTURE_FILE_PREVIEW_PAGE;
  92. }
  93. }
  94. if ("pdf".equalsIgnoreCase(tifPreviewType)) {
  95. model.addAttribute("pdfUrl", fileHandlerService.listConvertedFiles().get(cacheName));
  96. return PDF_FILE_PREVIEW_PAGE;
  97. }
  98. else if ("jpg".equalsIgnoreCase(tifPreviewType)) {
  99. model.addAttribute("imgUrls", fileHandlerService.getImgCache(cacheName));
  100. model.addAttribute("currentUrl", fileHandlerService.getImgCache(cacheName).get(0));
  101. return PICTURE_FILE_PREVIEW_PAGE;
  102. }
  103. }
  104. // 不是http开头,浏览器不能直接访问,需下载到本地
  105. if (url != null && !url.toLowerCase().startsWith("http")) {
  106. if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(fileName) || !ConfigConstants.isCacheEnabled()) {
  107. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
  108. if (response.isFailure()) {
  109. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  110. }
  111. model.addAttribute("currentUrl", fileHandlerService.getRelativePath(response.getContent()));
  112. if (ConfigConstants.isCacheEnabled()) {
  113. // 加入缓存
  114. fileHandlerService.addConvertedFile(fileName, fileHandlerService.getRelativePath(outFilePath));
  115. }
  116. } else {
  117. model.addAttribute("currentUrl", fileName);
  118. }
  119. return TIFF_FILE_PREVIEW_PAGE;
  120. }
  121. model.addAttribute("currentUrl", url);
  122. return TIFF_FILE_PREVIEW_PAGE;
  123. }
  124. }