FileResourceManager.java 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package com.yihu.ehr.basic.fileresource.service;
  2. import com.fasterxml.jackson.databind.node.ObjectNode;
  3. import com.yihu.ehr.fastdfs.FastDFSUtil;
  4. import com.yihu.ehr.query.BaseJpaService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.beans.factory.annotation.Value;
  7. import org.springframework.stereotype.Service;
  8. import org.springframework.transaction.annotation.Transactional;
  9. import java.io.ByteArrayInputStream;
  10. import java.io.InputStream;
  11. import java.util.Base64;
  12. import java.util.List;
  13. /**
  14. * @author linaz
  15. * @created 2016.05.12 8:53
  16. */
  17. @Service
  18. @Transactional
  19. public class FileResourceManager extends BaseJpaService<FileResource, XFileResourceRepository> {
  20. @Autowired
  21. private XFileResourceRepository resourceRepository;
  22. @Autowired
  23. private FastDFSUtil fastDFSUtil;
  24. @Value("${fast-dfs.public-server}")
  25. private String fastDfsPublicServers;
  26. public String saveFileResource(String fileStr, String fileName, FileResource fileResource) throws Exception {
  27. byte[] bytes = Base64.getDecoder().decode(fileStr);
  28. InputStream inputStream = new ByteArrayInputStream(bytes);
  29. String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  30. ObjectNode objectNode = fastDFSUtil.upload(inputStream, fileExtension, "");
  31. String groupName = objectNode.get("groupName").toString();
  32. String remoteFileName = objectNode.get("remoteFileName").toString();
  33. String path = groupName.substring(1,groupName.length()-1) + ":" + remoteFileName.substring(1,remoteFileName.length()-1);
  34. //保存到resource表中
  35. fileResource.setStoragePath(path);
  36. return resourceRepository.save(fileResource).getId();
  37. }
  38. public String saveFileResourceReturnUrl(String fileStr, String fileName, FileResource fileResource) throws Exception {
  39. byte[] bytes = Base64.getDecoder().decode(fileStr);
  40. InputStream inputStream = new ByteArrayInputStream(bytes);
  41. String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  42. ObjectNode objectNode = fastDFSUtil.upload(inputStream, fileExtension, "");
  43. String groupName = objectNode.get("groupName").toString();
  44. String remoteFileName = objectNode.get("remoteFileName").toString();
  45. String path = groupName.substring(1,groupName.length()-1) + ":" + remoteFileName.substring(1,remoteFileName.length()-1);
  46. // 保存到resource表中
  47. fileResource.setStoragePath(path);
  48. resourceRepository.save(fileResource).getId();
  49. return path ;
  50. }
  51. public String saveFileResourceReturnHttpUrl(String fileStr, String fileName, FileResource fileResource) throws Exception {
  52. byte[] bytes = Base64.getDecoder().decode(fileStr);
  53. InputStream inputStream = new ByteArrayInputStream(bytes);
  54. String fileExtension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
  55. ObjectNode objectNode = fastDFSUtil.upload(inputStream, fileExtension, "");
  56. String groupName = objectNode.get("groupName").toString();
  57. String remoteFileName = objectNode.get("remoteFileName").toString();
  58. String path = groupName.substring(1,groupName.length()-1) + ":" + remoteFileName.substring(1,remoteFileName.length()-1);
  59. // 保存到resource表中
  60. fileResource.setStoragePath(path);
  61. resourceRepository.save(fileResource).getId();
  62. path = path.replace(":","/");
  63. return fastDfsPublicServers +"/"+path ;
  64. }
  65. public List<FileResource> findByObjectId(String objectId) {
  66. return resourceRepository.findByObjectId(objectId);
  67. }
  68. public List<FileResource> findByObjectIdAndMime(String objectId, String mime) {
  69. return resourceRepository.findByObjectIdAndMime(objectId, mime);
  70. }
  71. public List<FileResource> findByStoragePath(String storagePath) {
  72. return resourceRepository.findByStoragePath(storagePath);
  73. }
  74. public boolean deleteFileResource(List<FileResource> fileResources) throws Exception {
  75. for(FileResource fileResource : fileResources){
  76. //删除表数据
  77. resourceRepository.delete(fileResource.getId());
  78. //删除fastdfs上的文件
  79. String storagePath = fileResource.getStoragePath();
  80. String groupName = storagePath.split(":")[0];
  81. String remoteFileName = storagePath.split(":")[1];
  82. fastDFSUtil.delete(groupName,remoteFileName);
  83. }
  84. return true;
  85. }
  86. public String getStoragePathById(String id) {
  87. FileResource file= resourceRepository.findById(id);
  88. String storage_path=file.getStoragePath();
  89. return storage_path;
  90. }
  91. }