refactor: 添加获取文件byte[]接口

This commit is contained in:
caiyuchao
2025-09-02 18:09:02 +08:00
parent 10fc948b78
commit 9ef2f736c3
2 changed files with 22 additions and 0 deletions

View File

@@ -8,8 +8,10 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotEmpty;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@FeignClient(name = ApiConstants.NAME) // TODO 芋艿fallbackFactory =
@Tag(name = "RPC 服务 - 文件")
@@ -56,4 +58,7 @@ public interface FileApi {
@Operation(summary = "保存文件,并返回文件的访问路径")
CommonResult<String> createFile(@Valid @RequestBody FileCreateReqDTO createReqDTO);
@GetMapping(PREFIX + "/get")
@Operation(summary = "根据文件的访问路径返回文件内容")
CommonResult<byte[]> getFileContent(@Valid @RequestParam String url);
}

View File

@@ -1,8 +1,11 @@
package org.agt.module.infra.api.file;
import cn.hutool.core.util.StrUtil;
import org.agt.framework.common.pojo.CommonResult;
import org.agt.module.infra.api.file.dto.FileCreateReqDTO;
import org.agt.module.infra.service.file.FileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;
@@ -14,6 +17,7 @@ import static org.agt.framework.common.pojo.CommonResult.success;
@Validated
public class FileApiImpl implements FileApi {
private static final Logger log = LoggerFactory.getLogger(FileApiImpl.class);
@Resource
private FileService fileService;
@@ -23,4 +27,17 @@ public class FileApiImpl implements FileApi {
createReqDTO.getDirectory(), createReqDTO.getType()));
}
@Override
public CommonResult<byte[]> getFileContent(String url) {
byte[] content = null;
try {
String path = StrUtil.subAfter(url, "/get/", false);
String configId = StrUtil.subBetween(url, "/file/", "/get/");
content = fileService.getFileContent(Long.valueOf(configId), path);
} catch (Exception e) {
log.error("get file content error {}", url, e);
}
return success(content);
}
}