Added sample download rest resource
This commit is contained in:
parent
689f0ea67e
commit
973ebb6e9d
@ -2,20 +2,29 @@ package de.w665.sharepulse.rest;
|
||||
|
||||
import de.w665.sharepulse.db.repo.FileUploadRepository;
|
||||
import de.w665.sharepulse.model.FileUpload;
|
||||
import de.w665.sharepulse.service.FileService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
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.*;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
@RestController
|
||||
public class TestRestResource extends ApiRestController {
|
||||
|
||||
private final FileUploadRepository fileUploadRepository;
|
||||
private final FileService fileService;
|
||||
|
||||
@Autowired
|
||||
public TestRestResource(FileUploadRepository fileUploadRepository) {
|
||||
public TestRestResource(FileUploadRepository fileUploadRepository, FileService fileService) {
|
||||
this.fileUploadRepository = fileUploadRepository;
|
||||
this.fileService = fileService;
|
||||
}
|
||||
|
||||
@GetMapping("test")
|
||||
@ -23,4 +32,26 @@ public class TestRestResource extends ApiRestController {
|
||||
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id);
|
||||
return fileUpload != null ? fileUpload.toString() : "FileUpload not found for id: " + id;
|
||||
}
|
||||
|
||||
@GetMapping("test/download")
|
||||
public ResponseEntity<Resource> download(@RequestParam String id) throws IOException {
|
||||
|
||||
File file = fileService.getFileById(id);
|
||||
|
||||
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"edmbass.flac\"");
|
||||
//headers.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + file.getName() + "\"");
|
||||
headers.add(HttpHeaders.CACHE_CONTROL, "no-cache, no-store, must-revalidate");
|
||||
headers.add(HttpHeaders.PRAGMA, "no-cache");
|
||||
headers.add(HttpHeaders.EXPIRES, "0");
|
||||
|
||||
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.headers(headers)
|
||||
.contentLength(file.length())
|
||||
.contentType(MediaType.APPLICATION_OCTET_STREAM)
|
||||
.body(resource);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user