replace entity with dto

develop
bvn13 2019-04-19 21:39:20 +03:00
parent 9a6a3cff37
commit e4cc811af9
2 changed files with 7 additions and 7 deletions

View File

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import ru.bvn13.adastor.entities.Stortion; import ru.bvn13.adastor.entities.dtos.StortionDto;
import ru.bvn13.adastor.web.services.StortionService; import ru.bvn13.adastor.web.services.StortionService;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@ -29,13 +29,13 @@ public class ViewController {
@GetMapping("/v/{uuid}") @GetMapping("/v/{uuid}")
public void getStortion(@PathVariable("uuid") String uuid, HttpServletResponse response) throws IOException { public void getStortion(@PathVariable("uuid") String uuid, HttpServletResponse response) throws IOException {
Optional<Stortion> stortion = stortionService.findStortion(uuid); Optional<StortionDto> stortion = stortionService.findStortion(uuid);
if (stortion.isPresent()) { if (stortion.isPresent()) {
try(BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) { try(BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
InputStream is = stortionService.getInputStream(stortion.get()); InputStream is = stortionService.getInputStream(stortion.get());
is.transferTo(bos); is.transferTo(bos);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
response.sendError(403, "not found"); response.sendError(404, "not found");
} }
} else { } else {
response.sendRedirect("/notfound"); response.sendRedirect("/notfound");

View File

@ -53,16 +53,16 @@ public class StortionService {
this.diskFreeSpaceChecker = diskFreeSpaceChecker; this.diskFreeSpaceChecker = diskFreeSpaceChecker;
} }
public Optional<Stortion> findStortion(String uuid) { public Optional<StortionDto> findStortion(String uuid) {
return stortionRepository.findById(uuid); return stortionRepository.findById(uuid).map(this::convertToDto);
} }
public String getPath(Stortion stortion) { public String getPath(StortionDto stortion) {
String storagePath = config.getStoragePath(); String storagePath = config.getStoragePath();
return storagePath + stortion.getPath(); return storagePath + stortion.getPath();
} }
public InputStream getInputStream(Stortion stortion) throws FileNotFoundException { public InputStream getInputStream(StortionDto stortion) throws FileNotFoundException {
String path = getPath(stortion); String path = getPath(stortion);
InputStream targetStream = new DataInputStream(new FileInputStream(path)); InputStream targetStream = new DataInputStream(new FileInputStream(path));
return targetStream; return targetStream;