Browse Source

replace entity with dto

develop
bvn13 3 years ago
parent
commit
e4cc811af9
  1. 6
      src/main/java/ru/bvn13/adastor/web/controllers/ViewController.java
  2. 8
      src/main/java/ru/bvn13/adastor/web/services/StortionService.java

6
src/main/java/ru/bvn13/adastor/web/controllers/ViewController.java

@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -4,7 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 javax.servlet.http.HttpServletResponse;
@ -29,13 +29,13 @@ public class ViewController { @@ -29,13 +29,13 @@ public class ViewController {
@GetMapping("/v/{uuid}")
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()) {
try(BufferedOutputStream bos = new BufferedOutputStream(response.getOutputStream())) {
InputStream is = stortionService.getInputStream(stortion.get());
is.transferTo(bos);
} catch (FileNotFoundException e) {
response.sendError(403, "not found");
response.sendError(404, "not found");
}
} else {
response.sendRedirect("/notfound");

8
src/main/java/ru/bvn13/adastor/web/services/StortionService.java

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

Loading…
Cancel
Save