FileResourceEndPoint.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package com.yihu.ehr.basic.fileresource.controller;
  2. import com.yihu.ehr.basic.fileresource.service.FileResource;
  3. import com.yihu.ehr.basic.fileresource.service.FileResourceManager;
  4. import com.yihu.ehr.constants.ApiVersion;
  5. import com.yihu.ehr.controller.EnvelopRestEndPoint;
  6. import com.yihu.ehr.fastdfs.FastDFSUtil;
  7. import com.yihu.ehr.util.id.BizObject;
  8. import io.swagger.annotations.Api;
  9. import io.swagger.annotations.ApiOperation;
  10. import io.swagger.annotations.ApiParam;
  11. import org.apache.commons.lang.StringUtils;
  12. import org.springframework.beans.factory.annotation.Autowired;
  13. import org.springframework.beans.factory.annotation.Value;
  14. import org.springframework.web.bind.annotation.*;
  15. import java.net.URLEncoder;
  16. import java.util.ArrayList;
  17. import java.util.Base64;
  18. import java.util.List;
  19. /**
  20. * @author linaz
  21. * @created 2016.05.12 8:53
  22. */
  23. @RestController
  24. @RequestMapping(ApiVersion.Version1_0)
  25. @Api(value = "files", description = "文件管理接口", tags = {"文件管理接口"})
  26. public class FileResourceEndPoint extends EnvelopRestEndPoint {
  27. @Autowired
  28. private FastDFSUtil fastDFSUtil;
  29. @Autowired
  30. private FileResourceManager fileResourceManager;
  31. @Value("${fast-dfs.public-server}")
  32. private String fastDfsPublicServers;
  33. @RequestMapping(value = "/files_upload", method = RequestMethod.POST)
  34. @ApiOperation(value = "上传文件")
  35. public String fileUpload(
  36. @ApiParam(name = "file_str", value = "文件字符串", required = true)
  37. @RequestBody String fileStr,
  38. @ApiParam(name = "file_name", value = "文件名", required = true)
  39. @RequestParam(value = "file_name") String fileName,
  40. @ApiParam(name = "json_data", value = "文件资源属性", required = true)
  41. @RequestParam(value = "json_data") String jsonData) throws Exception {
  42. FileResource fileResource = toEntity(jsonData, FileResource.class);
  43. fileResource.setId(getObjectId(BizObject.FileResource));
  44. return fileResourceManager.saveFileResource(fileStr, fileName, fileResource);
  45. }
  46. @RequestMapping(value = "/files_upload_returnUrl", method = RequestMethod.POST)
  47. @ApiOperation(value = "上传文件,并返回存储相对路径")
  48. public String fileUploadReturnUrl(
  49. @ApiParam(name = "file_str", value = "文件字符串", required = true)
  50. @RequestBody String fileStr,
  51. @ApiParam(name = "file_name", value = "文件名", required = true)
  52. @RequestParam(value = "file_name") String fileName,
  53. @ApiParam(name = "json_data", value = "文件资源属性", required = true)
  54. @RequestParam(value = "json_data") String jsonData) throws Exception {
  55. FileResource fileResource = toEntity(jsonData, FileResource.class);
  56. fileResource.setId(getObjectId(BizObject.FileResource));
  57. return fileResourceManager.saveFileResourceReturnUrl(fileStr, fileName, fileResource);
  58. }
  59. @RequestMapping(value = "/files_upload_returnHttpUrl", method = RequestMethod.POST)
  60. @ApiOperation(value = "上传文件,并返回存储绝对路径")
  61. public String fileUploadReturnHttpUrl(
  62. @ApiParam(name = "file_str", value = "文件字符串", required = true)
  63. @RequestBody String fileStr,
  64. @ApiParam(name = "file_name", value = "文件名", required = true)
  65. @RequestParam(value = "file_name") String fileName,
  66. @ApiParam(name = "json_data", value = "文件资源属性", required = true)
  67. @RequestParam(value = "json_data") String jsonData) throws Exception {
  68. FileResource fileResource = toEntity(jsonData, FileResource.class);
  69. fileResource.setId(getObjectId(BizObject.FileResource));
  70. return fileResourceManager.saveFileResourceReturnHttpUrl(fileStr, fileName, fileResource);
  71. }
  72. @RequestMapping(value = "/files", method = RequestMethod.DELETE)
  73. @ApiOperation(value = "删除资源表对应关系,并且删除fastdfs相对应文件")
  74. public boolean filesDelete(
  75. @ApiParam(name = "object_id", value = "文件字符串", required = true)
  76. @RequestParam(value = "object_id") String objectId) throws Exception {
  77. List<FileResource> fileResources = fileResourceManager.findByObjectId(objectId);
  78. return fileResourceManager.deleteFileResource(fileResources);
  79. }
  80. @RequestMapping(value = "/image_delete", method = RequestMethod.DELETE)
  81. @ApiOperation(value = "删除资源表对应关系,并且删除fastdfs相对应文件")
  82. public boolean filesDeleteByPath(
  83. @ApiParam(name = "storagePath", value = "文件路径", required = true)
  84. @RequestParam(value = "storagePath") String storagePath) throws Exception {
  85. String s = java.net.URLDecoder.decode(storagePath, "UTF-8");
  86. List<FileResource> fileResources = fileResourceManager.findByStoragePath(s);
  87. return fileResourceManager.deleteFileResource(fileResources);
  88. }
  89. @RequestMapping(value = "/files_download", method = RequestMethod.GET)
  90. @ApiOperation(value = "下载文件")
  91. public List<String> filesDownload(
  92. @ApiParam(name = "object_id", value = "文件字符串", required = true)
  93. @RequestParam(value = "object_id") String objectId,
  94. @ApiParam(name = "mime", value = "所有者", required = true)
  95. @RequestParam(value = "mime", required = false) String mime) throws Exception {
  96. List<FileResource> fileResources;
  97. if (StringUtils.isEmpty(mime))
  98. fileResources = fileResourceManager.findByObjectId(objectId);
  99. else
  100. fileResources = fileResourceManager.findByObjectIdAndMime(objectId, mime);
  101. List<String> filesStrs = new ArrayList<>();
  102. for (FileResource fileResource : fileResources) {
  103. String storagePath = fileResource.getStoragePath();
  104. String groupName = storagePath.split(":")[0];
  105. String remoteFileName = storagePath.split(":")[1];
  106. byte[] bytes = fastDFSUtil.download(groupName, remoteFileName);
  107. String fileStream = new String(Base64.getEncoder().encode(bytes));
  108. filesStrs.add(fileStream);
  109. }
  110. return filesStrs;
  111. }
  112. @RequestMapping(value = "/files_path", method = RequestMethod.GET)
  113. @ApiOperation(value = "获取文件路径")
  114. public List<String> getFilePath(
  115. @ApiParam(name = "object_id", value = "文件字符串", required = true)
  116. @RequestParam(value = "object_id") String objectId) throws Exception {
  117. List<FileResource> fileResources = fileResourceManager.findByObjectId(objectId);
  118. List<String> filesStrs = new ArrayList<>();
  119. for (FileResource fileResource : fileResources) {
  120. String storagePath = fileResource.getStoragePath();
  121. storagePath = URLEncoder.encode(storagePath, "ISO8859-1");
  122. filesStrs.add(storagePath);
  123. }
  124. return filesStrs;
  125. }
  126. @RequestMapping(value = "/image_view", method = RequestMethod.GET)
  127. @ApiOperation(value = "下载文件")
  128. public String imageView(
  129. @ApiParam(name = "storagePath", value = "文件路径", required = true)
  130. @RequestParam(value = "storagePath") String storagePath) throws Exception {
  131. String s = java.net.URLDecoder.decode(storagePath, "UTF-8");
  132. String groupName = s.split(":")[0];
  133. String remoteFileName = s.split(":")[1];
  134. byte[] bytes = fastDFSUtil.download(groupName, remoteFileName);
  135. String fileStream = new String(Base64.getEncoder().encode(bytes));
  136. return fileStream;
  137. }
  138. @ApiOperation(value = "根据文件ID,获取文件的真实访问路径")
  139. @RequestMapping(value = "/file/getRealPathById", method = RequestMethod.GET)
  140. public String getRealPathById(
  141. @ApiParam(name = "fileId", value = "文件ID", required = true)
  142. @RequestParam(value = "fileId") String fileId) throws Exception {
  143. String s = java.net.URLDecoder.decode(fileId, "UTF-8");
  144. String path = fileResourceManager.getStoragePathById(s);
  145. path = path.replace(":", "/");
  146. path = fastDfsPublicServers + "/" + path;
  147. return path;
  148. }
  149. @ApiOperation(value = "根据文件的存储路径,获取文件的真实访问路径")
  150. @RequestMapping(value = "/file/getRealPathByStoragePath", method = RequestMethod.GET)
  151. public String getRealPathByStoragePath(
  152. @ApiParam(name = "storagePath", value = "文件存储路径", required = true)
  153. @RequestParam(value = "storagePath") String storagePath) throws Exception {
  154. String realPath = fastDfsPublicServers + "/" + storagePath.replace(":", "/");
  155. return realPath;
  156. }
  157. }