From cb66f9f7e1bd25914ac2cc00479a5128dc08f8c4 Mon Sep 17 00:00:00 2001 From: duzhichao <1098974448@qq.com> Date: Thu, 6 Nov 2025 17:05:33 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=B5=84=E6=BA=90=E5=88=97=E8=A1=A8?= =?UTF-8?q?=E6=89=B9=E9=87=8F=E4=B8=8B=E8=BD=BD=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../file/controller/FileController.java | 5 + .../service/TimeWindowStorageService.java | 90 ++++++ .../service/impl/AbstractIFileService.java | 2 + .../central/file/service/impl/CosService.java | 5 + .../file/service/impl/EnhancedS3Service.java | 5 + .../file/service/impl/FastdfsService.java | 5 + .../central/file/service/impl/OssService.java | 5 + .../central/file/service/impl/S3Service.java | 5 + .../order/controller/OrderFileController.java | 24 +- .../order/feign/FileCenterService.java | 4 + .../FileCenterServiceFallbackFactory.java | 5 + .../com/central/oss/template/OssTemplate.java | 67 ++++- .../com/central/oss/template/S3Template.java | 68 ++++- .../Order/components/OrderResourcesModal.tsx | 257 ++++++------------ zlt-web/react-web-next/src/services/orders.ts | 6 + 15 files changed, 365 insertions(+), 188 deletions(-) diff --git a/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java b/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java index c8a169fa..0d82f0b7 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java +++ b/zlt-business/file-center/src/main/java/com/central/file/controller/FileController.java @@ -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 包装) */ diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/TimeWindowStorageService.java b/zlt-business/file-center/src/main/java/com/central/file/service/TimeWindowStorageService.java index 5097703a..e380e428 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/TimeWindowStorageService.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/TimeWindowStorageService.java @@ -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(通过路径) */ diff --git a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java index d38be674..9b92dece 100644 --- a/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java +++ b/zlt-business/file-center/src/main/java/com/central/file/service/impl/AbstractIFileService.java @@ -111,6 +111,8 @@ public abstract class AbstractIFileService extends ServiceImpl 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 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; + } + } diff --git a/zlt-business/order-center/src/main/java/com/central/order/feign/FileCenterService.java b/zlt-business/order-center/src/main/java/com/central/order/feign/FileCenterService.java index 07e3ec81..e39d22f2 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/feign/FileCenterService.java +++ b/zlt-business/order-center/src/main/java/com/central/order/feign/FileCenterService.java @@ -70,4 +70,8 @@ public interface FileCenterService { Result batchPrefetchFiles(@RequestBody List 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); } diff --git a/zlt-business/order-center/src/main/java/com/central/order/feign/fallback/FileCenterServiceFallbackFactory.java b/zlt-business/order-center/src/main/java/com/central/order/feign/fallback/FileCenterServiceFallbackFactory.java index e5f8f66a..36b3bb6f 100644 --- a/zlt-business/order-center/src/main/java/com/central/order/feign/fallback/FileCenterServiceFallbackFactory.java +++ b/zlt-business/order-center/src/main/java/com/central/order/feign/fallback/FileCenterServiceFallbackFactory.java @@ -73,6 +73,11 @@ public class FileCenterServiceFallbackFactory implements FallbackFactory