OssEndpoint.java 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /**
  2. * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * <p>
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springblade.modules.resource;
  17. import io.swagger.annotations.Api;
  18. import lombok.AllArgsConstructor;
  19. import lombok.SneakyThrows;
  20. import org.springblade.core.oss.QiniuTemplate;
  21. import org.springblade.core.oss.model.BladeFile;
  22. import org.springblade.core.oss.model.OssFile;
  23. import org.springblade.core.tool.api.R;
  24. import org.springblade.core.tool.utils.Func;
  25. import org.springframework.web.bind.annotation.*;
  26. import org.springframework.web.multipart.MultipartFile;
  27. /**
  28. * 对象存储端点
  29. *
  30. * @author Chill
  31. */
  32. @RestController
  33. @AllArgsConstructor
  34. @RequestMapping("/blade-resource/oss/endpoint")
  35. @Api(value = "对象存储端点", tags = "对象存储端点")
  36. public class OssEndpoint {
  37. private QiniuTemplate qiniuTemplate;
  38. /**
  39. * 创建存储桶
  40. *
  41. * @param bucketName 存储桶名称
  42. * @return Bucket
  43. */
  44. @SneakyThrows
  45. @PostMapping("/make-bucket")
  46. public R makeBucket(@RequestParam String bucketName) {
  47. qiniuTemplate.makeBucket(bucketName);
  48. return R.success("创建成功");
  49. }
  50. /**
  51. * 创建存储桶
  52. *
  53. * @param bucketName 存储桶名称
  54. * @return R
  55. */
  56. @SneakyThrows
  57. @PostMapping("/remove-bucket")
  58. public R removeBucket(@RequestParam String bucketName) {
  59. qiniuTemplate.removeBucket(bucketName);
  60. return R.success("删除成功");
  61. }
  62. /**
  63. * 拷贝文件
  64. *
  65. * @param fileName 存储桶对象名称
  66. * @param destBucketName 目标存储桶名称
  67. * @param destFileName 目标存储桶对象名称
  68. * @return R
  69. */
  70. @SneakyThrows
  71. @PostMapping("/copy-file")
  72. public R copyFile(@RequestParam String fileName, @RequestParam String destBucketName, String destFileName) {
  73. qiniuTemplate.copyFile(fileName, destBucketName, destFileName);
  74. return R.success("操作成功");
  75. }
  76. /**
  77. * 获取文件信息
  78. *
  79. * @param fileName 存储桶对象名称
  80. * @return InputStream
  81. */
  82. @SneakyThrows
  83. @GetMapping("/stat-file")
  84. public R<OssFile> statFile(@RequestParam String fileName) {
  85. return R.data(qiniuTemplate.statFile(fileName));
  86. }
  87. /**
  88. * 获取文件相对路径
  89. *
  90. * @param fileName 存储桶对象名称
  91. * @return String
  92. */
  93. @SneakyThrows
  94. @GetMapping("/file-path")
  95. public R<String> filePath(@RequestParam String fileName) {
  96. return R.data(qiniuTemplate.filePath(fileName));
  97. }
  98. /**
  99. * 获取文件外链
  100. *
  101. * @param fileName 存储桶对象名称
  102. * @return String
  103. */
  104. @SneakyThrows
  105. @GetMapping("/file-link")
  106. public R<String> fileLink(@RequestParam String fileName) {
  107. return R.data(qiniuTemplate.fileLink(fileName));
  108. }
  109. /**
  110. * 上传文件
  111. *
  112. * @param file 文件
  113. * @return ObjectStat
  114. */
  115. @SneakyThrows
  116. @PostMapping("/put-file")
  117. public R<BladeFile> putFile(@RequestParam MultipartFile file) {
  118. BladeFile bladeFile = qiniuTemplate.putFile(file.getOriginalFilename(), file.getInputStream());
  119. return R.data(bladeFile);
  120. }
  121. /**
  122. * 上传文件
  123. *
  124. * @param fileName 存储桶对象名称
  125. * @param file 文件
  126. * @return ObjectStat
  127. */
  128. @SneakyThrows
  129. @PostMapping("/put-file-by-name")
  130. public R<BladeFile> putFile(@RequestParam String fileName, @RequestParam MultipartFile file) {
  131. BladeFile bladeFile = qiniuTemplate.putFile(fileName, file.getInputStream());
  132. return R.data(bladeFile);
  133. }
  134. /**
  135. * 删除文件
  136. *
  137. * @param fileName 存储桶对象名称
  138. * @return R
  139. */
  140. @SneakyThrows
  141. @PostMapping("/remove-file")
  142. public R removeFile(@RequestParam String fileName) {
  143. qiniuTemplate.removeFile(fileName);
  144. return R.success("操作成功");
  145. }
  146. /**
  147. * 批量删除文件
  148. *
  149. * @param fileNames 存储桶对象名称集合
  150. * @return R
  151. */
  152. @SneakyThrows
  153. @PostMapping("/remove-files")
  154. public R removeFiles(@RequestParam String fileNames) {
  155. qiniuTemplate.removeFiles(Func.toStrList(fileNames));
  156. return R.success("操作成功");
  157. }
  158. }