Add image upload functionality to PrestashopClient and PrestashopProxyController
This commit is contained in:
@@ -8,6 +8,7 @@ import org.springframework.http.MediaType;
|
|||||||
import org.springframework.http.ResponseEntity;
|
import org.springframework.http.ResponseEntity;
|
||||||
import org.springframework.util.AntPathMatcher;
|
import org.springframework.util.AntPathMatcher;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.servlet.HandlerMapping;
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
|
||||||
import java.net.URLDecoder;
|
import java.net.URLDecoder;
|
||||||
@@ -115,4 +116,28 @@ public class PrestashopProxyController {
|
|||||||
.contentType(MediaType.APPLICATION_XML)
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
.body(prestaResponse.getBody());
|
.body(prestaResponse.getBody());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload d'une image produit :
|
||||||
|
* Front → (multipart/form-data) → /api/ps/images/products/{productId}
|
||||||
|
* Backend → (bytes) → https://shop.gameovergne.fr/api/images/products/{productId}
|
||||||
|
*/
|
||||||
|
@PostMapping(
|
||||||
|
path = "/api/ps/images/products/{productId}",
|
||||||
|
consumes = MediaType.MULTIPART_FORM_DATA_VALUE,
|
||||||
|
produces = MediaType.APPLICATION_XML_VALUE
|
||||||
|
)
|
||||||
|
public ResponseEntity<String> uploadProductImage(
|
||||||
|
jakarta.servlet.http.HttpServletRequest request,
|
||||||
|
@PathVariable("productId") String productId,
|
||||||
|
@RequestPart("image") MultipartFile imageFile
|
||||||
|
) {
|
||||||
|
String rawQuery = request.getQueryString(); // si jamais tu ajoutes des options côté Presta
|
||||||
|
|
||||||
|
log.info("[Proxy] Upload image produit {} (size={} bytes, ct={})",
|
||||||
|
productId, imageFile.getSize(), imageFile.getContentType());
|
||||||
|
|
||||||
|
return prestashopClient.uploadProductImage(productId, rawQuery, imageFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -10,8 +10,10 @@ import org.springframework.util.MultiValueMap;
|
|||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
import org.springframework.web.client.RestClientException;
|
import org.springframework.web.client.RestClientException;
|
||||||
import org.springframework.web.client.RestClientResponseException;
|
import org.springframework.web.client.RestClientResponseException;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Base64;
|
import java.util.Base64;
|
||||||
|
|
||||||
@@ -222,4 +224,61 @@ public class PrestashopClient {
|
|||||||
.body("Error while calling PrestaShop WebService");
|
.body("Error while calling PrestaShop WebService");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Upload d'une image produit vers PrestaShop :
|
||||||
|
* POST /api/images/products/{productId}
|
||||||
|
*
|
||||||
|
* On envoie directement les bytes du fichier avec le bon content-type (image/jpeg, image/png, ...).
|
||||||
|
*/
|
||||||
|
public ResponseEntity<String> uploadProductImage(String productId, String rawQuery, MultipartFile imageFile) {
|
||||||
|
String uri = baseUrl + "/images/products/" + productId;
|
||||||
|
if (rawQuery != null && !rawQuery.isBlank()) {
|
||||||
|
uri = uri + "?" + rawQuery;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Content-Type de l'image : on essaie de respecter celui fourni par le client,
|
||||||
|
// sinon on force sur du JPEG par défaut.
|
||||||
|
String ct = imageFile.getContentType();
|
||||||
|
MediaType mediaType = (ct != null && !ct.isBlank())
|
||||||
|
? MediaType.parseMediaType(ct)
|
||||||
|
: MediaType.IMAGE_JPEG;
|
||||||
|
|
||||||
|
log.info("[PrestaShop] POST (image) {} (size={} bytes, contentType={})",
|
||||||
|
uri, imageFile.getSize(), mediaType);
|
||||||
|
|
||||||
|
try {
|
||||||
|
byte[] bytes = imageFile.getBytes();
|
||||||
|
|
||||||
|
return client.post()
|
||||||
|
.uri(uri)
|
||||||
|
.contentType(mediaType)
|
||||||
|
.body(bytes)
|
||||||
|
.retrieve()
|
||||||
|
.toEntity(String.class);
|
||||||
|
|
||||||
|
} catch (RestClientResponseException ex) {
|
||||||
|
// Erreur fonctionnelle renvoyée par Presta (taille, format, etc.)
|
||||||
|
log.error("[PrestaShop] POST image error {} : {}", ex.getRawStatusCode(), ex.getResponseBodyAsString());
|
||||||
|
return ResponseEntity
|
||||||
|
.status(ex.getRawStatusCode())
|
||||||
|
.contentType(MediaType.APPLICATION_XML)
|
||||||
|
.body(ex.getResponseBodyAsString());
|
||||||
|
} catch (IOException ex) {
|
||||||
|
// Problème de lecture du fichier envoyé depuis le client
|
||||||
|
log.error("[PrestaShop] POST image, erreur lecture fichier", ex);
|
||||||
|
return ResponseEntity
|
||||||
|
.status(400)
|
||||||
|
.contentType(MediaType.TEXT_PLAIN)
|
||||||
|
.body("Invalid image file");
|
||||||
|
} catch (RestClientException ex) {
|
||||||
|
// Cas réseau / technique
|
||||||
|
log.error("[PrestaShop] POST image technical error", ex);
|
||||||
|
return ResponseEntity
|
||||||
|
.status(502)
|
||||||
|
.contentType(MediaType.TEXT_PLAIN)
|
||||||
|
.body("Error while calling PrestaShop WebService");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user