lead
8 天以前 bc00242ee0410684eb9f4c5fe0aa486dfebb3162
yudao-module-infra/src/main/java/cn/iocoder/yudao/module/infra/controller/admin/file/FileController.java
@@ -1,15 +1,18 @@
package cn.iocoder.yudao.module.infra.controller.admin.file;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.core.util.URLUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.http.HttpUtils;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.infra.controller.admin.file.vo.file.*;
import cn.iocoder.yudao.module.infra.dal.dataobject.file.FileDO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import com.google.common.cache.CacheLoader;
import com.google.common.util.concurrent.UncheckedExecutionException;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
@@ -27,7 +30,6 @@
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.nio.charset.StandardCharsets;
import java.util.List;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
@@ -111,19 +113,34 @@
        if (StrUtil.isEmpty(path)) {
            throw new IllegalArgumentException("结尾的 path 路径必须传递");
        }
        // 解码,解决中文路径的问题
        // 解码,解决中文、%、+ 等特殊字符路径的问题
        // https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/807/
        // https://gitee.com/zhijiantianya/ruoyi-vue-pro/pulls/1432/
        path = URLUtil.decode(path, StandardCharsets.UTF_8, false);
        path = HttpUtils.decodeUrlPath(path);
        // 读取内容
        byte[] content = fileService.getFileContent(configId, path);
        byte[] content;
        try {
            content = fileService.getFileContent(configId, path);
        } catch (IllegalArgumentException | UncheckedExecutionException ex) {
            // 文件配置不存在(如 configId 无效)时,按资源不存在返回 404,而不是 500
            log.warn("[getFileContent][configId({}) path({}) 文件配置不存在]", configId, path);
            response.setStatus(HttpStatus.NOT_FOUND.value());
            return;
        } catch (CacheLoader.InvalidCacheLoadException ex) {
            // LoadingCache 的 load 返回 null 时抛出(例如历史文件记录的 configId 已被删除),同样按资源不存在处理
            log.warn("[getFileContent][configId({}) path({}) 文件配置不存在]", configId, path);
            response.setStatus(HttpStatus.NOT_FOUND.value());
            return;
        }
        if (content == null) {
            log.warn("[getFileContent][configId({}) path({}) 文件不存在]", configId, path);
            response.setStatus(HttpStatus.NOT_FOUND.value());
            return;
        }
        writeAttachment(response, path, content);
        FileDO file = fileService.getFileByConfigIdAndPath(configId, path);
        String filename = file != null && StrUtil.isNotEmpty(file.getName()) ? file.getName() : FileUtil.getName(path);
        writeAttachment(response, filename, content);
    }
    @GetMapping("/page")