Commit ccc4cad1c8d6575f452d17e8bc28af306b62f8e5
Committed by
Gitee
1 parent
c04ad317
删除文件 yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/mo…
…dule/infra/controller/admin/file/FileController.java
Showing
1 changed file
with
0 additions
and
100 deletions
yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java deleted
| 1 | -package cn.iocoder.yudao.module.infra.controller.admin.file; | |
| 2 | - | |
| 3 | -import cn.hutool.core.io.IoUtil; | |
| 4 | -import cn.hutool.core.util.StrUtil; | |
| 5 | -import cn.iocoder.yudao.framework.common.pojo.CommonResult; | |
| 6 | -import cn.iocoder.yudao.framework.common.pojo.PageResult; | |
| 7 | -import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; | |
| 8 | -import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog; | |
| 9 | -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FilePageReqVO; | |
| 10 | -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileRespVO; | |
| 11 | -import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.FileUploadReqVO; | |
| 12 | -import cn.iocoder.yudao.module.infra.convert.file.FileConvert; | |
| 13 | -import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO; | |
| 14 | -import cn.iocoder.yudao.module.infra.service.file.FileService; | |
| 15 | -import io.swagger.annotations.Api; | |
| 16 | -import io.swagger.annotations.ApiImplicitParam; | |
| 17 | -import io.swagger.annotations.ApiImplicitParams; | |
| 18 | -import io.swagger.annotations.ApiOperation; | |
| 19 | -import lombok.extern.slf4j.Slf4j; | |
| 20 | -import org.springframework.http.HttpStatus; | |
| 21 | -import org.springframework.security.access.prepost.PreAuthorize; | |
| 22 | -import org.springframework.validation.annotation.Validated; | |
| 23 | -import org.springframework.web.bind.annotation.*; | |
| 24 | -import org.springframework.web.multipart.MultipartFile; | |
| 25 | - | |
| 26 | -import javax.annotation.Resource; | |
| 27 | -import javax.annotation.security.PermitAll; | |
| 28 | -import javax.servlet.http.HttpServletRequest; | |
| 29 | -import javax.servlet.http.HttpServletResponse; | |
| 30 | -import javax.validation.Valid; | |
| 31 | - | |
| 32 | -import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success; | |
| 33 | - | |
| 34 | -@Api(tags = "管理后台 - 文件存储") | |
| 35 | -@RestController | |
| 36 | -@RequestMapping("/infra/file") | |
| 37 | -@Validated | |
| 38 | -@Slf4j | |
| 39 | -public class FileController { | |
| 40 | - | |
| 41 | - @Resource | |
| 42 | - private FileService fileService; | |
| 43 | - | |
| 44 | - @PostMapping("/upload") | |
| 45 | - @ApiOperation("上传文件") | |
| 46 | - @OperateLog(logArgs = false) // 上传文件,没有记录操作日志的必要 | |
| 47 | - public CommonResult<String> uploadFile(FileUploadReqVO uploadReqVO) throws Exception { | |
| 48 | - MultipartFile file = uploadReqVO.getFile(); | |
| 49 | - String path = uploadReqVO.getPath(); | |
| 50 | - return success(fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()))); | |
| 51 | - } | |
| 52 | - | |
| 53 | - @DeleteMapping("/delete") | |
| 54 | - @ApiOperation("删除文件") | |
| 55 | - @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class) | |
| 56 | - @PreAuthorize("@ss.hasPermission('infra:file:delete')") | |
| 57 | - public CommonResult<Boolean> deleteFile(@RequestParam("id") Long id) throws Exception { | |
| 58 | - fileService.deleteFile(id); | |
| 59 | - return success(true); | |
| 60 | - } | |
| 61 | - | |
| 62 | - @GetMapping("/{configId}/get/**") | |
| 63 | - @PermitAll | |
| 64 | - @ApiOperation("下载文件") | |
| 65 | - @ApiImplicitParams({ | |
| 66 | - @ApiImplicitParam(name = "configId", value = "配置编号", required = true, dataTypeClass = Long.class), | |
| 67 | - @ApiImplicitParam(name = "fileName", value = "文件实际名称", dataTypeClass = String.class) | |
| 68 | - }) | |
| 69 | - public void getFileContent(HttpServletRequest request, | |
| 70 | - HttpServletResponse response, | |
| 71 | - String fileName, | |
| 72 | - @PathVariable("configId") Long configId) throws Exception { | |
| 73 | - // 获取请求的路径 | |
| 74 | - String path = StrUtil.subAfter(request.getRequestURI(), "/get/", false); | |
| 75 | - if (StrUtil.isEmpty(path)) { | |
| 76 | - throw new IllegalArgumentException("结尾的 path 路径必须传递"); | |
| 77 | - } | |
| 78 | - | |
| 79 | - // 读取内容 | |
| 80 | - byte[] content = fileService.getFileContent(configId, path); | |
| 81 | - if (content == null) { | |
| 82 | - log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path); | |
| 83 | - response.setStatus(HttpStatus.NOT_FOUND.value()); | |
| 84 | - return; | |
| 85 | - } | |
| 86 | - if (StrUtil.isBlank(fileName)) { | |
| 87 | - fileName = StrUtil.subAfter(path, "/", true); | |
| 88 | - } | |
| 89 | - ServletUtils.writeAttachment(response, fileName, content); | |
| 90 | - } | |
| 91 | - | |
| 92 | - @GetMapping("/page") | |
| 93 | - @ApiOperation("获得文件分页") | |
| 94 | - @PreAuthorize("@ss.hasPermission('infra:file:query')") | |
| 95 | - public CommonResult<PageResult<FileRespVO>> getFilePage(@Valid FilePageReqVO pageVO) { | |
| 96 | - PageResult<FileDO> pageResult = fileService.getFilePage(pageVO); | |
| 97 | - return success(FileConvert.INSTANCE.convertPage(pageResult)); | |
| 98 | - } | |
| 99 | - | |
| 100 | -} |