fix: OSS上传文件URL使用transformEndpoint替换内部endpoint

- 新增transformBladeFile()方法,在返回文件URL时将minio:9000替换为外网域名
- 修改putFile/putFileAttach等接口,返回外网可访问的URL
- 修改fileLink接口,返回转换后的外链地址

Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
This commit is contained in:
2026-01-24 13:30:01 +08:00
co-authored by factory-droid[bot]
parent dc7a8e66ac
commit 4d43210d73
@@ -8,13 +8,16 @@ import org.springblade.core.launch.constant.AppConstant;
import org.springblade.core.oss.model.BladeFile;
import org.springblade.core.oss.model.OssFile;
import org.springblade.core.secure.annotation.PreAuth;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.NonDS;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.RoleConstant;
import org.springblade.core.tool.utils.FileUtil;
import org.springblade.core.tool.utils.Func;
import org.springblade.core.tool.utils.StringUtil;
import org.springblade.modules.resource.builder.OssBuilder;
import org.springblade.modules.resource.pojo.entity.Attach;
import org.springblade.modules.resource.pojo.entity.Oss;
import org.springblade.modules.resource.service.IAttachService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
@@ -41,6 +44,21 @@ public class OssEndpoint {
*/
private final IAttachService attachService;
/**
* 转换文件链接,将内部endpoint替换为外部transformEndpoint
*/
private BladeFile transformBladeFile(BladeFile bladeFile) {
Oss oss = ossBuilder.getOss(AuthUtil.getTenantId(), "");
if (StringUtil.isNotBlank(oss.getTransformEndpoint()) && StringUtil.isNotBlank(oss.getEndpoint())) {
String link = bladeFile.getLink();
if (StringUtil.isNotBlank(link)) {
bladeFile.setLink(link.replace(oss.getEndpoint(), oss.getTransformEndpoint()));
}
bladeFile.setDomain(oss.getTransformEndpoint());
}
return bladeFile;
}
/**
* 创建存储桶
*
@@ -118,7 +136,12 @@ public class OssEndpoint {
@SneakyThrows
@GetMapping("/file-link")
public R<String> fileLink(@RequestParam String fileName) {
return R.data(ossBuilder.template().fileLink(fileName));
String link = ossBuilder.template().fileLink(fileName);
Oss oss = ossBuilder.getOss(AuthUtil.getTenantId(), "");
if (StringUtil.isNotBlank(oss.getTransformEndpoint()) && StringUtil.isNotBlank(oss.getEndpoint())) {
link = link.replace(oss.getEndpoint(), oss.getTransformEndpoint());
}
return R.data(link);
}
/**
@@ -131,7 +154,7 @@ public class OssEndpoint {
@PostMapping("/put-file")
public R<BladeFile> putFile(@RequestParam MultipartFile file) {
BladeFile bladeFile = ossBuilder.template().putFile(file.getOriginalFilename(), file.getInputStream());
return R.data(bladeFile);
return R.data(transformBladeFile(bladeFile));
}
/**
@@ -145,7 +168,7 @@ public class OssEndpoint {
@PostMapping("/put-file-by-name")
public R<BladeFile> putFile(@RequestParam String fileName, @RequestParam MultipartFile file) {
BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
return R.data(bladeFile);
return R.data(transformBladeFile(bladeFile));
}
/**
@@ -159,6 +182,7 @@ public class OssEndpoint {
public R<BladeFile> putFileAttach(@RequestParam MultipartFile file) {
String fileName = file.getOriginalFilename();
BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
bladeFile = transformBladeFile(bladeFile);
Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
bladeFile.setAttachId(attachId);
return R.data(bladeFile);
@@ -175,6 +199,7 @@ public class OssEndpoint {
@PostMapping("/put-file-attach-by-name")
public R<BladeFile> putFileAttach(@RequestParam String fileName, @RequestParam MultipartFile file) {
BladeFile bladeFile = ossBuilder.template().putFile(fileName, file.getInputStream());
bladeFile = transformBladeFile(bladeFile);
Long attachId = buildAttach(fileName, file.getSize(), bladeFile);
bladeFile.setAttachId(attachId);
return R.data(bladeFile);