Refactor PrestashopClient to improve multipart image upload handling and logging
This commit is contained in:
@@ -4,6 +4,7 @@ import lombok.extern.slf4j.Slf4j;
|
|||||||
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
import org.springframework.core.io.ByteArrayResource;
|
import org.springframework.core.io.ByteArrayResource;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
|
import org.springframework.http.client.MultipartBodyBuilder;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.LinkedMultiValueMap;
|
import org.springframework.util.LinkedMultiValueMap;
|
||||||
import org.springframework.util.MultiValueMap;
|
import org.springframework.util.MultiValueMap;
|
||||||
@@ -241,7 +242,7 @@ public class PrestashopClient {
|
|||||||
MultipartFile imageFile
|
MultipartFile imageFile
|
||||||
) {
|
) {
|
||||||
try {
|
try {
|
||||||
// On construit l'URL complète (absolue)
|
// URL absolue
|
||||||
StringBuilder urlBuilder = new StringBuilder(baseUrl)
|
StringBuilder urlBuilder = new StringBuilder(baseUrl)
|
||||||
.append("/images/products/")
|
.append("/images/products/")
|
||||||
.append(productId);
|
.append(productId);
|
||||||
@@ -253,26 +254,34 @@ public class PrestashopClient {
|
|||||||
String url = urlBuilder.toString();
|
String url = urlBuilder.toString();
|
||||||
|
|
||||||
byte[] bytes = imageFile.getBytes();
|
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={})",
|
String filename = imageFile.getOriginalFilename();
|
||||||
url, bytes.length, imageFile.getContentType());
|
if (filename == null || filename.isBlank()) {
|
||||||
|
filename = "image.jpg";
|
||||||
|
}
|
||||||
|
|
||||||
ByteArrayResource fileResource = new ByteArrayResource(bytes) {
|
log.info(
|
||||||
@Override
|
"[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})",
|
||||||
public String getFilename() {
|
url, bytes.length, contentType
|
||||||
String name = imageFile.getOriginalFilename();
|
);
|
||||||
return (name != null && !name.isBlank()) ? name : "image.jpg";
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
|
// Construction propre du multipart avec un champ `image` fichier
|
||||||
// *** clé "image" obligatoire pour Presta ***
|
MultipartBodyBuilder bodyBuilder = new MultipartBodyBuilder();
|
||||||
body.add("image", fileResource);
|
bodyBuilder
|
||||||
|
.part("image", bytes)
|
||||||
|
.filename(filename)
|
||||||
|
.contentType(MediaType.parseMediaType(contentType));
|
||||||
|
|
||||||
|
MultiValueMap<String, HttpEntity<?>> multipartBody = bodyBuilder.build();
|
||||||
|
|
||||||
return client.post()
|
return client.post()
|
||||||
.uri(URI.create(url)) // <-- URL absolue, plus de scheme undefined
|
.uri(URI.create(url))
|
||||||
.contentType(MediaType.MULTIPART_FORM_DATA)
|
.contentType(MediaType.MULTIPART_FORM_DATA)
|
||||||
.body(body)
|
.body(multipartBody)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.toEntity(String.class);
|
.toEntity(String.class);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user