feat: 资源列表批量下载优化
This commit is contained in:
@@ -120,6 +120,11 @@ public class FileController {
|
||||
return timeWindowStorageService.getSmartViewUrl(id);
|
||||
}
|
||||
|
||||
@GetMapping("/file/download/{id}")
|
||||
public String getFiledownloadUrl(@PathVariable String id) {
|
||||
return timeWindowStorageService.getFileDownLoadUrl(id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据文件ID获取智能访问 URL(Result 包装)
|
||||
*/
|
||||
|
||||
+90
@@ -81,6 +81,25 @@ public class TimeWindowStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取文件下载地址
|
||||
*/
|
||||
public String getFileDownLoadUrl(String fileId) {
|
||||
try {
|
||||
FileInfo fileInfo = ossService.getFileInfo(fileId);
|
||||
if (fileInfo == null) {
|
||||
log.warn("文件不存在: fileId={}", fileId);
|
||||
return "";
|
||||
}
|
||||
String storageType = getStorageTypeByTime(fileInfo);
|
||||
return getDownloadUrlByStorageType(fileInfo, storageType);
|
||||
} catch (Exception e) {
|
||||
log.error("智能存储查询失败: fileId={}, error={}", fileId, e.getMessage(), e);
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 智能获取文件访问URL(通过ID)
|
||||
*/
|
||||
@@ -201,6 +220,31 @@ public class TimeWindowStorageService {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取下载地址
|
||||
*/
|
||||
private String getDownloadUrlByStorageType(FileInfo fileInfo, String storageType) {
|
||||
|
||||
if ("S3".equals(storageType)) {
|
||||
// 冷数据:S3优先,失败降级到OSS
|
||||
String s3Url = queryDownLoadFromS3(fileInfo);
|
||||
if (s3Url != null && !s3Url.isEmpty()) {
|
||||
return s3Url;
|
||||
}
|
||||
String ossUrl = queryDownLoadFromOss(fileInfo);
|
||||
return applyCdnAcceleration(ossUrl, "OSS");
|
||||
} else {
|
||||
// 热数据:OSS优先,失败降级到S3
|
||||
String ossUrl = queryDownLoadFromOss(fileInfo);
|
||||
if (ossUrl != null && !ossUrl.isEmpty()) {
|
||||
return applyCdnAcceleration(ossUrl, "OSS");
|
||||
}
|
||||
String s3Url = queryDownLoadFromS3(fileInfo);
|
||||
return s3Url;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据存储类型获取访问URL
|
||||
*/
|
||||
@@ -295,6 +339,25 @@ public class TimeWindowStorageService {
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 从OSS查询文件URL
|
||||
*/
|
||||
private String queryDownLoadFromOss(FileInfo fileInfo) {
|
||||
try {
|
||||
String path = fileInfo.getPath();
|
||||
if (path != null && !path.isEmpty()) {
|
||||
// 一键成片可能保存完整 OSS URL,这里去掉域名前缀转为相对路径
|
||||
String normalized = path.startsWith(OSS_DOMAIN_PREFIX) ? path.substring(OSS_DOMAIN_PREFIX.length()) : path;
|
||||
return ossService.getDownloadUrl(normalized);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("从OSS查询失败: fileId={}, error={}", fileInfo.getId(), e.getMessage());
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从S3查询文件URL
|
||||
*/
|
||||
@@ -361,6 +424,33 @@ public class TimeWindowStorageService {
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从OSS查询文件URL
|
||||
*/
|
||||
private String queryDownLoadFromS3(FileInfo fileInfo) {
|
||||
try {
|
||||
String path = fileInfo.getPath();
|
||||
if (path != null && !path.isEmpty()) {
|
||||
// 一键成片可能保存完整 OSS URL,这里去掉域名前缀转为相对路径
|
||||
final String OSS_DOMAIN_PREFIX = "https://goflyapp.oss-cn-beijing.aliyuncs.com/";
|
||||
String normalized = path.startsWith(OSS_DOMAIN_PREFIX) ? path.substring(OSS_DOMAIN_PREFIX.length()) : path;
|
||||
if(s3Service != null) {
|
||||
log.debug("使用S3Service查询: fileId={}, path={}", fileInfo.getId(), normalized);
|
||||
String url = s3Service.getDownloadUrl(normalized);
|
||||
log.info("从s3获取到的路径为:{}",url);
|
||||
return url;
|
||||
} else {
|
||||
log.warn("S3服务未配置,无法查询S3: fileId={}", fileInfo.getId());
|
||||
return "";
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.warn("从S3查询失败: fileId={}, error={}", fileInfo.getId(), e.getMessage());
|
||||
return "";
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* 从S3查询文件URL(通过路径)
|
||||
*/
|
||||
|
||||
+2
@@ -111,6 +111,8 @@ public abstract class AbstractIFileService extends ServiceImpl<FileMapper, FileI
|
||||
|
||||
public abstract String getViewUrlByPath(String name);
|
||||
|
||||
public abstract String getDownloadUrl(String path);
|
||||
|
||||
@Override
|
||||
public FileInfo getFileInfo(String id) {
|
||||
return baseMapper.selectById(id);
|
||||
|
||||
@@ -55,6 +55,11 @@ public class CosService extends AbstractIFileService {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZipViewUrlByPath(String name, String style) {
|
||||
return "";
|
||||
|
||||
+5
@@ -89,6 +89,11 @@ public class EnhancedS3Service extends AbstractIFileService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZipViewUrlByPath(String name, String style) {
|
||||
// 实现压缩文件URL获取逻辑
|
||||
|
||||
+5
@@ -51,6 +51,11 @@ public class FastdfsService extends AbstractIFileService {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return "";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZipViewUrlByPath(String name, String style) {
|
||||
return "";
|
||||
|
||||
@@ -86,6 +86,11 @@ public class OssService extends AbstractIFileService {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return ossTemplate.getDownloadUrl(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZipViewUrlByPath(String path, String style) {
|
||||
return ossTemplate.getViewUrl(path,style);
|
||||
|
||||
@@ -68,6 +68,11 @@ public class S3Service extends AbstractIFileService {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDownloadUrl(String path) {
|
||||
return s3Template.getDownloadUrl(path);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getZipViewUrlByPath(String name, String style) {
|
||||
try {
|
||||
|
||||
+12
-12
@@ -26,12 +26,8 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.URL;
|
||||
import java.net.URLConnection;
|
||||
import java.net.URLEncoder;
|
||||
@@ -601,26 +597,20 @@ public class OrderFileController {
|
||||
public ResponseEntity<StreamingResponseBody> downloadFile(@RequestBody FileDownloadRequest request) throws IOException {
|
||||
String fileUrl = request.getUrl();
|
||||
log.info("📥 开始代理下载文件: {}", fileUrl);
|
||||
|
||||
if (fileUrl == null || fileUrl.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("文件URL不能为空");
|
||||
}
|
||||
|
||||
// 从外部 CDN 下载文件
|
||||
URL url = new URL(fileUrl);
|
||||
URLConnection connection = url.openConnection();
|
||||
|
||||
// 设置超时和默认User-Agent
|
||||
connection.setConnectTimeout(10000); // 10秒连接超时
|
||||
connection.setReadTimeout(300000); // 5分钟读取超时,避免网络差时频繁超时
|
||||
connection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64)");
|
||||
|
||||
// 获取文件大小
|
||||
long fileSize = connection.getContentLengthLong();
|
||||
String fileName = extractFileNameFromUrl(fileUrl);
|
||||
|
||||
log.info("📊 文件信息: 名称={}, 大小={}MB", fileName, fileSize / (1024 * 1024));
|
||||
|
||||
// 构建响应头
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
|
||||
@@ -656,7 +646,6 @@ public class OrderFileController {
|
||||
throw e;
|
||||
}
|
||||
};
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.body(streamingBody);
|
||||
@@ -680,7 +669,6 @@ public class OrderFileController {
|
||||
if (orderNo == null || orderNo.trim().isEmpty()) {
|
||||
throw new IllegalArgumentException("订单编号不能为空");
|
||||
}
|
||||
|
||||
try {
|
||||
// 查询订单所有文件
|
||||
List<OrderFile> orderFiles = orderFileService.getOrderFileByOrderNo(orderNo);
|
||||
@@ -817,4 +805,16 @@ public class OrderFileController {
|
||||
}
|
||||
}
|
||||
|
||||
@Operation(summary = "根据文件id获取下载链接")
|
||||
@GetMapping("/file/download/{fileId}")
|
||||
public String getFileDownLoadUrl(@PathVariable("fileId") String fileId) {
|
||||
if(fileId==null && fileId==""){
|
||||
return "";
|
||||
}
|
||||
|
||||
String url= fileCenterService.getFileDownLoadUrl(fileId);
|
||||
log.info("返回的下载url为{}",url);
|
||||
return url;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -70,4 +70,8 @@ public interface FileCenterService {
|
||||
Result<Boolean> batchPrefetchFiles(@RequestBody List<String> urls,
|
||||
@RequestParam(value = "batchSize", defaultValue = "100") int batchSize,
|
||||
@RequestParam(value = "area", required = false) String area);
|
||||
|
||||
|
||||
@GetMapping("/file/download/{id}")
|
||||
String getFileDownLoadUrl(@PathVariable("id") String id);
|
||||
}
|
||||
|
||||
+5
@@ -73,6 +73,11 @@ public class FileCenterServiceFallbackFactory implements FallbackFactory<FileCen
|
||||
return Result.failed("CDN批量预热文件失败: " + cause.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFileDownLoadUrl(String id) {
|
||||
return "";
|
||||
}
|
||||
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user