fix(下载): 各模块结果下载由流式代理改 302 重定向到对象存储直链——原实现在 Controller 内边读边写代理整个文件,请求线程被下载时长占满;且恒回 200 全量,客户端 Range 断点续传实际失效
覆盖 brand(/tasks/{taskId}/download)、delete-brand(任务 + 结果两处)、appearance-patent、
patrol-delete、price-track、withdraw、query-asin、product-risk、shop-match、shop-data-crawl
共 10 个 Controller,与既有的 convert/dedupe/split 302 实现对齐(ResponseEntity.status(FOUND)
.location(直链) + Cache-Control no-store),并补重定向 info 日志便于排查。
前端各 tab 本就通过 saveUrlWithProgress(url, filename) 传入文件名与下载 id,不依赖
Content-Disposition,故无需前端改动即可同时获得断点续传与进度回调。
验证:两节点重启后逐个实测 11 个端点均 302(含 brand 任务下载与巡店/跟价的有文件记录);
无文件时保持既有业务错误响应(HTTP 200 + success:false)。
This commit is contained in:
+14
-21
@@ -1,7 +1,6 @@
|
||||
package com.nanri.aiimage.modules.queryasin.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.util.DownloadHeaderUtil;
|
||||
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskCandidateAddRequest;
|
||||
import com.nanri.aiimage.modules.productrisk.model.dto.ProductRiskMatchShopsRequest;
|
||||
import com.nanri.aiimage.modules.productrisk.model.vo.ProductRiskCandidateVo;
|
||||
@@ -24,7 +23,10 @@ import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -35,7 +37,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.server.ResponseStatusException;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.List;
|
||||
@@ -44,6 +45,7 @@ import com.nanri.aiimage.modules.task.model.vo.TaskProgressLightBatchVo;
|
||||
|
||||
@RestController
|
||||
@RequiredArgsConstructor
|
||||
@Slf4j
|
||||
@RequestMapping("/api/query-asin")
|
||||
@Tag(
|
||||
name = "查询ASIN",
|
||||
@@ -265,32 +267,23 @@ public class QueryAsinTaskController {
|
||||
}
|
||||
|
||||
@GetMapping("/results/{resultId}/download")
|
||||
@Operation(summary = "下载结果文件", description = "按结果记录下载服务端已生成并上传到 OSS 的查询 ASIN Excel 文件。")
|
||||
public void downloadResult(
|
||||
@Operation(summary = "下载结果文件", description = "按结果记录 302 重定向到服务端已生成并上传到 OSS 的查询 ASIN Excel 直链。")
|
||||
public ResponseEntity<Void> downloadResult(
|
||||
@Parameter(description = "结果记录主键", example = "4599")
|
||||
@PathVariable Long resultId,
|
||||
@Parameter(name = "user_id", description = "当前用户 ID", required = true, in = ParameterIn.QUERY, example = "1")
|
||||
@RequestParam("user_id") Long userId,
|
||||
jakarta.servlet.http.HttpServletResponse response) {
|
||||
@RequestParam("user_id") Long userId) {
|
||||
String url = queryAsinTaskService.resolveResultDownloadUrl(resultId, userId);
|
||||
String filename = queryAsinTaskService.resolveResultDownloadFilename(resultId, userId);
|
||||
if (url == null || url.isBlank()) {
|
||||
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "暂无可下载结果");
|
||||
}
|
||||
try {
|
||||
response.setContentType("application/octet-stream");
|
||||
DownloadHeaderUtil.setAttachment(response, filename);
|
||||
try (InputStream in = URI.create(url).toURL().openStream()) {
|
||||
byte[] buffer = new byte[65536];
|
||||
int read;
|
||||
while ((read = in.read(buffer)) != -1) {
|
||||
response.getOutputStream().write(buffer, 0, read);
|
||||
}
|
||||
response.getOutputStream().flush();
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "下载失败");
|
||||
}
|
||||
// 结果文件可能较大:改为 302 重定向到对象存储直链,避免 Tomcat 请求线程被整个下载时长占满,
|
||||
// 并让客户端 Range 断点续传生效。
|
||||
log.info("[查询ASIN] 结果文件下载重定向 结果ID={} 用户ID={}", resultId, userId);
|
||||
return ResponseEntity.status(HttpStatus.FOUND)
|
||||
.location(URI.create(url))
|
||||
.header(HttpHeaders.CACHE_CONTROL, "no-store")
|
||||
.build();
|
||||
}
|
||||
|
||||
@DeleteMapping("/tasks/{taskId}")
|
||||
|
||||
Reference in New Issue
Block a user