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 2ebd5a1..ed9605b 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
@@ -2,10 +2,12 @@ package fr.gameovergne.api.service.prestashop;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
+import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClient;
import org.springframework.web.client.RestClientException;
@@ -229,56 +231,50 @@ public class PrestashopClient {
/**
* Upload d'une image produit vers PrestaShop :
* POST /api/images/products/{productId}
- *
+ *
* On envoie directement les bytes du fichier avec le bon content-type (image/jpeg, image/png, ...).
*/
- public ResponseEntity uploadProductImage(String productId, String rawQuery, MultipartFile imageFile) {
- String uri = baseUrl + "/images/products/" + productId;
- if (rawQuery != null && !rawQuery.isBlank()) {
- uri = uri + "?" + rawQuery;
- }
-
- // Content-Type de l'image : on essaie de respecter celui fourni par le client,
- // sinon on force sur du JPEG par défaut.
- String ct = imageFile.getContentType();
- MediaType mediaType = (ct != null && !ct.isBlank())
- ? MediaType.parseMediaType(ct)
- : MediaType.IMAGE_JPEG;
-
- log.info("[PrestaShop] POST (image) {} (size={} bytes, contentType={})",
- uri, imageFile.getSize(), mediaType);
+ public ResponseEntity uploadProductImage(String productId,
+ String rawQuery,
+ MultipartFile imageFile) {
try {
- byte[] bytes = imageFile.getBytes();
+ StringBuilder url = new StringBuilder(baseUrl)
+ .append("/images/products/")
+ .append(productId);
- return client.post()
- .uri(uri)
- .contentType(mediaType)
- .body(bytes)
+ if (rawQuery != null && !rawQuery.isBlank()) {
+ url.append("?").append(rawQuery);
+ }
+
+ // Corps multipart avec un champ "image" comme demandé par PrestaShop
+ MultiValueMap body = new LinkedMultiValueMap<>();
+
+ ByteArrayResource imageResource = new ByteArrayResource(imageFile.getBytes()) {
+ @Override
+ public String getFilename() {
+ // PrestaShop veut un filename, même si ce n'est pas critique
+ return imageFile.getOriginalFilename() != null
+ ? imageFile.getOriginalFilename()
+ : "image.jpg";
+ }
+ };
+
+ body.add("image", imageResource); // 👈 IMPORTANT : le nom du champ = "image"
+
+ log.info("[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})",
+ url, imageFile.getSize(), imageFile.getContentType());
+
+ return client
+ .post()
+ .uri(url.toString())
+ .contentType(MediaType.MULTIPART_FORM_DATA)
+ .body(body)
.retrieve()
.toEntity(String.class);
- } catch (RestClientResponseException ex) {
- // Erreur fonctionnelle renvoyée par Presta (taille, format, etc.)
- log.error("[PrestaShop] POST image error {} : {}", ex.getRawStatusCode(), ex.getResponseBodyAsString());
- return ResponseEntity
- .status(ex.getRawStatusCode())
- .contentType(MediaType.APPLICATION_XML)
- .body(ex.getResponseBodyAsString());
- } catch (IOException ex) {
- // Problème de lecture du fichier envoyé depuis le client
- log.error("[PrestaShop] POST image, erreur lecture fichier", ex);
- return ResponseEntity
- .status(400)
- .contentType(MediaType.TEXT_PLAIN)
- .body("Invalid image file");
- } catch (RestClientException ex) {
- // Cas réseau / technique
- log.error("[PrestaShop] POST image technical error", ex);
- return ResponseEntity
- .status(502)
- .contentType(MediaType.TEXT_PLAIN)
- .body("Error while calling PrestaShop WebService");
+ } catch (IOException e) {
+ throw new RuntimeException("Erreur lors de la lecture du fichier image", e);
}
}
}
\ No newline at end of file