ng-tracker/src/main/java/ru/ulstu/file/FileController.java

49 lines
1.8 KiB
Java
Raw Normal View History

2018-05-05 00:24:48 +04:00
package ru.ulstu.file;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import ru.ulstu.configuration.Constants;
import ru.ulstu.core.model.response.Response;
import ru.ulstu.file.model.File;
import ru.ulstu.file.service.FileService;
import java.io.IOException;
import static ru.ulstu.paper.controller.PaperController.URL;
@RestController
@RequestMapping(URL)
public class FileController {
public static final String URL = Constants.API_1_0 + "files";
private final FileService fileService;
public FileController(FileService fileService) {
this.fileService = fileService;
}
@GetMapping("/download-tmp/{tmp-file-name}")
public ResponseEntity<byte[]> getFile(@PathVariable("tmp-file-name") String tmpFileName) throws IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename="
+ fileService.getTmpFileName(tmpFileName));
return new ResponseEntity<>(fileService.getTmpFile(tmpFileName), headers, HttpStatus.OK);
}
@GetMapping("/download/{file-id}")
public ResponseEntity<byte[]> getFile(@PathVariable("file-id") Integer fileId) {
HttpHeaders headers = new HttpHeaders();
File file = fileService.getFile(fileId);
headers.add("Content-Disposition", "attachment; filename=" + file.getName());
return new ResponseEntity<>(file.getData(), headers, HttpStatus.OK);
}
@PostMapping("/uploadTmpFile")
public Response<String> upload(@RequestBody MultipartFile multipartFile) throws IOException {
return new Response(fileService.uploadToTmpDir(multipartFile));
}
}