Refactor PrestashopClient to construct absolute URL for image uploads and improve logging

This commit is contained in:
Vincent Guillet
2025-12-03 14:36:40 +01:00
parent fa7a1c2f26
commit 389beca604

View File

@@ -15,6 +15,7 @@ import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.util.UriComponentsBuilder;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
@@ -240,20 +241,22 @@ public class PrestashopClient {
MultipartFile imageFile
) {
try {
// construction de l'URL relative (le RestClient a déjà baseUrl)
StringBuilder pathBuilder = new StringBuilder("/images/products/")
// On construit l'URL complète (absolue)
StringBuilder urlBuilder = new StringBuilder(baseUrl)
.append("/images/products/")
.append(productId);
if (rawQuery != null && !rawQuery.isBlank()) {
pathBuilder.append('?').append(rawQuery);
urlBuilder.append('?').append(rawQuery);
}
String path = pathBuilder.toString();
String url = urlBuilder.toString();
byte[] bytes = imageFile.getBytes();
log.info("[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})",
baseUrl + path, bytes.length, imageFile.getContentType());
url, bytes.length, imageFile.getContentType());
// Ressource avec un filename pour que Spring pose bien le Content-Disposition
ByteArrayResource fileResource = new ByteArrayResource(bytes) {
@Override
public String getFilename() {
@@ -262,13 +265,12 @@ public class PrestashopClient {
}
};
// corps multipart : clé -> "image"
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
// *** clé "image" obligatoire pour Presta ***
body.add("image", fileResource);
// Envoi de la requête multipart/form-data
return client.post()
.uri(path)
.uri(URI.create(url)) // <-- URL absolue, plus de scheme undefined
.contentType(MediaType.MULTIPART_FORM_DATA)
.body(body)
.retrieve()