This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
package ru.soune.nocopy.controller;
|
||||
|
||||
import com.google.common.io.ByteStreams;
|
||||
import com.vrt.NoCopyFileService;
|
||||
import com.vrt.fileprotection.FileProtector;
|
||||
import com.vrt.fileprotection.NoCopyCheckResult;
|
||||
@@ -8,6 +7,8 @@ import com.vrt.fileprotection.audio.AudioCheckResult;
|
||||
import com.vrt.fileprotection.documents.DocumentCheckResult;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.core.io.InputStreamResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.Pageable;
|
||||
import org.springframework.data.web.PageableDefault;
|
||||
@@ -16,7 +17,6 @@ import org.springframework.validation.BindingResult;
|
||||
import org.springframework.validation.FieldError;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
|
||||
import ru.soune.nocopy.dto.BaseRequest;
|
||||
import ru.soune.nocopy.dto.BaseResponse;
|
||||
import ru.soune.nocopy.dto.MessageCode;
|
||||
@@ -47,7 +47,6 @@ import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URLEncoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@@ -164,7 +163,7 @@ public class ApiController {
|
||||
}
|
||||
|
||||
@GetMapping("/internal/data-download/{fileId}")
|
||||
public ResponseEntity<StreamingResponseBody> getPublicFile(@PathVariable String fileId) {
|
||||
public ResponseEntity<Resource> getPublicFile(@PathVariable String fileId) {
|
||||
try {
|
||||
FileEntity fileEntity = fileEntityRepository.findById(fileId)
|
||||
.orElseThrow(() -> new FileNotFoundException("Not found in DB: " + fileId));
|
||||
@@ -175,20 +174,14 @@ public class ApiController {
|
||||
.filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8)
|
||||
.build();
|
||||
|
||||
StreamingResponseBody responseBody = outputStream -> {
|
||||
try (InputStream inputStream = cloudStorageService.readFileFromStorage(fileEntity.getFilePath())) {
|
||||
ByteStreams.copy(inputStream, outputStream);
|
||||
} catch (NoSuchKeyException e) {
|
||||
log.error("File disappeared from S3 during streaming: {}", fileId, e);
|
||||
throw new IOException("File not found in storage", e);
|
||||
}
|
||||
};
|
||||
InputStream inputStream = cloudStorageService.readFileFromStorage(fileEntity.getFilePath());
|
||||
InputStreamResource resource = new InputStreamResource(inputStream);
|
||||
|
||||
return ResponseEntity.ok()
|
||||
.contentType(mediaType)
|
||||
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
|
||||
.header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600")
|
||||
.body(responseBody);
|
||||
.body(resource);
|
||||
} catch (FileNotFoundException e) {
|
||||
log.warn("File not found in DB: {}", fileId);
|
||||
return ResponseEntity.notFound().build();
|
||||
@@ -201,7 +194,6 @@ public class ApiController {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@PostMapping("/internal/data-control")
|
||||
public ResponseEntity<?> handleInternalControlPostRequest(@RequestBody BaseRequest request) {
|
||||
Integer msgId = request.getMsgId();
|
||||
|
||||
Reference in New Issue
Block a user