Add Images as base64, mpf secondary (ImageController)
This commit is contained in:
@@ -30,6 +30,7 @@ public class ControllerPathConfig {
|
|||||||
|
|
||||||
//ImageController
|
//ImageController
|
||||||
public static final String IMAGE_BASE = "/image";
|
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";
|
public static final String IMAGE_ALL = IMAGE_BASE + "/all";
|
||||||
|
|
||||||
//FarmImageController
|
//FarmImageController
|
||||||
|
|||||||
@@ -16,8 +16,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_ALL;
|
import static de.htwsaar.webshop.config.ControllerPathConfig.*;
|
||||||
import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_BASE;
|
|
||||||
import static de.htwsaar.webshop.config.ParameterConfig.*;
|
import static de.htwsaar.webshop.config.ParameterConfig.*;
|
||||||
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
|
||||||
|
|
||||||
@@ -61,33 +60,32 @@ public class ImageController {
|
|||||||
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
|
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.POST, produces = "application/json")
|
||||||
public ResponseEntity<Boolean> add(HttpServletRequest request,
|
public ResponseEntity<Boolean> add(HttpServletRequest request,
|
||||||
@RequestParam(value = PARAM_UUID) UUID articleUuid,
|
@RequestParam(value = PARAM_UUID) UUID articleUuid,
|
||||||
@RequestParam(value = PARAM_IMAGE) MultipartFile file) {
|
@RequestParam(value = PARAM_IMAGE) String base64) {
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
|
|
||||||
if (articleUuid == null || articleService.findByUUID(articleUuid) == null
|
if (articleUuid == null || articleService.findByUUID(articleUuid) == null
|
||||||
|| file == null || file.isEmpty()) {
|
|| base64 == null || base64.isEmpty()) {
|
||||||
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
return ResponseEntity.badRequest().body(false);
|
return ResponseEntity.badRequest().body(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
Image a = imageService.save(articleUuid, file);
|
return ResponseEntity.ok(imageService.save(articleUuid, base64) != null);
|
||||||
return ResponseEntity.ok(a != null);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(path = IMAGE_ALL, method = RequestMethod.POST, produces = "application/json")
|
@RequestMapping(path = IMAGE_ALL, method = RequestMethod.POST, produces = "application/json")
|
||||||
public ResponseEntity<Integer> addAll(HttpServletRequest request,
|
public ResponseEntity<Integer> addAll(HttpServletRequest request,
|
||||||
@RequestParam(value = PARAM_UUID) UUID articleUuid,
|
@RequestParam(value = PARAM_UUID) UUID articleUuid,
|
||||||
@RequestParam(value = PARAM_IMAGE) List<MultipartFile> files) {
|
@RequestParam(value = PARAM_IMAGE) List<String> base64list) {
|
||||||
logRequest(request);
|
logRequest(request);
|
||||||
|
|
||||||
if (articleUuid == null || articleService.findByUUID(articleUuid) == null
|
if (articleUuid == null || articleService.findByUUID(articleUuid) == null
|
||||||
|| files == null || files.isEmpty()) {
|
|| base64list == null || base64list.isEmpty()) {
|
||||||
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
|
||||||
return ResponseEntity.badRequest().body(0);
|
return ResponseEntity.badRequest().body(0);
|
||||||
}
|
}
|
||||||
int[] successful = {0};
|
int[] successful = {0};
|
||||||
files.forEach(mpf -> {
|
base64list.forEach(b64 -> {
|
||||||
if (imageService.save(articleUuid, mpf) != null) {
|
if (imageService.save(articleUuid, b64) != null) {
|
||||||
successful[0]++;
|
successful[0]++;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -121,4 +119,20 @@ public class ImageController {
|
|||||||
imageService.deleteById(imageId);
|
imageService.deleteById(imageId);
|
||||||
return ResponseEntity.ok().build();
|
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, MultipartFile file);
|
||||||
|
|
||||||
|
Image save(UUID uuid, String base64);
|
||||||
|
|
||||||
void deleteById(Long imageId);
|
void deleteById(Long imageId);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,6 +84,24 @@ public class ImageServiceImpl implements ImageService {
|
|||||||
return null;
|
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
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
public void deleteById(Long imageId) {
|
public void deleteById(Long imageId) {
|
||||||
|
|||||||
Reference in New Issue
Block a user