fix(aiot): snap端点改用302重定向到COS公共URL
<img>标签请求snap时,直接302重定向到COS公共URL, 浏览器自动跟随重定向加载图片。用sendRedirect绕过 GlobalResponseAdvice的WVPResult包装,避免byte[]类型转换错误。 同时GlobalResponseAdvice增加byte[]排除以防其他场景。 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -15,6 +15,8 @@ import org.springframework.http.MediaType;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -88,22 +90,21 @@ public class AiRoiController {
|
||||
public ResponseEntity<?> getSnap(
|
||||
@RequestParam String cameraCode,
|
||||
@RequestParam(defaultValue = "false") boolean force,
|
||||
@RequestHeader(value = "Accept", defaultValue = "application/json") String accept) {
|
||||
@RequestHeader(value = "Accept", defaultValue = "application/json") String accept,
|
||||
HttpServletResponse httpResponse) throws IOException {
|
||||
|
||||
Map<String, Object> result = screenshotService.requestScreenshot(cameraCode, force);
|
||||
|
||||
// 如果是 <img> 标签发起的请求(Accept: image/*),直接返回图片字节
|
||||
// <img> 标签请求(Accept: image/*):302 重定向到 COS 公共 URL
|
||||
if (accept.contains("image/") && "ok".equals(result.get("status"))) {
|
||||
byte[] image = screenshotService.proxyScreenshotImage(cameraCode);
|
||||
if (image != null) {
|
||||
return ResponseEntity.ok()
|
||||
.contentType(MediaType.IMAGE_JPEG)
|
||||
.header("Cache-Control", "max-age=60")
|
||||
.body(image);
|
||||
String cosUrl = (String) result.get("url");
|
||||
if (cosUrl != null && cosUrl.startsWith("https://")) {
|
||||
httpResponse.sendRedirect(cosUrl);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 默认返回 JSON(XHR/fetch 请求)
|
||||
// JSON 请求(XHR/fetch):返回 JSON
|
||||
return ResponseEntity.ok(result);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user