Merge pull request #7292 from Doha2012/master

remove exception handling
This commit is contained in:
Loredana Crusoveanu 2019-07-13 11:23:11 +03:00 committed by GitHub
commit 7b0d2aabf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 67 deletions

View File

@ -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 {
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;
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);
return photo.getId();
}
}

View File

@ -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.setTitle(file.getMetadata().get("title").toString());
try {
video.setStream(operations.getResource(file).getInputStream());
} catch (IOException e) {
return null;
}
}
Video video = new Video();
video.setTitle(file.getMetadata().get("title").toString());
video.setStream(operations.getResource(file).getInputStream());
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();
}

View File

@ -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,13 +24,9 @@ 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";
model.addAttribute("title", photo.getTitle());
model.addAttribute("image", Base64.getEncoder().encodeToString(photo.getImage().getData()));
return "photos";
}
@GetMapping("/photos/upload")
@ -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;
}
}

View File

@ -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";
model.addAttribute("title", video.getTitle());
model.addAttribute("url", "/videos/stream/" + id);
return "videos";
}
@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);
}
FileCopyUtils.copy(video.getStream(), response.getOutputStream());
}
@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;
}
}