Add Images as base64, mpf secondary (ImageController)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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<Boolean> 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<Integer> addAll(HttpServletRequest request,
|
||||
@RequestParam(value = PARAM_UUID) UUID articleUuid,
|
||||
@RequestParam(value = PARAM_IMAGE) List<MultipartFile> files) {
|
||||
@RequestParam(value = PARAM_IMAGE) List<String> 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<Boolean> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,5 +20,7 @@ public interface ImageService {
|
||||
|
||||
Image save(UUID uuid, MultipartFile file);
|
||||
|
||||
Image save(UUID uuid, String base64);
|
||||
|
||||
void deleteById(Long imageId);
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user