Refactor PrestashopClient to enhance image upload logging and improve filename handling
This commit is contained in:
@@ -242,7 +242,7 @@ public class PrestashopClient {
|
||||
MultipartFile imageFile
|
||||
) {
|
||||
try {
|
||||
// Construire l’URL complète
|
||||
// Construire l’URL Presta
|
||||
StringBuilder urlBuilder = new StringBuilder(baseUrl)
|
||||
.append("/images/products/")
|
||||
.append(productId);
|
||||
@@ -250,35 +250,42 @@ public class PrestashopClient {
|
||||
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||
urlBuilder.append('?').append(rawQuery);
|
||||
}
|
||||
|
||||
String url = urlBuilder.toString();
|
||||
|
||||
byte[] bytes = imageFile.getBytes();
|
||||
|
||||
String originalFilename = imageFile.getOriginalFilename();
|
||||
if (originalFilename == null || originalFilename.isBlank()) {
|
||||
originalFilename = "image.jpg";
|
||||
}
|
||||
|
||||
String contentType = imageFile.getContentType();
|
||||
if (contentType == null || contentType.isBlank()) {
|
||||
contentType = MediaType.APPLICATION_OCTET_STREAM_VALUE;
|
||||
}
|
||||
|
||||
String filename = imageFile.getOriginalFilename();
|
||||
if (filename == null || filename.isBlank()) {
|
||||
filename = "image.jpg";
|
||||
}
|
||||
|
||||
log.info(
|
||||
"[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={})",
|
||||
url, bytes.length, contentType
|
||||
"[PrestaShop] POST (image multipart) {} (size={} bytes, contentType={}, filename={})",
|
||||
url, bytes.length, contentType, originalFilename
|
||||
);
|
||||
|
||||
// ----- 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);
|
||||
// Resource avec filename pour que PHP le traite comme un fichier dans $_FILES["image"]
|
||||
String finalOriginalFilename = originalFilename;
|
||||
ByteArrayResource imageResource = new ByteArrayResource(bytes) {
|
||||
@Override
|
||||
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<>();
|
||||
body.add("image", imagePart);
|
||||
body.add("image", imageResource);
|
||||
|
||||
return client.post()
|
||||
.uri(URI.create(url))
|
||||
|
||||
Reference in New Issue
Block a user