SysOssController.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /**
  2. * Copyright 2018 人人开源 http://www.renren.io
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * 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, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package io.renren.modules.oss.controller;
  17. import com.google.gson.Gson;
  18. import io.renren.common.exception.RRException;
  19. import io.renren.common.utils.ConfigConstant;
  20. import io.renren.common.utils.Constant;
  21. import io.renren.common.utils.PageUtils;
  22. import io.renren.common.utils.R;
  23. import io.renren.common.validator.ValidatorUtils;
  24. import io.renren.common.validator.group.AliyunGroup;
  25. import io.renren.common.validator.group.QcloudGroup;
  26. import io.renren.common.validator.group.QiniuGroup;
  27. import io.renren.modules.oss.cloud.CloudStorageConfig;
  28. import io.renren.modules.oss.cloud.OSSFactory;
  29. import io.renren.modules.oss.entity.SysOssEntity;
  30. import io.renren.modules.oss.service.SysOssService;
  31. import io.renren.modules.sys.service.SysConfigService;
  32. import org.apache.shiro.authz.annotation.RequiresPermissions;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.web.bind.annotation.*;
  35. import org.springframework.web.multipart.MultipartFile;
  36. import java.util.Arrays;
  37. import java.util.Date;
  38. import java.util.Map;
  39. /**
  40. * 文件上传
  41. *
  42. * @author chenshun
  43. * @email sunlightcs@gmail.com
  44. * @date 2017-03-25 12:13:26
  45. */
  46. @RestController
  47. @RequestMapping("sys/oss")
  48. public class SysOssController {
  49. @Autowired
  50. private SysOssService sysOssService;
  51. @Autowired
  52. private SysConfigService sysConfigService;
  53. private final static String KEY = ConfigConstant.CLOUD_STORAGE_CONFIG_KEY;
  54. /**
  55. * 列表
  56. */
  57. @GetMapping("/list")
  58. @RequiresPermissions("sys:oss:all")
  59. public R list(@RequestParam Map<String, Object> params){
  60. PageUtils page = sysOssService.queryPage(params);
  61. return R.ok().put("page", page);
  62. }
  63. /**
  64. * 云存储配置信息
  65. */
  66. @GetMapping("/config")
  67. @RequiresPermissions("sys:oss:all")
  68. public R config(){
  69. CloudStorageConfig config = sysConfigService.getConfigObject(KEY, CloudStorageConfig.class);
  70. return R.ok().put("config", config);
  71. }
  72. /**
  73. * 保存云存储配置信息
  74. */
  75. @PostMapping("/saveConfig")
  76. @RequiresPermissions("sys:oss:all")
  77. public R saveConfig(@RequestBody CloudStorageConfig config){
  78. //校验类型
  79. ValidatorUtils.validateEntity(config);
  80. if(config.getType() == Constant.CloudService.QINIU.getValue()){
  81. //校验七牛数据
  82. ValidatorUtils.validateEntity(config, QiniuGroup.class);
  83. }else if(config.getType() == Constant.CloudService.ALIYUN.getValue()){
  84. //校验阿里云数据
  85. ValidatorUtils.validateEntity(config, AliyunGroup.class);
  86. }else if(config.getType() == Constant.CloudService.QCLOUD.getValue()){
  87. //校验腾讯云数据
  88. ValidatorUtils.validateEntity(config, QcloudGroup.class);
  89. }
  90. sysConfigService.updateValueByKey(KEY, new Gson().toJson(config));
  91. return R.ok();
  92. }
  93. /**
  94. * 上传文件
  95. */
  96. @PostMapping("/upload")
  97. @RequiresPermissions("sys:oss:all")
  98. public R upload(@RequestParam("file") MultipartFile file) throws Exception {
  99. if (file.isEmpty()) {
  100. throw new RRException("上传文件不能为空");
  101. }
  102. //上传文件
  103. String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
  104. String url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
  105. //保存文件信息
  106. SysOssEntity ossEntity = new SysOssEntity();
  107. ossEntity.setUrl(url);
  108. ossEntity.setCreateDate(new Date());
  109. sysOssService.insert(ossEntity);
  110. return R.ok().put("url", url);
  111. }
  112. /**
  113. * 删除
  114. */
  115. @PostMapping("/delete")
  116. @RequiresPermissions("sys:oss:all")
  117. public R delete(@RequestBody Long[] ids){
  118. sysOssService.deleteBatchIds(Arrays.asList(ids));
  119. return R.ok();
  120. }
  121. }