2
0

feat: return gateway url and local url while upload file

This commit is contained in:
zhangsz
2025-01-16 11:53:04 +08:00
parent d1ff37370c
commit d5e3533d2d
8 changed files with 63 additions and 16 deletions

File diff suppressed because one or more lines are too long

View File

@@ -15,6 +15,11 @@ public class SysFile
*/ */
private String name; private String name;
/**
* 文件内部地址
*/
private String localUrl;
/** /**
* 文件地址 * 文件地址
*/ */
@@ -30,6 +35,14 @@ public class SysFile
this.name = name; this.name = name;
} }
public String getLocalUrl() {
return localUrl;
}
public void setLocalUrl(String localUrl) {
this.localUrl = localUrl;
}
public String getUrl() public String getUrl()
{ {
return url; return url;
@@ -44,6 +57,7 @@ public class SysFile
public String toString() { public String toString() {
return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE) return new ToStringBuilder(this,ToStringStyle.MULTI_LINE_STYLE)
.append("name", getName()) .append("name", getName())
.append("localUrl", getLocalUrl())
.append("url", getUrl()) .append("url", getUrl())
.toString(); .toString();
} }

View File

@@ -9,6 +9,7 @@ import org.springframework.web.multipart.MultipartFile;
import org.wfc.common.core.domain.R; import org.wfc.common.core.domain.R;
import org.wfc.common.core.utils.file.FileUtils; import org.wfc.common.core.utils.file.FileUtils;
import org.wfc.file.service.ISysFileService; import org.wfc.file.service.ISysFileService;
import org.wfc.file.service.LocalSysFileServiceImpl.FileUploadResult;
import org.wfc.system.api.domain.SysFile; import org.wfc.system.api.domain.SysFile;
/** /**
@@ -30,10 +31,12 @@ public class SysFileController {
public R<SysFile> upload(MultipartFile file) { public R<SysFile> upload(MultipartFile file) {
try { try {
// 上传并返回访问地址 // 上传并返回访问地址
String url = sysFileService.uploadFile(file); FileUploadResult urlResult = sysFileService.uploadFile(file);
SysFile sysFile = new SysFile(); SysFile sysFile = new SysFile();
sysFile.setName(FileUtils.getName(url)); sysFile.setName(FileUtils.getName(urlResult.getLocalUrl()));
sysFile.setUrl(url); sysFile.setName(FileUtils.getName(urlResult.getGatewayUrl()));
sysFile.setLocalUrl(urlResult.getLocalUrl());
sysFile.setUrl(urlResult.getGatewayUrl());
return R.ok(sysFile); return R.ok(sysFile);
} catch (Exception e) { } catch (Exception e) {
log.error("上传文件失败", e); log.error("上传文件失败", e);

View File

@@ -28,19 +28,20 @@ public class FastDfsSysFileServiceImpl implements ISysFileService
private FastFileStorageClient storageClient; private FastFileStorageClient storageClient;
/** /**
* FastDfs文件上传接口 * FastDFS文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return FileUploadResult 包含本地和网关访问地址
* @throws Exception * @throws Exception
*/ */
@Override @Override
public String uploadFile(MultipartFile file) throws Exception public LocalSysFileServiceImpl.FileUploadResult uploadFile(MultipartFile file) throws Exception
{ {
InputStream inputStream = file.getInputStream(); InputStream inputStream = file.getInputStream();
StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(), StorePath storePath = storageClient.uploadFile(inputStream, file.getSize(),
FileTypeUtils.getExtension(file), null); FileTypeUtils.getExtension(file), null);
IoUtils.closeQuietly(inputStream); IoUtils.closeQuietly(inputStream);
return domain + "/" + storePath.getFullPath(); String fileUrl = domain + "/" + storePath.getFullPath();
return new LocalSysFileServiceImpl.FileUploadResult(fileUrl, fileUrl);
} }
} }

View File

@@ -1,6 +1,7 @@
package org.wfc.file.service; package org.wfc.file.service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.wfc.file.service.LocalSysFileServiceImpl.FileUploadResult;
/** /**
* 文件上传接口 * 文件上传接口
@@ -16,5 +17,5 @@ public interface ISysFileService
* @return 访问地址 * @return 访问地址
* @throws Exception * @throws Exception
*/ */
public String uploadFile(MultipartFile file) throws Exception; public FileUploadResult uploadFile(MultipartFile file) throws Exception;
} }

View File

@@ -33,17 +33,42 @@ public class LocalSysFileServiceImpl implements ISysFileService
@Value("${file.path}") @Value("${file.path}")
private String localFilePath; private String localFilePath;
@Value("${file.gateway}")
public String gateway;
@Value("${file.pathPrefix}")
public String pathPrefix;
/** /**
* 本地文件上传接口 * 本地文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return FileUploadResult 包含本地和网关访问地址
* @throws Exception * @throws Exception
*/ */
@Override @Override
public String uploadFile(MultipartFile file) throws Exception public FileUploadResult uploadFile(MultipartFile file) throws Exception {
{
String name = FileUploadUtils.upload(localFilePath, file); String name = FileUploadUtils.upload(localFilePath, file);
return domain + localFilePrefix + name; String localUrl = domain + localFilePrefix + name;
String gatewayUrl = gateway + pathPrefix + localFilePrefix + name;
return new FileUploadResult(localUrl, gatewayUrl);
}
public static class FileUploadResult {
private final String localUrl;
private final String gatewayUrl;
public FileUploadResult(String localUrl, String gatewayUrl) {
this.localUrl = localUrl;
this.gatewayUrl = gatewayUrl;
}
public String getLocalUrl() {
return localUrl;
}
public String getGatewayUrl() {
return gatewayUrl;
}
} }
} }

View File

@@ -28,11 +28,11 @@ public class MinioSysFileServiceImpl implements ISysFileService
* Minio文件上传接口 * Minio文件上传接口
* *
* @param file 上传的文件 * @param file 上传的文件
* @return 访问地址 * @return FileUploadResult 包含本地和网关访问地址
* @throws Exception * @throws Exception
*/ */
@Override @Override
public String uploadFile(MultipartFile file) throws Exception public LocalSysFileServiceImpl.FileUploadResult uploadFile(MultipartFile file) throws Exception
{ {
String fileName = FileUploadUtils.extractFilename(file); String fileName = FileUploadUtils.extractFilename(file);
InputStream inputStream = file.getInputStream(); InputStream inputStream = file.getInputStream();
@@ -44,6 +44,7 @@ public class MinioSysFileServiceImpl implements ISysFileService
.build(); .build();
client.putObject(args); client.putObject(args);
IoUtils.closeQuietly(inputStream); IoUtils.closeQuietly(inputStream);
return minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName; String fileUrl = minioConfig.getUrl() + "/" + minioConfig.getBucketName() + "/" + fileName;
return new LocalSysFileServiceImpl.FileUploadResult(fileUrl, fileUrl);
} }
} }

View File

@@ -3,6 +3,8 @@ file:
domain: wfc-file:${WFC_FILE_PORT:9201} domain: wfc-file:${WFC_FILE_PORT:9201}
path: /opt/wfc/upload path: /opt/wfc/upload
prefix: /kyc prefix: /kyc
gateway: http://${GATEWAY_SERVER_IP}:${GATEWAY_SERVER_PORT} # 新增网关地址配置
pathPrefix: /file # 新增文件模块地址前缀
# FastDFS配置 # FastDFS配置
fdfs: fdfs: