PdfFilePreviewImpl.java 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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.DownloadUtils;
  8. import cn.keking.utils.WebUtils;
  9. import org.apache.commons.lang3.exception.ExceptionUtils;
  10. import org.apache.poi.EncryptedDocumentException;
  11. import org.springframework.stereotype.Service;
  12. import org.springframework.ui.Model;
  13. import java.io.IOException;
  14. import java.util.List;
  15. /**
  16. * Created by kl on 2018/1/17.
  17. * Content :处理pdf文件
  18. */
  19. @Service
  20. public class PdfFilePreviewImpl implements FilePreview {
  21. private final FileHandlerService fileHandlerService;
  22. private final OtherFilePreviewImpl otherFilePreview;
  23. private static final String PDF_PASSWORD_MSG = "password";
  24. public PdfFilePreviewImpl(FileHandlerService fileHandlerService, OtherFilePreviewImpl otherFilePreview) {
  25. this.fileHandlerService = fileHandlerService;
  26. this.otherFilePreview = otherFilePreview;
  27. }
  28. @Override
  29. public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
  30. String pdfName = fileAttribute.getName(); //获取原始文件名
  31. String officePreviewType = fileAttribute.getOfficePreviewType(); //转换类型
  32. boolean forceUpdatedCache=fileAttribute.forceUpdatedCache(); //是否启用强制更新命令
  33. String outFilePath = fileAttribute.getOutFilePath(); //生成的文件路径
  34. String originFilePath = fileAttribute.getOriginFilePath(); //原始文件路径
  35. if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType) || OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_ALL_IMAGES.equals(officePreviewType)) {
  36. //当文件不存在时,就去下载
  37. if (forceUpdatedCache || !fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
  38. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
  39. if (response.isFailure()) {
  40. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  41. }
  42. originFilePath = response.getContent();
  43. if (ConfigConstants.isCacheEnabled()) {
  44. // 加入缓存
  45. fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(originFilePath));
  46. }
  47. }
  48. List<String> imageUrls;
  49. try {
  50. imageUrls = fileHandlerService.pdf2jpg(originFilePath,outFilePath, pdfName, fileAttribute);
  51. } catch (Exception e) {
  52. Throwable[] throwableArray = ExceptionUtils.getThrowables(e);
  53. for (Throwable throwable : throwableArray) {
  54. if (throwable instanceof IOException || throwable instanceof EncryptedDocumentException) {
  55. if (e.getMessage().toLowerCase().contains(PDF_PASSWORD_MSG)) {
  56. model.addAttribute("needFilePassword", true);
  57. return EXEL_FILE_PREVIEW_PAGE;
  58. }
  59. }
  60. }
  61. return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
  62. }
  63. if (imageUrls == null || imageUrls.size() < 1) {
  64. return otherFilePreview.notSupportedFile(model, fileAttribute, "pdf转图片异常,请联系管理员");
  65. }
  66. model.addAttribute("imgUrls", imageUrls);
  67. model.addAttribute("currentUrl", imageUrls.get(0));
  68. if (OfficeFilePreviewImpl.OFFICE_PREVIEW_TYPE_IMAGE.equals(officePreviewType)) {
  69. return OFFICE_PICTURE_FILE_PREVIEW_PAGE;
  70. } else {
  71. return PICTURE_FILE_PREVIEW_PAGE;
  72. }
  73. } else {
  74. // 不是http开头,浏览器不能直接访问,需下载到本地
  75. if (url != null && !url.toLowerCase().startsWith("http")) {
  76. if (!fileHandlerService.listConvertedFiles().containsKey(pdfName) || !ConfigConstants.isCacheEnabled()) {
  77. ReturnResponse<String> response = DownloadUtils.downLoad(fileAttribute, pdfName);
  78. if (response.isFailure()) {
  79. return otherFilePreview.notSupportedFile(model, fileAttribute, response.getMsg());
  80. }
  81. model.addAttribute("pdfUrl", fileHandlerService.getRelativePath(response.getContent()));
  82. if (ConfigConstants.isCacheEnabled()) {
  83. // 加入缓存
  84. fileHandlerService.addConvertedFile(pdfName, fileHandlerService.getRelativePath(outFilePath));
  85. }
  86. } else {
  87. model.addAttribute("pdfUrl", WebUtils.encodeFileName(pdfName));
  88. }
  89. } else {
  90. model.addAttribute("pdfUrl", url);
  91. }
  92. }
  93. return PDF_FILE_PREVIEW_PAGE;
  94. }
  95. }