diff --git a/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java b/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java index 983eae3..a5010b7 100644 --- a/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java +++ b/api/src/main/java/fr/gameovergne/api/service/prestashop/PrestashopClient.java @@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.io.ByteArrayResource; import org.springframework.http.*; +import org.springframework.http.client.MultipartBodyBuilder; import org.springframework.stereotype.Service; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; @@ -241,7 +242,7 @@ public class PrestashopClient { MultipartFile imageFile ) { try { - // On construit l'URL complète (absolue) + // URL absolue StringBuilder urlBuilder = new StringBuilder(baseUrl) .append("/images/products/") .append(productId); @@ -253,26 +254,34 @@ public class PrestashopClient { String url = urlBuilder.toString(); byte[] bytes = imageFile.getBytes(); + String contentType = imageFile.getContentType(); + if (contentType == null || contentType.isBlank()) { + contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE; + } - log.info("[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})", - url, bytes.length, imageFile.getContentType()); + String filename = imageFile.getOriginalFilename(); + if (filename == null || filename.isBlank()) { + filename = "image.jpg"; + } - ByteArrayResource fileResource = new ByteArrayResource(bytes) { - @Override - public String getFilename() { - String name = imageFile.getOriginalFilename(); - return (name != null && !name.isBlank()) ? name : "image.jpg"; - } - }; + log.info( + "[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})", + url, bytes.length, contentType + ); - MultiValueMap body = new LinkedMultiValueMap<>(); - // *** clé "image" obligatoire pour Presta *** - body.add("image", fileResource); + // Construction propre du multipart avec un champ `image` fichier + MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder(); + bodyBuilder + .part("image", bytes) + .filename(filename) + .contentType(MediaType.parseMediaType(contentType)); + + MultiValueMap> multipartBody = bodyBuilder.build(); return client.post() - .uri(URI.create(url)) // <-- URL absolue, plus de scheme undefined + .uri(URI.create(url)) .contentType(MediaType.MULTIPART_FORM_DATA) - .body(body) + .body(multipartBody) .retrieve() .toEntity(String.class);