PictureFilePreviewImpl.java 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package cn.keking.service.impl;
  2. import cn.keking.model.FileAttribute;
  3. import cn.keking.model.ReturnResponse;
  4. import cn.keking.service.FilePreview;
  5. import cn.keking.utils.DownloadUtils;
  6. import cn.keking.service.FilePreviewCommonService;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.ui.Model;
  9. import org.springframework.util.CollectionUtils;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * Created by kl on 2018/1/17.
  14. * Content :图片文件处理
  15. */
  16. @Service
  17. public class PictureFilePreviewImpl implements FilePreview {
  18. private final FilePreviewCommonService filePreviewCommonService;
  19. private final DownloadUtils downloadUtils;
  20. public PictureFilePreviewImpl(FilePreviewCommonService filePreviewCommonService,
  21. DownloadUtils downloadUtils) {
  22. this.filePreviewCommonService = filePreviewCommonService;
  23. this.downloadUtils = downloadUtils;
  24. }
  25. @Override
  26. public String filePreviewHandle(String url, Model model, FileAttribute fileAttribute) {
  27. List<String> imgUrls = new ArrayList<>();
  28. imgUrls.add(url);
  29. String fileKey = fileAttribute.getFileKey();
  30. List<String> zipImgUrls = filePreviewCommonService.getImgCache(fileKey);
  31. if (!CollectionUtils.isEmpty(zipImgUrls)) {
  32. imgUrls.addAll(zipImgUrls);
  33. }
  34. // 不是http开头,浏览器不能直接访问,需下载到本地
  35. if (url != null && !url.toLowerCase().startsWith("http")) {
  36. ReturnResponse<String> response = downloadUtils.downLoad(fileAttribute, null);
  37. if (0 != response.getCode()) {
  38. model.addAttribute("fileType", fileAttribute.getSuffix());
  39. model.addAttribute("msg", response.getMsg());
  40. return "fileNotSupported";
  41. } else {
  42. String file = filePreviewCommonService.getRelativePath(response.getContent());
  43. imgUrls.clear();
  44. imgUrls.add(file);
  45. model.addAttribute("imgurls", imgUrls);
  46. model.addAttribute("currentUrl", file);
  47. }
  48. } else {
  49. model.addAttribute("imgurls", imgUrls);
  50. model.addAttribute("currentUrl", url);
  51. }
  52. return "picture";
  53. }
  54. }