TiffFilePreviewImpl.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 cn.keking.web.filter.BaseUrlFilter;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.ui.Model;
  13. import org.springframework.util.StringUtils;
  14. import java.io.File;
  15. import java.util.ArrayList;
  16. import java.util.List;
  17. /**
  18. * tiff 图片文件处理
  19. *
  20. * @author kl (http://kailing.pub)
  21. * @since 2021/2/8
  22. */
  23. @Service
  24. public class TiffFilePreviewImpl implements FilePreview {
  25. private final FileHandlerService fileHandlerService;
  26. private final OtherFilePreviewImpl otherFilePreview;
  27. public TiffFilePreviewImpl(FileHandlerService fileHandlerService,OtherFilePreviewImpl otherFilePreview) {
  28. this.fileHandlerService = fileHandlerService;
  29. this.otherFilePreview = otherFilePreview;
  30. }
  31. private final String fileDir = ConfigConstants.getFileDir();
  32. @Override
  33. public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
  34. String fileName = fileAttribute.getName();
  35. String baseUrl = BaseUrlFilter.getBaseUrl();
  36. String tifPreviewType = ConfigConstants.getTifPreviewType();
  37. String tifOnLinePreviewType = fileAttribute.getTifPreviewType();
  38. boolean forceUpdatedCache=fileAttribute.forceUpdatedCache();
  39. if (StringUtils.hasText(tifOnLinePreviewType)) {
  40. tifPreviewType = tifOnLinePreviewType;
  41. }
  42. if ("tif".equalsIgnoreCase(tifPreviewType)) {
  43. model.addAttribute("currentUrl", url);
  44. return TIFF_FILE_PREVIEW_PAGE;
  45. } else if ("jpg".equalsIgnoreCase(tifPreviewType) || "pdf".equalsIgnoreCase(tifPreviewType)) {
  46. String pdfName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "pdf";
  47. String jpgName = fileName.substring(0, fileName.lastIndexOf(".") + 1) + "jpg";
  48. String strLocalTif = fileDir + fileName;
  49. String outFilePath = fileDir + pdfName;
  50. if ("pdf".equalsIgnoreCase(tifPreviewType)) {
  51. //当文件不存在时,就去下载
  52. if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
  53. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
  54. if (response.isFailure()) {
  55. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  56. }
  57. String filePath = response.getContent();
  58. if(ConvertPicUtil.convertJpg2Pdf(filePath, outFilePath)){
  59. if(ConfigConstants.getdeletesourcefile()){ //是否保留TIFF源文件
  60. KkFileUtils.deleteFileByPath(filePath);
  61. }
  62. if (ConfigConstants.isCacheEnabled()) {
  63. // 加入缓存
  64. fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
  65. }
  66. model.addAttribute("pdfUrl", pdfName);
  67. return PDF_FILE_PREVIEW_PAGE;
  68. }else {
  69. return NOT_SUPPORTED_FILE_PAGE;
  70. }
  71. }
  72. else {
  73. model.addAttribute("pdfUrl", pdfName);
  74. return PDF_FILE_PREVIEW_PAGE;
  75. }
  76. } else {
  77. File fileTiff = new File(strLocalTif);
  78. // 如果本地不存在这个tif文件,则下载
  79. if (!fileTiff.exists()) {
  80. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, fileName);
  81. if (response.isFailure()) {
  82. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  83. }
  84. strLocalTif = response.getContent();
  85. }
  86. // 以JPG模式预览的过程
  87. String strJpgFilePathName = fileDir + jpgName;
  88. // 将tif转换为jpg,返回转换后的文件路径、文件名的list
  89. List<String> listPic2Jpg = ConvertPicUtil.convertTif2Jpg(strLocalTif, strJpgFilePathName);
  90. // 将返回页面的图片url的list对象
  91. List<String> listImageUrls = new ArrayList<>();
  92. // 循环,拼装url的list对象
  93. for (String strJpg : listPic2Jpg) {
  94. listImageUrls.add(baseUrl + strJpg);
  95. }
  96. model.addAttribute("imgUrls", listImageUrls);
  97. model.addAttribute("currentUrl", listImageUrls.get(0));
  98. }
  99. return PICTURE_FILE_PREVIEW_PAGE;
  100. }
  101. return NOT_SUPPORTED_FILE_PAGE;
  102. }
  103. }