OssEndpoint.java 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * Redistributions of source code must retain the above copyright notice,
  8. * this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * Neither the name of the dreamlu.net developer nor the names of its
  13. * contributors may be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. * Author: Chill 庄骞 (smallchill@163.com)
  16. */
  17. package org.springblade.modules.resource.endpoint;
  18. import io.swagger.annotations.Api;
  19. import lombok.AllArgsConstructor;
  20. import lombok.SneakyThrows;
  21. import org.springblade.core.launch.constant.AppConstant;
  22. import org.springblade.core.oss.model.BladeFile;
  23. import org.springblade.core.oss.model.OssFile;
  24. import org.springblade.core.secure.annotation.PreAuth;
  25. import org.springblade.core.tenant.annotation.NonDS;
  26. import org.springblade.core.tool.api.R;
  27. import org.springblade.core.tool.constant.RoleConstant;
  28. import org.springblade.core.tool.utils.FileUtil;
  29. import org.springblade.core.tool.utils.Func;
  30. import org.springblade.modules.resource.builder.oss.OssBuilder;
  31. import org.springblade.modules.resource.entity.Attach;
  32. import org.springblade.modules.resource.service.IAttachService;
  33. import org.springframework.web.bind.annotation.*;
  34. import org.springframework.web.multipart.MultipartFile;
  35. /**
  36. * 对象存储端点
  37. *
  38. * @author Chill
  39. */
  40. @NonDS
  41. @RestController
  42. @AllArgsConstructor
  43. @Api(value = "对象存储端点", tags = "对象存储端点")
  44. @RequestMapping(AppConstant.APPLICATION_RESOURCE_NAME + "/oss/endpoint")
  45. public class OssEndpoint {
  46. /**
  47. * 对象存储构建类
  48. */
  49. private final OssBuilder ossBuilder;
  50. /**
  51. * 附件表服务
  52. */
  53. private final IAttachService attachService;
  54. /**
  55. * 创建存储桶
  56. *
  57. * @param bucketName 存储桶名称
  58. * @return Bucket
  59. */
  60. @SneakyThrows
  61. @PostMapping("/make-bucket")
  62. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  63. public R makeBucket(@RequestParam String bucketName) {
  64. ossBuilder.template().makeBucket(bucketName);
  65. return R.success("创建成功");
  66. }
  67. /**
  68. * 创建存储桶
  69. *
  70. * @param bucketName 存储桶名称
  71. * @return R
  72. */
  73. @SneakyThrows
  74. @PostMapping("/remove-bucket")
  75. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  76. public R removeBucket(@RequestParam String bucketName) {
  77. ossBuilder.template().removeBucket(bucketName);
  78. return R.success("删除成功");
  79. }
  80. /**
  81. * 拷贝文件
  82. *
  83. * @param fileName 存储桶对象名称
  84. * @param destBucketName 目标存储桶名称
  85. * @param destFileName 目标存储桶对象名称
  86. * @return R
  87. */
  88. @SneakyThrows
  89. @PostMapping("/copy-file")
  90. public R copyFile(@RequestParam String fileName, @RequestParam String destBucketName, String destFileName) {
  91. ossBuilder.template().copyFile(fileName, destBucketName, destFileName);
  92. return R.success("操作成功");
  93. }
  94. /**
  95. * 获取文件信息
  96. *
  97. * @param fileName 存储桶对象名称
  98. * @return InputStream
  99. */
  100. @SneakyThrows
  101. @GetMapping("/stat-file")
  102. public R<OssFile> statFile(@RequestParam String fileName) {
  103. return R.data(ossBuilder.template().statFile(fileName));
  104. }
  105. /**
  106. * 获取文件相对路径
  107. *
  108. * @param fileName 存储桶对象名称
  109. * @return String
  110. */
  111. @SneakyThrows
  112. @GetMapping("/file-path")
  113. public R<String> filePath(@RequestParam String fileName) {
  114. return R.data(ossBuilder.template().filePath(fileName));
  115. }
  116. /**
  117. * 获取文件外链
  118. *
  119. * @param fileName 存储桶对象名称
  120. * @return String
  121. */
  122. @SneakyThrows
  123. @GetMapping("/file-link")
  124. public R<String> fileLink(@RequestParam String fileName) {
  125. return R.data(ossBuilder.template().fileLink(fileName));
  126. }
  127. /**
  128. * 上传文件
  129. *
  130. * @param file 文件
  131. * @return ObjectStat
  132. */
  133. @SneakyThrows
  134. @PostMapping("/put-file")
  135. public R<BladeFile> putFile(@RequestParam MultipartFile file) {
  136. BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
  137. return R.data(bladeFile);
  138. }
  139. /**
  140. * 上传文件
  141. *
  142. * @param fileName 存储桶对象名称
  143. * @param file 文件
  144. * @return ObjectStat
  145. */
  146. @SneakyThrows
  147. @PostMapping("/put-file-by-name")
  148. public R<BladeFile> putFile(@RequestParam String fileName, @RequestParam MultipartFile file) {
  149. BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
  150. return R.data(bladeFile);
  151. }
  152. /**
  153. * 上传文件并保存至附件表
  154. *
  155. * @param file 文件
  156. * @return ObjectStat
  157. */
  158. @SneakyThrows
  159. @PostMapping("/put-file-attach")
  160. public R<BladeFile> putFileAttach(@RequestParam MultipartFile file) {
  161. String fileName = file.getOriginalFilename();
  162. BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
  163. Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
  164. bladeFile.setAttachId(attachId);
  165. return R.data(bladeFile);
  166. }
  167. /**
  168. * 上传文件并保存至附件表
  169. *
  170. * @param fileName 存储桶对象名称
  171. * @param file 文件
  172. * @return ObjectStat
  173. */
  174. @SneakyThrows
  175. @PostMapping("/put-file-attach-by-name")
  176. public R<BladeFile> putFileAttach(@RequestParam String fileName, @RequestParam MultipartFile file) {
  177. BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
  178. Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
  179. bladeFile.setAttachId(attachId);
  180. return R.data(bladeFile);
  181. }
  182. /**
  183. * 构建附件表
  184. *
  185. * @param fileName 文件名
  186. * @param fileSize 文件大小
  187. * @param bladeFile 对象存储文件
  188. * @return attachId
  189. */
  190. private Long buildAttach(String fileName, Long fileSize, BladeFile bladeFile) {
  191. String fileExtension = FileUtil.getFileExtension(fileName);
  192. Attach attach = new Attach();
  193. attach.setDomain(bladeFile.getDomain());
  194. attach.setLink(bladeFile.getLink());
  195. attach.setName(bladeFile.getName());
  196. attach.setOriginalName(bladeFile.getOriginalName());
  197. attach.setAttachSize(fileSize);
  198. attach.setExtension(fileExtension);
  199. attachService.save(attach);
  200. return attach.getId();
  201. }
  202. /**
  203. * 删除文件
  204. *
  205. * @param fileName 存储桶对象名称
  206. * @return R
  207. */
  208. @SneakyThrows
  209. @PostMapping("/remove-file")
  210. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  211. public R removeFile(@RequestParam String fileName) {
  212. ossBuilder.template().removeFile(fileName);
  213. return R.success("操作成功");
  214. }
  215. /**
  216. * 批量删除文件
  217. *
  218. * @param fileNames 存储桶对象名称集合
  219. * @return R
  220. */
  221. @SneakyThrows
  222. @PostMapping("/remove-files")
  223. @PreAuth(RoleConstant.HAS_ROLE_ADMIN)
  224. public R removeFiles(@RequestParam String fileNames) {
  225. ossBuilder.template().removeFiles(Func.toStrList(fileNames));
  226. return R.success("操作成功");
  227. }
  228. }