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
|
||||
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;
|
||||
}
|
||||
|
||||
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||
@GetMapping(value = "/images/{id}", produces = MediaType.IMAGE_JPEG_VALUE)
|
||||
public ResponseEntity<?> getImage(@PathVariable("id") long id) {
|
||||
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
Optional<Image> 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);
|
||||
}
|
||||
|
||||
@DeleteMapping(value = "/images/{id}")
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||
@DeleteMapping(value = "/images/{id}")
|
||||
public ResponseEntity<?> deleteImage(@PathVariable("id") long id) {
|
||||
|
||||
Optional<Image> image = imageDao.retrieve(id);
|
||||
Optional<Image> 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);
|
||||
}
|
||||
|
||||
@PostMapping(value = "/images")
|
||||
public ResponseEntity<?> addImage(@RequestParam("file") MultipartFile file, RedirectAttributes redirectAttributes) {
|
||||
@PostMapping(value = "/images")
|
||||
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.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);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/images", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ArrayNode getImageList() {
|
||||
List<Image> 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());
|
||||
objectNode.put("size", image.getSize());
|
||||
objectNode.put("type", image.getMediaType().toString());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
@GetMapping(value = "/images", produces = "application/json")
|
||||
@ResponseBody
|
||||
public ArrayNode getImageList() {
|
||||
List<Image> 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());
|
||||
objectNode.put("size", image.getSize());
|
||||
objectNode.put("type", image.getMediaType().toString());
|
||||
nodes.add(objectNode);
|
||||
}
|
||||
return nodes;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user