RED-7140: changed to CompletableFuture with InputStreamResource to make it... #52

Merged
ali.oezyetimoglu1 merged 1 commits from RED-7140 into master 2023-07-17 08:56:02 +02:00
2 changed files with 28 additions and 22 deletions

View File

@ -7,6 +7,7 @@ import static com.iqser.red.service.persistence.management.v1.processor.utils.Do
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -196,15 +197,16 @@ public class DownloadController implements DownloadResource {
@PreAuthorize("hasAuthority('" + PROCESS_DOWNLOAD + "')")
public ResponseEntity<FileSystemResource> downloadFile(@RequestParam(STORAGE_ID) String storageId,
@RequestParam(value = "inline", required = false, defaultValue = FALSE) boolean inline) {
public CompletableFuture<ResponseEntity<InputStreamResource>> downloadFile(@RequestParam(STORAGE_ID) String storageId,
@RequestParam(value = "inline", required = false, defaultValue = FALSE) boolean inline) {
return CompletableFuture.supplyAsync(() -> {
var userId = KeycloakSecurity.getUserId();
var userId = KeycloakSecurity.getUserId();
var downloadStatus = getDownloadStatus(storageId, userId);
var fileDownloadStream = getFileForDownload(storageId, userId);
var downloadStatus = getDownloadStatus(storageId, userId);
var fileDownloadStream = getFileForDownload(storageId, userId);
return getResponseEntity(inline, fileDownloadStream, downloadStatus.getFilename(), MediaType.parseMediaType("application/zip"));
return getResponseEntity(inline, fileDownloadStream, downloadStatus.getFilename(), MediaType.parseMediaType("application/zip"), downloadStatus.getFileSize());
});
}
@ -238,15 +240,16 @@ public class DownloadController implements DownloadResource {
@SneakyThrows
private ResponseEntity<FileSystemResource> getResponseEntity(boolean inline, InputStreamResource resource, String filename, MediaType mediaType) {
private ResponseEntity<InputStreamResource> getResponseEntity(boolean inline, InputStreamResource resource, String filename, MediaType mediaType, long fileSize) {
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentType(mediaType);
httpHeaders.setContentLength(fileSize);
if (filename != null) {
httpHeaders.add("Content-Disposition", inline ? "inline" : "attachment" + "; filename*=utf-8''" + StringEncodingUtils.urlEncode(filename));
}
return new ResponseEntity<>(fileProxyStreamForDownload(resource.getInputStream()), httpHeaders, HttpStatus.OK);
return new ResponseEntity<>(resource, httpHeaders, HttpStatus.OK);
}
@ -261,21 +264,21 @@ public class DownloadController implements DownloadResource {
@Override
public ResponseEntity<FileSystemResource> downloadFileUsingOTT(@PathVariable(OTT) String oneTimeToken,
public CompletableFuture<ResponseEntity<InputStreamResource>> downloadFileUsingOTT(@PathVariable(OTT) String oneTimeToken,
@RequestParam(value = "inline", required = false, defaultValue = FALSE) boolean inline,
@RequestParam(value = "tenantId") String tenantId) {
return CompletableFuture.supplyAsync(() -> {
TenantContext.setTenantId(tenantId);
TenantContext.setTenantId(tenantId);
log.debug("downloadFileUsingOTT '{}'", oneTimeToken);
var token = oneTimeTokenDownloadService.getToken(oneTimeToken);
var downloadStatus = getDownloadStatus(token.getStorageId(), token.getUserId());
var fileDownloadStream = getFileForDownload(token.getStorageId(), token.getUserId());
log.debug("downloadFileUsingOTT '{}'", oneTimeToken);
var token = oneTimeTokenDownloadService.getToken(oneTimeToken);
var downloadStatus = getDownloadStatus(token.getStorageId(), token.getUserId());
var fileDownloadStream = getFileForDownload(token.getStorageId(), token.getUserId());
TenantContext.clear();
return getResponseEntity(inline, fileDownloadStream, downloadStatus.getFilename(), MediaType.parseMediaType("application/zip"));
TenantContext.clear();
return getResponseEntity(inline, fileDownloadStream, downloadStatus.getFilename(), MediaType.parseMediaType("application/zip"), downloadStatus.getFileSize());
});
}

View File

@ -1,6 +1,9 @@
package com.iqser.red.service.persistence.service.v1.api.external.resource;
import java.util.concurrent.CompletableFuture;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
@ -66,8 +69,8 @@ public interface DownloadResource {
@Operation(summary = "Returns a downloadable byte stream of the requested file", description = "Use the optional \"inline\" request parameter " + "to select, if this report will be opened in the browser.")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Download with this Id is no longer available")})
@GetMapping(value = REST_PATH)
ResponseEntity<FileSystemResource> downloadFile(@RequestParam(STORAGE_ID) String storageId,
@RequestParam(value = "inline", required = false, defaultValue = FALSE) boolean inline);
CompletableFuture<ResponseEntity<InputStreamResource>> downloadFile(@RequestParam(STORAGE_ID) String storageId,
@RequestParam(value = "inline", required = false, defaultValue = FALSE) boolean inline);
@ResponseBody
@ -83,7 +86,7 @@ public interface DownloadResource {
@Operation(summary = "Returns a downloadable byte stream of the requested file using a valid oneTimeToken", description = "Use the optional \"inline\" request parameter " + "to select, if this report will be opened in the browser.")
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK"), @ApiResponse(responseCode = "404", description = "Download with this Id is no longer available"), @ApiResponse(responseCode = "400", description = "OTT is not valid")})
@GetMapping(value = REST_PATH + OTT_PATH + OTT_PATH_VARIABLE)
ResponseEntity<FileSystemResource> downloadFileUsingOTT(@PathVariable(OTT) String oneTimeToken,
CompletableFuture<ResponseEntity<InputStreamResource>> downloadFileUsingOTT(@PathVariable(OTT) String oneTimeToken,
@RequestParam(value = "inline", required = false, defaultValue = FALSE) boolean inline,
@RequestParam(value = "tenantId") String tenantId);