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.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.core.io.buffer.DefaultDataBufferFactory;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.MediaType;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.stereotype.Service;
@@ -203,35 +201,50 @@ public class FacebookPostingService {
throws JsonProcessingException {
try {
// Создаем multipart form data для загрузки изображения
DataBufferFactory bufferFactory = new DefaultDataBufferFactory();
DataBuffer imageBuffer = bufferFactory.wrap(imageData);
ByteArrayResource imageResource = new ByteArrayResource(imageData) {
@Override
public String getFilename() {
return "image.jpg";
}
};
MultiValueMap<String, Object> formData = new LinkedMultiValueMap<>();
formData.add("message", message);
formData.add("source", imageBuffer);
formData.add("source", imageResource);
String response = webClient.post()
.uri(uriBuilder -> uriBuilder
.path("/{pageId}/photos")
.queryParam("access_token", accessToken)
.build(pageId))
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.retrieve()
.bodyToMono(String.class)
.block(Duration.ofMillis(timeoutMs * 2)); // Увеличиваем таймаут для загрузки изображения
String response;
try {
response = webClient.post()
.uri(uriBuilder -> uriBuilder
.path("/{pageId}/photos")
.queryParam("access_token", accessToken)
.build(pageId))
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(BodyInserters.fromMultipartData(formData))
.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);
String photoId = jsonNode.get("id").asText();
// Возвращаем ID фото, который также является ID поста
return photoId;
} catch (WebClientResponseException e) {
if (isTokenExpiredError(e)) {
throw parseFacebookError(e);
}
logger.error("Facebook API error: {} - {}", e.getStatusCode(), e.getResponseBodyAsString());
throw new RuntimeException("Failed to publish post with image: " + e.getMessage(), e);
} catch (JsonProcessingException e) {
logger.error("Failed to parse Facebook response", e);
throw e;
} catch (Exception e) {
logger.error("Unexpected error publishing post with image", e);
throw new RuntimeException("Failed to publish post with image", e);
}
}