ADD download functionality

This commit is contained in:
2025-05-11 14:56:22 +02:00
parent 3ec9f26264
commit 518a1f3f9f
4 changed files with 100 additions and 0 deletions

View File

@@ -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<Resource> 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<String> handleFileNotFound(JobNotFound ex) {
return ResponseEntity.status(404).body(ex.getMessage());
}
@ExceptionHandler(JobNotFinished.class)
public ResponseEntity<String> handleJobNotFinished(JobNotFinished ex) {
return ResponseEntity.status(404).body(ex.getMessage());
}
}