Reimplement ImageController / Service to hold base64 images instead of uris

This commit is contained in:
Tim
2025-06-05 10:02:04 +02:00
parent 6484a611cd
commit 65cc4ede31
10 changed files with 76 additions and 70 deletions

View File

@@ -25,7 +25,7 @@ public class ControllerPathConfig {
//ImageController
public static final String IMAGE_BASE = "/image";
public static final String IMAGE_GET_ALL = IMAGE_BASE + "/all";
public static final String IMAGE_ALL = IMAGE_BASE + "/all";
//OrderController
public static final String ORDER_BASE = "/order";

View File

@@ -2,7 +2,6 @@ package de.htwsaar.webshop.config;
@SuppressWarnings("unused")
public class ParameterConfig {
public static final String PARAM_ID = "id";
public static final String PARAM_UUID = "uuid";
public static final String PARAM_ARTICLE_ID = "articleId";
@@ -11,4 +10,5 @@ public class ParameterConfig {
public static final String PARAM_CUSTOMER_ID = "customerId";
public static final String PARAM_URI = "uri";
public static final String PARAM_RATING = "rating";
public static final String PARAM_IMAGE = "image";
}

View File

@@ -1,6 +1,5 @@
package de.htwsaar.webshop.controller;
import de.htwsaar.webshop.model.ImageModel;
import de.htwsaar.webshop.repository.entities.Image;
import de.htwsaar.webshop.service.ArticleService;
import de.htwsaar.webshop.service.ImageService;
@@ -10,12 +9,13 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_BASE;
import static de.htwsaar.webshop.config.ControllerPathConfig.IMAGE_GET_ALL;
import static de.htwsaar.webshop.config.ControllerPathConfig.*;
import static de.htwsaar.webshop.config.ParameterConfig.*;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
@@ -31,44 +31,67 @@ public class ImageController {
this.articleService = articleService;
}
@RequestMapping(path = IMAGE_GET_ALL, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<ImageModel>> getAll(HttpServletRequest request,
@RequestMapping(path = IMAGE_ALL, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<List<String>> getAll(HttpServletRequest request,
@RequestParam(value = PARAM_UUID) UUID uuid) {
logRequest(request);
List<Image> images = imageService.getImagesByUUID(uuid);
if(images.isEmpty()) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return ResponseEntity.ok(imageService.from(images));
List<String> images_base = new ArrayList<>();
images.forEach(image -> images_base.add(image.getBase64()));
return ResponseEntity.ok(images_base);
}
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "application/json")
public ResponseEntity<ImageModel> getFirst(HttpServletRequest request,
@RequestParam(value = PARAM_UUID) UUID uuid) {
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.GET, produces = "image/*")
public ResponseEntity<String> getFirst(HttpServletRequest request,
@RequestParam(value = PARAM_UUID) UUID uuid) {
logRequest(request);
Image image = imageService.getImageByUUID(uuid);
if(image == null) {
return new ResponseEntity<>(HttpStatus.NO_CONTENT);
}
return ResponseEntity.ok(imageService.from(image));
return ResponseEntity.ok(image.getBase64());
}
@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_URI) String uri) {
@RequestParam(value = PARAM_UUID) UUID articleUuid,
@RequestParam(value = PARAM_IMAGE) MultipartFile file) {
logRequest(request);
if (articleUuid == null || articleService.findByUUID(articleUuid) == null
|| uri == null || uri.isEmpty()) {
|| file == null || file.isEmpty()) {
log.warn("[{}] failed Validation, sending bad request", request.getRequestURI());
return ResponseEntity.badRequest().body(false);
}
Image a = imageService.save(articleUuid, uri);
Image a = imageService.save(articleUuid, file);
return ResponseEntity.ok(a != 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) {
logRequest(request);
if (articleUuid == null || articleService.findByUUID(articleUuid) == null
|| files == null || files.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) {
successful[0]++;
}
});
return ResponseEntity.ok(successful[0]);
}
@RequestMapping(path = IMAGE_BASE, method = RequestMethod.PUT, produces = "application/json")
public ResponseEntity<Boolean> update(HttpServletRequest request,
@RequestParam(value = PARAM_ID) Long imageId,

View File

@@ -9,10 +9,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.io.InputStream;
import static de.htwsaar.webshop.config.ControllerPathConfig.HARDCODE_IMAGE_DPS_STICKER;
import static de.htwsaar.webshop.config.ControllerPathConfig.HEALTH;
import static de.htwsaar.webshop.util.LoggerUtil.logRequest;
@RestController

View File

@@ -1,13 +0,0 @@
package de.htwsaar.webshop.model;
import lombok.AllArgsConstructor;
import lombok.Getter;
import java.util.UUID;
@Getter
@AllArgsConstructor
public class ImageModel {
UUID articleId;
String uri;
}

View File

@@ -20,6 +20,6 @@ public class Image {
@JoinColumn(name = "article_id", referencedColumnName = "id", nullable = false)
private Article article;
@Column(name = "uri", nullable = false)
private String uri;
@Column(name = "base64", nullable = false)
private String base64;
}

View File

@@ -1,33 +1,23 @@
package de.htwsaar.webshop.service;
import de.htwsaar.webshop.model.ImageModel;
import de.htwsaar.webshop.repository.entities.Image;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.UUID;
public interface ImageService {
List<Image> getImagesByArticleId(Long articleId);
List<Image> getImagesByUUID(UUID uuid);
Image getImageByUUID(UUID uuid);
Image getImageByArticleId(Long imageId);
Image getImageById(Long imageId);
Image save(Image image);
Image save(UUID uuid, String uri);
Image save(UUID uuid, MultipartFile file);
void deleteById(Long imageId);
ImageModel from(Image image);
/**
* @param images Images to Map
* @return an <b>unmodifiable</b> {@link List} of {@link ImageModel}s
*/
List<ImageModel> from(List<Image> images);
}

View File

@@ -1,6 +1,5 @@
package de.htwsaar.webshop.service.impl;
import de.htwsaar.webshop.model.ImageModel;
import de.htwsaar.webshop.repository.ImageRepository;
import de.htwsaar.webshop.repository.entities.Article;
import de.htwsaar.webshop.repository.entities.Image;
@@ -10,7 +9,9 @@ import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import java.util.Base64;
import java.util.List;
import java.util.UUID;
@@ -27,17 +28,6 @@ public class ImageServiceImpl implements ImageService {
this.articleService = articleService;
}
@Override
public List<Image> getImagesByArticleId(Long articleId) {
return imageRepository.findAllByArticleId(articleId);
}
@Override
public Image getImageByArticleId(Long imageId) {
return imageRepository.findImageByArticleId(imageId);
}
@Override
public List<Image> getImagesByUUID(UUID uuid) {
return imageRepository.findImagesByArticle_Uuid(uuid);
@@ -71,19 +61,38 @@ public class ImageServiceImpl implements ImageService {
return imageRepository.save(image);
}
@Override
public Image save(UUID uuid, MultipartFile file) {
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 (file == null) {
log.warn("Got no file for {}", uuid);
return null;
}
try {
String based64 = Base64.getEncoder().encodeToString(file.getBytes());
if(based64 == null || based64.isEmpty()) {
log.warn("Could not save image with id {} and file size {}", uuid, file.getSize());
return null;
}
return imageRepository.save(new Image(null, article, based64));
} catch (Exception e) {
log.warn("Error saving image {} for article {}", file, uuid, e);
}
return null;
}
@Override
@Transactional
public void deleteById(Long imageId) {
imageRepository.deleteById(imageId);
}
@Override
public ImageModel from(Image image) {
return new ImageModel(image.getArticle().getUuid(), image.getUri());
}
@Override
public List<ImageModel> from(List<Image> images) {
return images.stream().map(this::from).toList();
}
}

View File

@@ -18,7 +18,7 @@ CREATE TABLE IF NOT EXISTS Images
(
id INTEGER PRIMARY KEY NOT NULL,
article_id INTEGER NOT NULL,
uri TEXT NOT NULL,
base64 TEXT NOT NULL,
FOREIGN KEY (article_id) REFERENCES Articles (id)
);