Refactor PrestashopClient to enhance image upload logging and improve filename handling

This commit is contained in:
Vincent Guillet
2025-12-03 15:06:53 +01:00
parent 48f7e84ef9
commit 078cef0585

View File

@@ -242,7 +242,7 @@ public class PrestashopClient {
MultipartFile imageFile MultipartFile imageFile
) { ) {
try { try {
// Construire lURL complète // Construire lURL Presta
StringBuilder urlBuilder = new StringBuilder(baseUrl) StringBuilder urlBuilder = new StringBuilder(baseUrl)
.append("/images/products/") .append("/images/products/")
.append(productId); .append(productId);
@@ -250,35 +250,42 @@ public class PrestashopClient {
if (rawQuery != null && !rawQuery.isBlank()) { if (rawQuery != null && !rawQuery.isBlank()) {
urlBuilder.append('?').append(rawQuery); urlBuilder.append('?').append(rawQuery);
} }
String url = urlBuilder.toString(); String url = urlBuilder.toString();
byte[] bytes = imageFile.getBytes(); byte[] bytes = imageFile.getBytes();
String originalFilename = imageFile.getOriginalFilename();
if (originalFilename == null || originalFilename.isBlank()) {
originalFilename = "image.jpg";
}
String contentType = imageFile.getContentType(); String contentType = imageFile.getContentType();
if (contentType == null || contentType.isBlank()) { if (contentType == null || contentType.isBlank()) {
contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE; contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
} }
String filename = imageFile.getOriginalFilename();
if (filename == null || filename.isBlank()) {
filename = "image.jpg";
}
log.info( log.info(
"[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})", "[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={}, filename={})",
url, bytes.length, contentType url, bytes.length, contentType, originalFilename
); );
// ----- PARTIE FICHIER "image" ----- // Resource avec filename pour que PHP le traite comme un fichier dans $_FILES["image"]
HttpHeaders partHeaders = new HttpHeaders(); String finalOriginalFilename = originalFilename;
partHeaders.setContentType(MediaType.parseMediaType(contentType)); ByteArrayResource imageResource = new ByteArrayResource(bytes) {
// Très important : filename + name = "image" pour que Presta le voie dans $_FILES['image'] @Override
partHeaders.setContentDispositionFormData("image", filename); public String getFilename() {
return finalOriginalFilename;
}
HttpEntity<byte[]> imagePart = new HttpEntity<>(bytes, partHeaders); @Override
public long contentLength() {
return bytes.length;
}
};
// body multipart : la clé "image" => part nommée "image"
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>(); MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("image", imagePart); body.add("image", imageResource);
return client.post() return client.post()
.uri(URI.create(url)) .uri(URI.create(url))