diff --git a/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java b/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java new file mode 100644 index 0000000..50e604e --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/controllers/DownloadController.java @@ -0,0 +1,48 @@ +package com.ddf.vodsystem.controllers; + +import com.ddf.vodsystem.exceptions.JobNotFinished; +import com.ddf.vodsystem.exceptions.JobNotFound; +import com.ddf.vodsystem.services.DownloadService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.Resource; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class DownloadController { + private final DownloadService downloadService; + + @Autowired + public DownloadController(DownloadService downloadService) { + this.downloadService = downloadService; + } + + @GetMapping("/download/{filename}") + public ResponseEntity downloadFile(@PathVariable String filename) { + Resource resource = downloadService.download(filename); + + if (resource == null || !resource.exists()) { + return ResponseEntity.notFound().build(); + } + + return ResponseEntity.ok() + .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"") + .contentType(MediaType.APPLICATION_OCTET_STREAM) + .body(resource); + } + + @ExceptionHandler(JobNotFound.class) + public ResponseEntity handleFileNotFound(JobNotFound ex) { + return ResponseEntity.status(404).body(ex.getMessage()); + } + + @ExceptionHandler(JobNotFinished.class) + public ResponseEntity handleJobNotFinished(JobNotFinished ex) { + return ResponseEntity.status(404).body(ex.getMessage()); + } +} diff --git a/src/main/java/com/ddf/vodsystem/exceptions/JobNotFinished.java b/src/main/java/com/ddf/vodsystem/exceptions/JobNotFinished.java new file mode 100644 index 0000000..95d17e5 --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/exceptions/JobNotFinished.java @@ -0,0 +1,7 @@ +package com.ddf.vodsystem.exceptions; + +public class JobNotFinished extends RuntimeException { + public JobNotFinished(String message) { + super(message); + } +} diff --git a/src/main/java/com/ddf/vodsystem/exceptions/JobNotFound.java b/src/main/java/com/ddf/vodsystem/exceptions/JobNotFound.java new file mode 100644 index 0000000..84e4f81 --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/exceptions/JobNotFound.java @@ -0,0 +1,7 @@ +package com.ddf.vodsystem.exceptions; + +public class JobNotFound extends RuntimeException { + public JobNotFound(String message) { + super(message); + } +} diff --git a/src/main/java/com/ddf/vodsystem/services/DownloadService.java b/src/main/java/com/ddf/vodsystem/services/DownloadService.java new file mode 100644 index 0000000..aae5eb3 --- /dev/null +++ b/src/main/java/com/ddf/vodsystem/services/DownloadService.java @@ -0,0 +1,38 @@ +package com.ddf.vodsystem.services; + +import com.ddf.vodsystem.entities.JobStatus; +import com.ddf.vodsystem.exceptions.JobNotFinished; +import com.ddf.vodsystem.exceptions.JobNotFound; +import com.ddf.vodsystem.tools.Job; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.FileSystemResource; +import org.springframework.core.io.Resource; +import org.springframework.stereotype.Service; + +import java.io.File; + +@Service +public class DownloadService { + + private final JobService jobService; + + @Autowired + public DownloadService(JobService jobService) { + this.jobService = jobService; + } + + public Resource download(String uuid) { + Job job = jobService.get(uuid); + + if (job == null) { + throw new JobNotFound("Job doesn't exist"); + } + + if (job.getStatus() != JobStatus.FINISHED) { + throw new JobNotFinished("Job is not finished"); + } + + File file = job.getOutputFile(); + return new FileSystemResource(file); + } +}