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