后台更新 服务优化

This commit is contained in:
supernijia
2026-08-11 14:50:21 +08:00
parent 641c70d451
commit 91cd02eeb4
24 changed files with 1623 additions and 502 deletions
@@ -14,7 +14,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.InputStream;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
@@ -33,6 +35,8 @@ import java.util.Set;
@RequiredArgsConstructor
public class ImageVideoCozeService {
private static final int RESPONSE_LOG_PREVIEW_CHARS = 4096;
private static final List<String> ORIGINAL_FIELDS = List.of(
"recognizedContent", "recognized_content", "originalContent", "original_content",
"originContent", "origin_content", "rawContent", "raw_content", "description", "content"
@@ -447,7 +451,8 @@ public class ImageVideoCozeService {
} catch (BusinessException ex) {
throw ex;
} catch (Exception ex) {
log.warn("[image-video] parse douyin copy response failed body={}", responseText, ex);
log.warn("[image-video] parse douyin copy response failed bodyPreview={}",
responsePreview(responseText), ex);
throw new BusinessException("Coze 返回解析失败: " + ex.getMessage());
}
}
@@ -588,12 +593,12 @@ public class ImageVideoCozeService {
.header("Accept-Charset", StandardCharsets.UTF_8.name())
.POST(HttpRequest.BodyPublishers.ofString(requestBody, StandardCharsets.UTF_8))
.build();
HttpResponse<String> response = httpClient().send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
String responseBody = response.body() == null ? "" : response.body();
HttpResponse<InputStream> response = httpClient().send(request, HttpResponse.BodyHandlers.ofInputStream());
String responseBody = readResponseBody(response.body());
log.info("[image-video] {} coze response status={} bodyLength={}",
label, response.statusCode(), responseBody.length());
log.info("[image-video] {} coze response detail status={} body={}",
label, response.statusCode(), responseBody);
log.debug("[image-video] {} coze response detail status={} bodyPreview={}",
label, response.statusCode(), responsePreview(responseBody));
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new BusinessException("Coze 调用失败: HTTP " + response.statusCode());
}
@@ -618,12 +623,12 @@ public class ImageVideoCozeService {
.header("Accept-Charset", StandardCharsets.UTF_8.name())
.GET()
.build();
HttpResponse<String> response = httpClient().send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
String responseBody = response.body() == null ? "" : response.body();
HttpResponse<InputStream> response = httpClient().send(request, HttpResponse.BodyHandlers.ofInputStream());
String responseBody = readResponseBody(response.body());
log.info("[image-video] {} coze response status={} bodyLength={}",
label, response.statusCode(), responseBody.length());
log.info("[image-video] {} coze response detail status={} body={}",
label, response.statusCode(), responseBody);
log.debug("[image-video] {} coze response detail status={} bodyPreview={}",
label, response.statusCode(), responsePreview(responseBody));
if (response.statusCode() < 200 || response.statusCode() >= 300) {
throw new BusinessException("Coze 调用失败: HTTP " + response.statusCode());
}
@@ -638,6 +643,37 @@ public class ImageVideoCozeService {
}
}
private String readResponseBody(InputStream inputStream) throws java.io.IOException {
if (inputStream == null) {
return "";
}
int maxBytes = Math.max(1024, properties.getCozeResponseMaxBytes());
try (InputStream input = inputStream;
ByteArrayOutputStream output = new ByteArrayOutputStream(Math.min(maxBytes, 8192))) {
byte[] buffer = new byte[8192];
int total = 0;
int read;
while ((read = input.read(buffer)) != -1) {
if (read == 0) {
continue;
}
total += read;
if (total > maxBytes) {
throw new java.io.IOException("Coze response exceeds configured size limit");
}
output.write(buffer, 0, read);
}
return output.toString(StandardCharsets.UTF_8);
}
}
private String responsePreview(String responseBody) {
if (responseBody == null || responseBody.length() <= RESPONSE_LOG_PREVIEW_CHARS) {
return responseBody == null ? "" : responseBody;
}
return responseBody.substring(0, RESPONSE_LOG_PREVIEW_CHARS) + "...(truncated)";
}
private HttpClient httpClient() {
HttpClient client = sharedHttpClient;
if (client == null) {
@@ -706,11 +742,13 @@ public class ImageVideoCozeService {
}
private String toJsonForLog(Object value) {
String json;
try {
return objectMapper.writeValueAsString(maskSecretsForLog(value));
json = objectMapper.writeValueAsString(maskSecretsForLog(value));
} catch (Exception ex) {
return String.valueOf(value);
json = String.valueOf(value);
}
return responsePreview(json);
}
private Object maskSecretsForLog(Object value) {