package com.ddf.vodsystem.controllers; 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.MediaTypeFactory; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("api/v1/download") public class DownloadController { private final DownloadService downloadService; private static final String FILENAME_HEADER = "inline; filename=\"%s\""; @Autowired public DownloadController(DownloadService downloadService) { this.downloadService = downloadService; } @GetMapping("/output/{uuid}") public ResponseEntity downloadFile(@PathVariable String uuid) { Resource resource = downloadService.downloadOutput(uuid); if (resource == null || !resource.exists()) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, String.format(FILENAME_HEADER, resource.getFilename())) .contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM)) .body(resource); } @GetMapping("/input/{uuid}") public ResponseEntity downloadInput(@PathVariable String uuid) { Resource resource = downloadService.downloadInput(uuid); if (resource == null || !resource.exists()) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, String.format(FILENAME_HEADER, resource.getFilename())) .contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM)) .body(resource); } @GetMapping("/clip/{id}") public ResponseEntity downloadClip(@PathVariable Long id) { Resource resource = downloadService.downloadClip(id); if (resource == null || !resource.exists()) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, String.format(FILENAME_HEADER, resource.getFilename())) .contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM)) .body(resource); } @GetMapping("/thumbnail/{id}") public ResponseEntity downloadThumbnail(@PathVariable Long id) { Resource resource = downloadService.downloadThumbnail(id); if (resource == null || !resource.exists()) { return ResponseEntity.notFound().build(); } return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, String.format(FILENAME_HEADER, resource.getFilename())) .contentType(MediaTypeFactory.getMediaType(resource).orElse(MediaType.APPLICATION_OCTET_STREAM)) .body(resource); } }