| | |
| | | import com.ruoyi.common.enums.BusinessType; |
| | | import com.ruoyi.common.exception.ServiceException; |
| | | import com.ruoyi.common.utils.file.FileUtils; |
| | | import com.ruoyi.oss.core.OssClient; |
| | | import com.ruoyi.oss.factory.OssFactory; |
| | | import com.ruoyi.system.domain.SysOss; |
| | | import com.ruoyi.system.domain.bo.SysOssBo; |
| | | import com.ruoyi.system.domain.vo.SysOssVo; |
| | | import com.ruoyi.system.service.ISysOssService; |
| | | import io.swagger.v3.oas.annotations.Operation; |
| | | import io.swagger.v3.oas.annotations.Parameter; |
| | | import io.swagger.v3.oas.annotations.Parameters; |
| | | import io.swagger.v3.oas.annotations.enums.ParameterIn; |
| | | import io.swagger.v3.oas.annotations.tags.Tag; |
| | | import lombok.RequiredArgsConstructor; |
| | | import org.springframework.http.MediaType; |
| | | import org.springframework.validation.annotation.Validated; |
| | | import org.springframework.web.bind.annotation.*; |
| | | import org.springframework.web.multipart.MultipartFile; |
| | | |
| | | import javax.servlet.http.HttpServletResponse; |
| | | import javax.validation.constraints.NotEmpty; |
| | | import jakarta.servlet.http.HttpServletResponse; |
| | | import jakarta.validation.constraints.NotEmpty; |
| | | import java.io.IOException; |
| | | import java.util.Arrays; |
| | | import java.util.HashMap; |
| | |
| | | * @author Lion Li |
| | | */ |
| | | @Validated |
| | | @Tag(name ="对象存储控制器", description = "对象存储管理") |
| | | @RequiredArgsConstructor |
| | | @RestController |
| | | @RequestMapping("/system/oss") |
| | |
| | | /** |
| | | * 查询OSS对象存储列表 |
| | | */ |
| | | @Operation(summary = "查询OSS对象存储列表") |
| | | @SaCheckPermission("system:oss:list") |
| | | @GetMapping("/list") |
| | | public TableDataInfo<SysOssVo> list(@Validated(QueryGroup.class) SysOssBo bo, PageQuery pageQuery) { |
| | |
| | | |
| | | /** |
| | | * 查询OSS对象基于id串 |
| | | * |
| | | * @param ossIds OSS对象ID串 |
| | | */ |
| | | @Operation(summary = "查询OSS对象基于ID") |
| | | @SaCheckPermission("system:oss:list") |
| | | @GetMapping("/listByIds/{ossIds}") |
| | | public R<List<SysOssVo>> listByIds(@Parameter(name = "OSS对象ID串") |
| | | @NotEmpty(message = "主键不能为空") |
| | | @PathVariable Long[] ossIds) { |
| | | public R<List<SysOssVo>> listByIds(@NotEmpty(message = "主键不能为空") |
| | | @PathVariable Long[] ossIds) { |
| | | List<SysOssVo> list = iSysOssService.listByIds(Arrays.asList(ossIds)); |
| | | return R.ok(list); |
| | | } |
| | | |
| | | /** |
| | | * 上传OSS对象存储 |
| | | * |
| | | * @param file 文件 |
| | | */ |
| | | @Operation(summary = "上传OSS对象存储") |
| | | @Parameters({ |
| | | @Parameter(name = "file", description = "文件", in = ParameterIn.QUERY, required = true) |
| | | }) |
| | | @SaCheckPermission("system:oss:upload") |
| | | @Log(title = "OSS对象存储", businessType = BusinessType.INSERT) |
| | | @PostMapping("/upload") |
| | | @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) |
| | | public R<Map<String, String>> upload(@RequestPart("file") MultipartFile file) { |
| | | if (ObjectUtil.isNull(file)) { |
| | | throw new ServiceException("上传文件不能为空"); |
| | | } |
| | | SysOss oss = iSysOssService.upload(file); |
| | | SysOssVo oss = iSysOssService.upload(file); |
| | | Map<String, String> map = new HashMap<>(2); |
| | | map.put("url", oss.getUrl()); |
| | | map.put("fileName", oss.getOriginalName()); |
| | |
| | | return R.ok(map); |
| | | } |
| | | |
| | | @Operation(summary = "下载OSS对象存储") |
| | | /** |
| | | * 下载OSS对象 |
| | | * |
| | | * @param ossId OSS对象ID |
| | | */ |
| | | @SaCheckPermission("system:oss:download") |
| | | @GetMapping("/download/{ossId}") |
| | | public void download(@Parameter(name = "OSS对象ID") @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)); |
| | | public void download(@PathVariable Long ossId, HttpServletResponse response) throws IOException { |
| | | iSysOssService.download(ossId,response); |
| | | } |
| | | |
| | | /** |
| | | * 删除OSS对象存储 |
| | | * |
| | | * @param ossIds OSS对象ID串 |
| | | */ |
| | | @Operation(summary = "删除OSS对象存储") |
| | | @SaCheckPermission("system:oss:remove") |
| | | @Log(title = "OSS对象存储", businessType = BusinessType.DELETE) |
| | | @DeleteMapping("/{ossIds}") |
| | | public R<Void> remove(@Parameter(name = "OSS对象ID串") |
| | | @NotEmpty(message = "主键不能为空") |
| | | @PathVariable Long[] ossIds) { |
| | | return toAjax(iSysOssService.deleteWithValidByIds(Arrays.asList(ossIds), true) ? 1 : 0); |
| | | public R<Void> remove(@NotEmpty(message = "主键不能为空") |
| | | @PathVariable Long[] ossIds) { |
| | | return toAjax(iSysOssService.deleteWithValidByIds(Arrays.asList(ossIds), true)); |
| | | } |
| | | |
| | | } |