OnlinePreviewController.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. package cn.keking.web.controller;
  2. import cn.keking.config.ConfigConstants;
  3. import cn.keking.model.FileAttribute;
  4. import cn.keking.service.FilePreview;
  5. import cn.keking.service.FilePreviewFactory;
  6. import cn.keking.service.cache.CacheService;
  7. import cn.keking.utils.DownloadUtils;
  8. import cn.keking.utils.FileUtils;
  9. import org.slf4j.Logger;
  10. import org.slf4j.LoggerFactory;
  11. import org.springframework.stereotype.Controller;
  12. import org.springframework.ui.Model;
  13. import org.springframework.web.bind.annotation.GetMapping;
  14. import org.springframework.web.bind.annotation.RequestMapping;
  15. import org.springframework.web.bind.annotation.RequestMethod;
  16. import org.springframework.web.bind.annotation.ResponseBody;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import java.io.*;
  20. import java.util.Arrays;
  21. import java.util.List;
  22. /**
  23. * @author yudian-it
  24. */
  25. @Controller
  26. public class OnlinePreviewController {
  27. private final Logger logger = LoggerFactory.getLogger(OnlinePreviewController.class);
  28. private final FilePreviewFactory previewFactory;
  29. private final CacheService cacheService;
  30. private final FileUtils fileUtils;
  31. private final DownloadUtils downloadUtils;
  32. public OnlinePreviewController(FilePreviewFactory filePreviewFactory,
  33. FileUtils fileUtils,
  34. CacheService cacheService,
  35. DownloadUtils downloadUtils) {
  36. this.previewFactory = filePreviewFactory;
  37. this.fileUtils = fileUtils;
  38. this.cacheService = cacheService;
  39. this.downloadUtils = downloadUtils;
  40. }
  41. @RequestMapping(value = "/onlinePreview", method = RequestMethod.GET)
  42. public String onlinePreview(String url, Model model, HttpServletRequest req) {
  43. FileAttribute fileAttribute = fileUtils.getFileAttribute(url);
  44. req.setAttribute("fileKey", req.getParameter("fileKey"));
  45. model.addAttribute("pdfDownloadDisable", ConfigConstants.getPdfDownloadDisable());
  46. model.addAttribute("officePreviewType", req.getParameter("officePreviewType"));
  47. FilePreview filePreview = previewFactory.get(fileAttribute);
  48. logger.info("预览文件url:{},previewType:{}", url, fileAttribute.getType());
  49. return filePreview.filePreviewHandle(url, model, fileAttribute);
  50. }
  51. @RequestMapping(value = "/picturesPreview")
  52. public String picturesPreview(Model model, HttpServletRequest req) {
  53. String urls = req.getParameter("urls");
  54. String currentUrl = req.getParameter("currentUrl");
  55. logger.info("预览文件url:{},urls:{}", currentUrl, urls);
  56. String[] imgs = urls.split("\\|");
  57. List<String> imgurls = Arrays.asList(imgs);
  58. model.addAttribute("imgurls", imgurls);
  59. model.addAttribute("currentUrl", currentUrl);
  60. return "picture";
  61. }
  62. /**
  63. * 根据url获取文件内容
  64. * 当pdfjs读取存在跨域问题的文件时将通过此接口读取
  65. *
  66. * @param urlPath url
  67. * @param response response
  68. */
  69. @RequestMapping(value = "/getCorsFile", method = RequestMethod.GET)
  70. public void getCorsFile(String urlPath, HttpServletResponse response) {
  71. logger.info("下载跨域pdf文件url:{}", urlPath);
  72. try {
  73. downloadUtils.saveToOutputStreamFromUrl(urlPath, response.getOutputStream());
  74. } catch (IOException e) {
  75. logger.error("下载跨域pdf文件异常,url:{}", urlPath, e);
  76. }
  77. }
  78. /**
  79. * 通过api接口入队
  80. * @param url 请编码后在入队
  81. */
  82. @GetMapping("/addTask")
  83. @ResponseBody
  84. public String addQueueTask(String url) {
  85. logger.info("添加转码队列url:{}", url);
  86. cacheService.addQueueTask(url);
  87. return "success";
  88. }
  89. }