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 {
|
||||
|
||||
Reference in New Issue
Block a user