Refactor PrestashopClient to improve multipart image upload construction and enhance header management

This commit is contained in:
Vincent Guillet
2025-12-03 14:53:39 +01:00
parent f975e57110
commit 48f7e84ef9

View File

@@ -242,7 +242,7 @@ public class PrestashopClient {
MultipartFile imageFile
) {
try {
// URL absolue
// Construire lURL complète
StringBuilder urlBuilder = new StringBuilder(baseUrl)
.append("/images/products/")
.append(productId);
@@ -269,19 +269,21 @@ public class PrestashopClient {
url, bytes.length, contentType
);
// Construction propre du multipart avec un champ `image` fichier
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
bodyBuilder
.part("image", bytes)
.filename(filename)
.contentType(MediaType.parseMediaType(contentType));
// ----- PARTIE FICHIER "image" -----
HttpHeaders partHeaders = new HttpHeaders();
partHeaders.setContentType(MediaType.parseMediaType(contentType));
// Très important : filename + name = "image" pour que Presta le voie dans $_FILES['image']
partHeaders.setContentDispositionFormData("image", filename);
MultiValueMap<String, HttpEntity<?>> multipartBody = bodyBuilder.build();
HttpEntity<byte[]> imagePart = new HttpEntity<>(bytes, partHeaders);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("image", imagePart);
return client.post()
.uri(URI.create(url))
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(multipartBody)
.body(body)
.retrieve()
.toEntity(String.class);