removed unnecessary lines
This commit is contained in:
1 parent
cad0adda05
commit
df871d42bb
2 files changed
+52
-54
No files matched your search
@@ -28,70 +28,69 @@ import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
|||||||
@RestController
|
@RestController
|
||||||
public class ImageController {
|
public class ImageController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private ObjectMapper mapper;
|
private ObjectMapper mapper;
|
||||||
|
|
||||||
private final ImageDao imageDao;
|
private final ImageDao imageDao;
|
||||||
|
|
||||||
@Autowired
|
public ImageController(ImageDao imageDao) {
|
||||||
public ImageController(ImageDao imageDao) {
|
this.imageDao = imageDao;
|
||||||
this.imageDao = imageDao;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
|
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||||
|
|
||||||
Optional<Image> image = imageDao.retrieve(id);
|
Optional<Image> image = imageDao.retrieve(id);
|
||||||
|
|
||||||
if (image.isPresent()) {
|
if (image.isPresent()) {
|
||||||
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
|
InputStream inputStream = new ByteArrayInputStream(image.get().getData());
|
||||||
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
|
return ResponseEntity.ok().contentType(MediaType.IMAGE_JPEG).body(new InputStreamResource(inputStream));
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@DeleteMapping(value = "/images/{id}")
|
@DeleteMapping(value = "/images/{id}")
|
||||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||||
|
|
||||||
Optional<Image> image = imageDao.retrieve(id);
|
Optional<Image> image = imageDao.retrieve(id);
|
||||||
|
|
||||||
if (image.isPresent()) {
|
if (image.isPresent()) {
|
||||||
imageDao.delete(image.get());
|
imageDao.delete(image.get());
|
||||||
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
|
return new ResponseEntity<>("Image id=" + id + " deleted.", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
return new ResponseEntity<>("Image id=" + id + " not found.", HttpStatus.NOT_FOUND);
|
||||||
}
|
}
|
||||||
|
|
||||||
@PostMapping(value = "/images")
|
@PostMapping(value = "/images")
|
||||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||||
|
|
||||||
String contentType = file.getContentType();
|
String contentType = file.getContentType();
|
||||||
if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) {
|
if (!contentType.equals(MediaType.IMAGE_JPEG.toString())) {
|
||||||
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
return new ResponseEntity<>("Only JPEG file format supported", HttpStatus.UNSUPPORTED_MEDIA_TYPE);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
|
imageDao.create(new Image(file.getOriginalFilename(), file.getBytes()));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
|
return new ResponseEntity<>("Failure to read file", HttpStatus.NO_CONTENT);
|
||||||
}
|
}
|
||||||
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
|
return new ResponseEntity<>("Image uploaded", HttpStatus.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/images", produces = "application/json")
|
@GetMapping(value = "/images", produces = "application/json")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public ArrayNode getImageList() {
|
public ArrayNode getImageList() {
|
||||||
List<Image> images = imageDao.retrieveAll();
|
List<Image> images = imageDao.retrieveAll();
|
||||||
ArrayNode nodes = mapper.createArrayNode();
|
ArrayNode nodes = mapper.createArrayNode();
|
||||||
for (Image image : images) {
|
for (Image image : images) {
|
||||||
ObjectNode objectNode = mapper.createObjectNode();
|
ObjectNode objectNode = mapper.createObjectNode();
|
||||||
objectNode.put("id", image.getId());
|
objectNode.put("id", image.getId());
|
||||||
objectNode.put("name", image.getName());
|
objectNode.put("name", image.getName());
|
||||||
objectNode.put("size", image.getSize());
|
objectNode.put("size", image.getSize());
|
||||||
objectNode.put("type", image.getMediaType().toString());
|
objectNode.put("type", image.getMediaType().toString());
|
||||||
nodes.add(objectNode);
|
nodes.add(objectNode);
|
||||||
}
|
}
|
||||||
return nodes;
|
return nodes;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -10,7 +10,6 @@ import java.util.Map;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
|
|
||||||
import org.springframework.core.io.ClassPathResource;
|
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
|
|||||||
Reference in new issue
Block a user