From ffdb59615b682b4d43c7a85cb9b6b51bb6df175f Mon Sep 17 00:00:00 2001 From: keahorvath Date: Mon, 13 Mar 2023 19:55:03 +0100 Subject: [PATCH] removed unnecessary lines --- .../java/pdl/backend/ImageController.java | 101 +++++++++--------- .../src/main/java/pdl/backend/ImageDao.java | 1 - 2 files changed, 50 insertions(+), 52 deletions(-) diff --git a/backend/src/main/java/pdl/backend/ImageController.java b/backend/src/main/java/pdl/backend/ImageController.java index 1e4962d..9a15ad7 100644 --- a/backend/src/main/java/pdl/backend/ImageController.java +++ b/backend/src/main/java/pdl/backend/ImageController.java @@ -27,68 +27,67 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes; @RestController public class ImageController { - @Autowired - private ObjectMapper mapper; + @Autowired + private ObjectMapper mapper; - private final ImageDao imageDao; + private final ImageDao imageDao; - @Autowired - public ImageController(ImageDao imageDao) { - this.imageDao = imageDao; - } + public ImageController(ImageDao imageDao) { + this.imageDao = imageDao; + } - @RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) - public ResponseEntity getImage(@PathVariable("id") long id) { + @RequestMapping(value = "/images/{id}", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) + public ResponseEntity getImage(@PathVariable("id") long id) { - Optional image = imageDao.retrieve(id); + Optional image = imageDao.retrieve(id); - if (image.isPresent()) { - InputStream inputStream = new ByteArrayInputStream(image.get().getData()); - return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream)); - } - return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); - } + if (image.isPresent()) { + InputStream inputStream = new ByteArrayInputStream(image.get().getData()); + return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream)); + } + return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); + } - @RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE) - public ResponseEntity deleteImage(@PathVariable("id") long id) { + @RequestMapping(value = "/images/{id}", method = RequestMethod.DELETE) + public ResponseEntity deleteImage(@PathVariable("id") long id) { - Optional image = imageDao.retrieve(id); + Optional image = imageDao.retrieve(id); - if (image.isPresent()) { - imageDao.delete(image.get()); - return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK); - } - return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); - } + if (image.isPresent()) { + imageDao.delete(image.get()); + return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK); + } + return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND); + } - @RequestMapping(value = "/images", method = RequestMethod.POST) - public ResponseEntity addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { + @RequestMapping(value = "/images", method = RequestMethod.POST) + public ResponseEntity addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) { - String contentType = file.getContentType(); - if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) { - return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE); - } + String contentType = file.getContentType(); + if (contentType != null && !contentType.equals(MediaType.IMAGE_JPEG.toString())) { + return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE); + } - try { - imageDao.create(new Image(file.getOriginalFilename(), file.getBytes())); - } catch (IOException e) { - return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT); - } - return new ResponseEntity<>("Image uploaded", HttpStatus.OK); - } + try { + imageDao.create(new Image(file.getOriginalFilename(), file.getBytes())); + } catch (IOException e) { + return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT); + } + return new ResponseEntity<>("Image uploaded", HttpStatus.OK); + } - @RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json") - @ResponseBody - public ArrayNode getImageList() { - List images = imageDao.retrieveAll(); - ArrayNode nodes = mapper.createArrayNode(); - for (Image image : images) { - ObjectNode objectNode = mapper.createObjectNode(); - objectNode.put("id", image.getId()); - objectNode.put("name", image.getName()); - nodes.add(objectNode); - } - return nodes; - } + @RequestMapping(value = "/images", method = RequestMethod.GET, produces = "application/json") + @ResponseBody + public ArrayNode getImageList() { + List images = imageDao.retrieveAll(); + ArrayNode nodes = mapper.createArrayNode(); + for (Image image : images) { + ObjectNode objectNode = mapper.createObjectNode(); + objectNode.put("id", image.getId()); + objectNode.put("name", image.getName()); + nodes.add(objectNode); + } + return nodes; + } } diff --git a/backend/src/main/java/pdl/backend/ImageDao.java b/backend/src/main/java/pdl/backend/ImageDao.java index 2531c9d..b3deda7 100644 --- a/backend/src/main/java/pdl/backend/ImageDao.java +++ b/backend/src/main/java/pdl/backend/ImageDao.java @@ -10,7 +10,6 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; -import org.springframework.core.io.ClassPathResource; import org.springframework.stereotype.Repository; @Repository