This commit is contained in:
root
2025-12-06 21:47:35 +05:00
parent beee7d71a2
commit 1245a37c8f
@@ -7,9 +7,7 @@ import kz.konturai.parser.exception.FacebookTokenExpiredException;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.buffer.DataBuffer; import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector; import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@@ -203,35 +201,50 @@ public class FacebookPostingService {
throws JsonProcessingException { throws JsonProcessingException {
try { try {
// Создаем multipart form data для загрузки изображения // Создаем multipart form data для загрузки изображения
DataBufferFactory bufferFactory = new DefaultDataBufferFactory(); ByteArrayResource imageResource = new ByteArrayResource(imageData) {
DataBuffer imageBuffer = bufferFactory.wrap(imageData); @Override
public String getFilename() {
return "image.jpg";
}
};
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
formData.add("message", message); formData.add("message", message);
formData.add("source", imageBuffer); formData.add("source", imageResource);
String response = webClient.post() String response;
.uri(uriBuilder -> uriBuilder try {
.path("/{pageId}/photos") response = webClient.post()
.queryParam("access_token", accessToken) .uri(uriBuilder -> uriBuilder
.build(pageId)) .path("/{pageId}/photos")
.contentType(MediaType.MULTIPART_FORM_DATA) .queryParam("access_token", accessToken)
.body(BodyInserters.fromMultipartData(formData)) .build(pageId))
.retrieve() .contentType(MediaType.MULTIPART_FORM_DATA)
.bodyToMono(String.class) .body(BodyInserters.fromMultipartData(formData))
.block(Duration.ofMillis(timeoutMs * 2)); // Увеличиваем таймаут для загрузки изображения .retrieve()
.bodyToMono(String.class)
.block(Duration.ofMillis(timeoutMs * 2)); // Увеличиваем таймаут для загрузки изображения
} catch (WebClientResponseException e) {
String responseBody = e.getResponseBodyAsString();
logger.error("Facebook API error during image upload: Status {} - Response body: {}",
e.getStatusCode(), responseBody != null ? responseBody : "No response body");
if (isTokenExpiredError(e)) {
throw parseFacebookError(e);
}
throw new RuntimeException("Failed to publish post with image: " + e.getMessage(), e);
}
JsonNode jsonNode = objectMapper.readTree(response); JsonNode jsonNode = objectMapper.readTree(response);
String photoId = jsonNode.get("id").asText(); String photoId = jsonNode.get("id").asText();
// Возвращаем ID фото, который также является ID поста // Возвращаем ID фото, который также является ID поста
return photoId; return photoId;
} catch (WebClientResponseException e) { } catch (JsonProcessingException e) {
if (isTokenExpiredError(e)) { logger.error("Failed to parse Facebook response", e);
throw parseFacebookError(e); throw e;
} } catch (Exception e) {
logger.error("Facebook API error: {} - {}", e.getStatusCode(), e.getResponseBodyAsString()); logger.error("Unexpected error publishing post with image", e);
throw new RuntimeException("Failed to publish post with image: " + e.getMessage(), e); throw new RuntimeException("Failed to publish post with image", e);
} }
} }