remove exception handling
This commit is contained in:
parent
344147c9b5
commit
c1afe47b93
|
@ -1,7 +1,6 @@
|
|||
package com.baeldung.mongodb.services;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.bson.BsonBinarySubType;
|
||||
import org.bson.types.Binary;
|
||||
|
@ -19,20 +18,13 @@ public class PhotoService {
|
|||
private PhotoRepository photoRepo;
|
||||
|
||||
public Photo getPhoto(String id) {
|
||||
Optional<Photo> result = photoRepo.findById(id);
|
||||
return result.isPresent() ? result.get() : null;
|
||||
return photoRepo.findById(id).get();
|
||||
}
|
||||
|
||||
public String addPhoto(String title, MultipartFile file) {
|
||||
String id = null;
|
||||
try {
|
||||
public String addPhoto(String title, MultipartFile file) throws IOException {
|
||||
Photo photo = new Photo(title);
|
||||
photo.setImage(new Binary(BsonBinarySubType.BINARY, file.getBytes()));
|
||||
photo = photoRepo.insert(photo);
|
||||
id = photo.getId();
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
return id;
|
||||
return photo.getId();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,31 +25,19 @@ public class VideoService {
|
|||
@Autowired
|
||||
private GridFsOperations operations;
|
||||
|
||||
public Video getVideo(String id) {
|
||||
Video video = null;
|
||||
public Video getVideo(String id) throws IllegalStateException, IOException {
|
||||
GridFSFile file = gridFsTemplate.findOne(new Query(Criteria.where("_id").is(id)));
|
||||
if (file != null) {
|
||||
video = new Video();
|
||||
Video video = new Video();
|
||||
video.setTitle(file.getMetadata().get("title").toString());
|
||||
try {
|
||||
video.setStream(operations.getResource(file).getInputStream());
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return video;
|
||||
}
|
||||
|
||||
public String addVideo(String title, MultipartFile file) {
|
||||
public String addVideo(String title, MultipartFile file) throws IOException {
|
||||
DBObject metaData = new BasicDBObject();
|
||||
metaData.put("type", "video");
|
||||
metaData.put("title", title);
|
||||
ObjectId id;
|
||||
try {
|
||||
id = gridFsTemplate.store(file.getInputStream(), file.getName(), file.getContentType(), metaData);
|
||||
} catch (IOException e) {
|
||||
return null;
|
||||
}
|
||||
ObjectId id = gridFsTemplate.store(file.getInputStream(), file.getName(), file.getContentType(), metaData);
|
||||
return id.toString();
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package com.baeldung.mongodb.web;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
@ -23,14 +24,10 @@ public class PhotoController {
|
|||
@GetMapping("/photos/{id}")
|
||||
public String getPhoto(@PathVariable String id, Model model) {
|
||||
Photo photo = photoService.getPhoto(id);
|
||||
if (photo != null) {
|
||||
model.addAttribute("title", photo.getTitle());
|
||||
model.addAttribute("image", Base64.getEncoder().encodeToString(photo.getImage().getData()));
|
||||
return "photos";
|
||||
}
|
||||
model.addAttribute("message", "Photo not found");
|
||||
return "index";
|
||||
}
|
||||
|
||||
@GetMapping("/photos/upload")
|
||||
public String uploadPhoto(Model model) {
|
||||
|
@ -39,12 +36,8 @@ public class PhotoController {
|
|||
}
|
||||
|
||||
@PostMapping("/photos/add")
|
||||
public String addPhoto(@RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model) {
|
||||
public String addPhoto(@RequestParam("title") String title, @RequestParam("image") MultipartFile image, Model model) throws IOException {
|
||||
String id = photoService.addPhoto(title, image);
|
||||
if (id == null) {
|
||||
model.addAttribute("message", "Error Occurred");
|
||||
return "index";
|
||||
}
|
||||
return "redirect:/photos/" + id;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,29 +24,17 @@ public class VideoController {
|
|||
private VideoService videoService;
|
||||
|
||||
@GetMapping("/videos/{id}")
|
||||
public String getVideo(@PathVariable String id, Model model) {
|
||||
public String getVideo(@PathVariable String id, Model model) throws IllegalStateException, IOException {
|
||||
Video video = videoService.getVideo(id);
|
||||
if (video != null) {
|
||||
model.addAttribute("title", video.getTitle());
|
||||
model.addAttribute("url", "/videos/stream/" + id);
|
||||
return "videos";
|
||||
}
|
||||
model.addAttribute("message", "Video not found");
|
||||
return "index";
|
||||
}
|
||||
|
||||
@GetMapping("/videos/stream/{id}")
|
||||
public void streamVideo(@PathVariable String id, HttpServletResponse response) {
|
||||
public void streamVideo(@PathVariable String id, HttpServletResponse response) throws IllegalStateException, IOException {
|
||||
Video video = videoService.getVideo(id);
|
||||
if (video != null) {
|
||||
try {
|
||||
FileCopyUtils.copy(video.getStream(), response.getOutputStream());
|
||||
} catch (IOException e) {
|
||||
response.setStatus(500);
|
||||
}
|
||||
} else {
|
||||
response.setStatus(404);
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/videos/upload")
|
||||
|
@ -56,12 +44,8 @@ public class VideoController {
|
|||
}
|
||||
|
||||
@PostMapping("/videos/add")
|
||||
public String addVideo(@RequestParam("title") String title, @RequestParam("file") MultipartFile file, Model model) {
|
||||
public String addVideo(@RequestParam("title") String title, @RequestParam("file") MultipartFile file, Model model) throws IOException {
|
||||
String id = videoService.addVideo(title, file);
|
||||
if (id == null) {
|
||||
model.addAttribute("message", "Error Occurred");
|
||||
return "index";
|
||||
}
|
||||
return "redirect:/videos/" + id;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue