From 746e94983b4f8e71ddde7cb56d3838051de5e7a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bal=C3=A1zs=20Sz=C3=BCcs?= <127139797+balazs-szucs@users.noreply.github.com> Date: Mon, 24 Nov 2025 15:18:26 +0100 Subject: [PATCH] feat(file-service): implement image downloading using RestTemplate with better headers and error handling (#1605) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(file-service): implement image downloading using RestTemplate with better headers and error handling Signed-off-by: Balázs Szücs * feat(config): enhance RestTemplate with custom connection and read timeouts Signed-off-by: Balázs Szücs --------- Signed-off-by: Balázs Szücs --- .../booklore/config/BeanConfig.java | 11 ++++++ .../booklore/util/FileService.java | 38 +++++++++++++++---- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/config/BeanConfig.java b/booklore-api/src/main/java/com/adityachandel/booklore/config/BeanConfig.java index 676c38ed1..971a99a41 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/config/BeanConfig.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/config/BeanConfig.java @@ -2,15 +2,26 @@ package com.adityachandel.booklore.config; import jakarta.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.web.client.RestTemplateBuilder; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import org.springframework.web.client.RestTemplate; import org.springframework.web.socket.config.WebSocketMessageBrokerStats; +import java.time.Duration; + @Configuration public class BeanConfig { @Autowired private WebSocketMessageBrokerStats webSocketMessageBrokerStats; + @Bean + public RestTemplate restTemplate(RestTemplateBuilder builder) { + return builder.connectTimeout(Duration.ofSeconds(10)).readTimeout(Duration.ofSeconds(15)) + .build(); + } + @PostConstruct public void init() { webSocketMessageBrokerStats.setLoggingPeriod(30 * 24 * 60 * 60 * 1000L); // 30 days diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/util/FileService.java b/booklore-api/src/main/java/com/adityachandel/booklore/util/FileService.java index a9e9584e9..f453d2826 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/util/FileService.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/util/FileService.java @@ -17,7 +17,12 @@ import org.apache.commons.lang3.StringUtils; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; import org.springframework.web.multipart.MultipartFile; import javax.imageio.ImageIO; @@ -26,8 +31,6 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; -import java.net.URI; -import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -44,6 +47,7 @@ import java.util.stream.Stream; public class FileService { private final AppProperties appProperties; + private final RestTemplate restTemplate; // @formatter:off private static final String IMAGES_DIR = "images"; @@ -155,13 +159,31 @@ public class FileService { public BufferedImage downloadImageFromUrl(String imageUrl) throws IOException { try { - URI uri = URI.create(imageUrl); - URL url = uri.toURL(); - BufferedImage image = ImageIO.read(url); - if (image == null) { - throw new IOException("Unable to read image from URL: " + imageUrl); + HttpHeaders headers = new HttpHeaders(); + headers.set(HttpHeaders.USER_AGENT, "BookLore/1.0 (Metadata Fetcher)"); + headers.set(HttpHeaders.ACCEPT, "image/*"); + + HttpEntity entity = new HttpEntity<>(headers); + + ResponseEntity response = restTemplate.exchange( + imageUrl, + HttpMethod.GET, + entity, + byte[].class + ); + + // Validate and convert + if (response.getStatusCode().is2xxSuccessful() && response.getBody() != null) { + try (ByteArrayInputStream inputStream = new ByteArrayInputStream(response.getBody())) { + BufferedImage image = ImageIO.read(inputStream); + if (image == null) { + throw new IOException("Downloaded content is not a supported image format."); + } + return image; + } + } else { + throw new IOException("Failed to download image. HTTP Status: " + response.getStatusCode()); } - return image; } catch (Exception e) { log.error("Failed to download image from URL: {} - {}", imageUrl, e.getMessage()); throw new IOException("Failed to download image from URL: " + imageUrl, e);