From 29ab557da3490b1831e0bc11fa2dd9ae1814b242 Mon Sep 17 00:00:00 2001 From: Tim <47184194+imgde@users.noreply.github.com> Date: Sun, 15 Jun 2025 12:30:01 +0200 Subject: [PATCH] Add Images as base64, mpf secondary (ImageController) --- .../webshop/config/ControllerPathConfig.java | 1 + .../webshop/controller/ImageController.java | 36 +++++++++++++------ .../htwsaar/webshop/service/ImageService.java | 2 ++ .../service/impl/ImageServiceImpl.java | 18 ++++++++++ 4 files changed, 46 insertions(+), 11 deletions(-) diff --git a/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java b/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java index 508159c..084eed2 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/config/ControllerPathConfig.java @@ -30,6 +30,7 @@ public class ControllerPathConfig { //ImageController public static final String IMAGE_BASE = "/image"; + public static final String IMAGE_FORM = IMAGE_BASE + "/form"; public static final String IMAGE_ALL = IMAGE_BASE + "/all"; //FarmImageController diff --git a/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java index 96236be..cc7e89e 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/controller/ImageController.java @@ -16,8 +16,7 @@ import java.util.ArrayList; import java.util.List; import java.util.UUID; -import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_ALL; -import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_BASE; +import static de.htwsaar.webshop.config.ControllerPathConfig.*; import static de.htwsaar.webshop.config.ParameterConfig.*; import static de.htwsaar.webshop.util.LoggerUtil.logRequest; @@ -60,34 +59,33 @@ public class ImageController { @RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json") public ResponseEntity add(HttpServletRequest request, - @RequestParam(value = PARAM_UUID) UUID articleUuid, - @RequestParam(value = PARAM_IMAGE) MultipartFile file) { + @RequestParam(value = PARAM_UUID) UUID articleUuid, + @RequestParam(value = PARAM_IMAGE) String base64) { logRequest(request); if (articleUuid == null || articleService.findByUUID(articleUuid) == null - || file == null || file.isEmpty()) { + || base64 == null || base64.isEmpty()) { log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); return ResponseEntity.badRequest().body(false); } - Image a = imageService.save(articleUuid, file); - return ResponseEntity.ok(a != null); + return ResponseEntity.ok(imageService.save(articleUuid, base64) != null); } @RequestMapping(path = IMAGE_ALL, method = RequestMethod.POST, produces = "application/json") public ResponseEntity addAll(HttpServletRequest request, @RequestParam(value = PARAM_UUID) UUID articleUuid, - @RequestParam(value = PARAM_IMAGE) List files) { + @RequestParam(value = PARAM_IMAGE) List base64list) { logRequest(request); if (articleUuid == null || articleService.findByUUID(articleUuid) == null - || files == null || files.isEmpty()) { + || base64list == null || base64list.isEmpty()) { log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); return ResponseEntity.badRequest().body(0); } int[] successful = {0}; - files.forEach(mpf -> { - if (imageService.save(articleUuid, mpf) != null) { + base64list.forEach(b64 -> { + if (imageService.save(articleUuid, b64) != null) { successful[0]++; } }); @@ -121,4 +119,20 @@ public class ImageController { imageService.deleteById(imageId); return ResponseEntity.ok().build(); } + + @RequestMapping(path = IMAGE_FORM, method = RequestMethod.POST, produces = "application/json") + public ResponseEntity addForm(HttpServletRequest request, + @RequestParam(value = PARAM_UUID) UUID articleUuid, + @RequestParam(value = PARAM_IMAGE) MultipartFile file) { + logRequest(request); + + if (articleUuid == null || articleService.findByUUID(articleUuid) == null + || file == null || file.isEmpty()) { + log.warn("[{}] failed Validation, sending bad request", request.getRequestURI()); + return ResponseEntity.badRequest().body(false); + } + + Image a = imageService.save(articleUuid, file); + return ResponseEntity.ok(a != null); + } } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java b/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java index 300fbb9..26a8575 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/ImageService.java @@ -20,5 +20,7 @@ public interface ImageService { Image save(UUID uuid, MultipartFile file); + Image save(UUID uuid, String base64); + void deleteById(Long imageId); } diff --git a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java index f368018..61ae398 100644 --- a/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java +++ b/00-backend/src/main/java/de/htwsaar/webshop/service/impl/ImageServiceImpl.java @@ -84,6 +84,24 @@ public class ImageServiceImpl implements ImageService { return null; } + @Override + public Image save(UUID uuid, String base64) { + if (uuid == null) { + log.warn("Got no UUID, aborting"); + return null; + } + Article article = articleService.findByUUID(uuid); + if (article == null) { + log.warn("Could not find article with id {}", uuid); + return null; + } + if (base64 == null || base64.isEmpty()) { + log.warn("Got no base64 for {}", uuid); + return null; + } + return imageRepository.save(new Image(null, article, base64)); + } + @Override @Transactional public void deleteById(Long imageId) {