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

54 lines
2.1 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;
2018-05-05 10:57:02 +04:00
import ru.ulstu.file.model.FileData;
2018-05-05 00:24:48 +04:00
import ru.ulstu.file.service.FileService;
import java.io.IOException;
2018-05-05 10:57:02 +04:00
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
2018-05-05 00:24:48 +04:00
2018-05-05 10:57:02 +04:00
import static java.nio.charset.StandardCharsets.UTF_8;
2018-05-05 00:24:48 +04:00
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();
2018-05-05 10:57:02 +04:00
headers.add("Content-Disposition", "attachment; filename='"
+ URLEncoder.encode(fileService.getTmpFileName(tmpFileName), UTF_8.toString()) + "'");
2018-05-05 00:24:48 +04:00
return new ResponseEntity<>(fileService.getTmpFile(tmpFileName), headers, HttpStatus.OK);
}
@GetMapping("/download/{file-id}")
2018-05-05 10:57:02 +04:00
public ResponseEntity<byte[]> getFile(@PathVariable("file-id") Integer fileId) throws UnsupportedEncodingException {
2018-05-05 00:24:48 +04:00
HttpHeaders headers = new HttpHeaders();
2018-05-05 10:57:02 +04:00
FileData fileData = fileService.getFile(fileId);
headers.add("Content-Disposition", "attachment; filename='" +
URLEncoder.encode(fileData.getName(), UTF_8.toString()) + "'");
return new ResponseEntity<>(fileData.getData(), headers, HttpStatus.OK);
2018-05-05 00:24:48 +04:00
}
@PostMapping("/uploadTmpFile")
public Response<String> upload(@RequestBody MultipartFile multipartFile) throws IOException {
return new Response(fileService.uploadToTmpDir(multipartFile));
}
}