疯狂的狮子li
2021-10-27 695cb6d76b838b1e4cb161bae335faa948dca12a
ruoyi-admin/src/main/java/com/ruoyi/web/controller/system/SysOssController.java
@@ -38,8 +38,6 @@
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -56,92 +54,90 @@
@RequestMapping("/system/oss")
public class SysOssController extends BaseController {
   private final ISysOssService iSysOssService;
   private final ISysConfigService iSysConfigService;
    private final ISysOssService iSysOssService;
    private final ISysConfigService iSysConfigService;
   /**
    * 查询OSS对象存储列表
    */
   @ApiOperation("查询OSS对象存储列表")
   @SaCheckPermission("system:oss:list")
   @GetMapping("/list")
   public TableDataInfo<SysOssVo> list(@Validated(QueryGroup.class) SysOssBo bo) {
      return iSysOssService.queryPageList(bo);
   }
    /**
     * 查询OSS对象存储列表
     */
    @ApiOperation("查询OSS对象存储列表")
    @SaCheckPermission("system:oss:list")
    @GetMapping("/list")
    public TableDataInfo<SysOssVo> list(@Validated(QueryGroup.class) SysOssBo bo) {
        return iSysOssService.queryPageList(bo);
    }
   /**
    * 上传OSS对象存储
    */
   @ApiOperation("上传OSS对象存储")
   @ApiImplicitParams({
      @ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
   })
   @SaCheckPermission("system:oss:upload")
   @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
   @RepeatSubmit
   @PostMapping("/upload")
   public AjaxResult<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
      if (ObjectUtil.isNull(file)) {
         throw new ServiceException("上传文件不能为空");
      }
      SysOss oss = iSysOssService.upload(file);
      Map<String, String> map = new HashMap<>(2);
      map.put("url", oss.getUrl());
      map.put("fileName", oss.getFileName());
      return AjaxResult.success(map);
   }
    /**
     * 上传OSS对象存储
     */
    @ApiOperation("上传OSS对象存储")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "java.io.File", required = true),
    })
    @SaCheckPermission("system:oss:upload")
    @Log(title = "OSS对象存储", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping("/upload")
    public AjaxResult<Map<String, String>> upload(@RequestPart("file") MultipartFile file) {
        if (ObjectUtil.isNull(file)) {
            throw new ServiceException("上传文件不能为空");
        }
        SysOss oss = iSysOssService.upload(file);
        Map<String, String> map = new HashMap<>(2);
        map.put("url", oss.getUrl());
        map.put("fileName", oss.getFileName());
        return AjaxResult.success(map);
    }
   @ApiOperation("下载OSS对象存储")
   @SaCheckPermission("system:oss:download")
   @GetMapping("/download/{ossId}")
   public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
      SysOss sysOss = iSysOssService.getById(ossId);
      if (ObjectUtil.isNull(sysOss)) {
         throw new ServiceException("文件数据不存在!");
      }
      response.reset();
      response.addHeader("Access-Control-Allow-Origin", "*");
      response.addHeader("Access-Control-Expose-Headers", "Content-Disposition");
      FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
      response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
      long data;
      try {
         data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false);
      } catch (HttpException e) {
         if (e.getMessage().contains("403")) {
            throw new ServiceException("无读取权限, 请在对应的OSS开启'公有读'权限!");
         } else {
            throw new ServiceException(e.getMessage());
         }
      }
      response.setContentLength(Convert.toInt(data));
   }
    @ApiOperation("下载OSS对象存储")
    @SaCheckPermission("system:oss:download")
    @GetMapping("/download/{ossId}")
    public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException {
        SysOss sysOss = iSysOssService.getById(ossId);
        if (ObjectUtil.isNull(sysOss)) {
            throw new ServiceException("文件数据不存在!");
        }
        response.reset();
        FileUtils.setAttachmentResponseHeader(response, sysOss.getOriginalName());
        response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE + "; charset=UTF-8");
        long data;
        try {
            data = HttpUtil.download(sysOss.getUrl(), response.getOutputStream(), false);
        } catch (HttpException e) {
            if (e.getMessage().contains("403")) {
                throw new ServiceException("无读取权限, 请在对应的OSS开启'公有读'权限!");
            } else {
                throw new ServiceException(e.getMessage());
            }
        }
        response.setContentLength(Convert.toInt(data));
    }
   /**
    * 删除OSS对象存储
    */
   @ApiOperation("删除OSS对象存储")
   @SaCheckPermission("system:oss:remove")
   @Log(title = "OSS对象存储" , businessType = BusinessType.DELETE)
   @DeleteMapping("/{ossIds}")
   public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                           @PathVariable Long[] ossIds) {
      return toAjax(iSysOssService.deleteWithValidByIds(Arrays.asList(ossIds), true) ? 1 : 0);
   }
    /**
     * 删除OSS对象存储
     */
    @ApiOperation("删除OSS对象存储")
    @SaCheckPermission("system:oss:remove")
    @Log(title = "OSS对象存储", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ossIds}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")
                                   @PathVariable Long[] ossIds) {
        return toAjax(iSysOssService.deleteWithValidByIds(Arrays.asList(ossIds), true) ? 1 : 0);
    }
   /**
    * 变更图片列表预览状态
    */
   @ApiOperation("变更图片列表预览状态")
   @SaCheckPermission("system:oss:edit")
   @Log(title = "OSS对象存储" , businessType = BusinessType.UPDATE)
   @PutMapping("/changePreviewListResource")
   public AjaxResult<Void> changePreviewListResource(@RequestBody String body) {
      Map<String, Boolean> map = JsonUtils.parseMap(body);
      SysConfig config = iSysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
         .eq(SysConfig::getConfigKey, CloudConstant.PEREVIEW_LIST_RESOURCE_KEY));
      config.setConfigValue(map.get("previewListResource").toString());
      return toAjax(iSysConfigService.updateConfig(config));
   }
    /**
     * 变更图片列表预览状态
     */
    @ApiOperation("变更图片列表预览状态")
    @SaCheckPermission("system:oss:edit")
    @Log(title = "OSS对象存储", businessType = BusinessType.UPDATE)
    @PutMapping("/changePreviewListResource")
    public AjaxResult<Void> changePreviewListResource(@RequestBody String body) {
        Map<String, Boolean> map = JsonUtils.parseMap(body);
        SysConfig config = iSysConfigService.getOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getConfigKey, CloudConstant.PEREVIEW_LIST_RESOURCE_KEY));
        config.setConfigValue(map.get("previewListResource").toString());
        return toAjax(iSysConfigService.updateConfig(config));
    }
}