dev test baseurl for test
Test Workflow / test (push) Has been cancelled

This commit is contained in:
2026-04-21 11:50:25 +07:00
parent 1dd6a6b7e2
commit d0be6580e4
2 changed files with 34 additions and 9 deletions
@@ -1,5 +1,7 @@
package ru.soune.nocopy.controller;
import com.google.api.client.util.IOUtils;
import com.google.common.io.ByteStreams;
import com.vrt.NoCopyFileService;
import com.vrt.fileprotection.FileProtector;
import lombok.AllArgsConstructor;
@@ -8,6 +10,7 @@ import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import ru.soune.nocopy.dto.file.CheckStatus;
import ru.soune.nocopy.entity.file.FileEntity;
import ru.soune.nocopy.entity.file.ProtectionStatus;
@@ -21,6 +24,7 @@ import ru.soune.nocopy.service.file.ImageResizeService;
import ru.soune.nocopy.service.file.ZipService;
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
import ru.soune.nocopy.util.FileUtil;
import software.amazon.awssdk.services.s3.model.NoSuchKeyException;
import java.io.*;
import java.net.URLEncoder;
@@ -55,12 +59,10 @@ public class FileController {
private CloudStorageService cloudStorageService;
@GetMapping("/public/{fileId}")
public ResponseEntity<InputStreamResource> getPublicFile(@PathVariable String fileId) {
public ResponseEntity<StreamingResponseBody> getPublicFile(@PathVariable String fileId) {
try {
FileEntity fileEntity = fileRepository.findById(fileId)
.orElseThrow(() -> new FileNotFoundException("Not found: " + fileId));
InputStream inputStream = cloudStorageService.readFileFromStorage(fileEntity.getFilePath());
.orElseThrow(() -> new FileNotFoundException("Not found in DB: " + fileId));
MediaType mediaType = getMediaType(fileEntity);
@@ -68,15 +70,26 @@ public class FileController {
.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);
}
};
return ResponseEntity.ok()
.contentType(mediaType)
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
.header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600")
.body(new InputStreamResource(inputStream));
.body(responseBody);
} catch (FileNotFoundException e) {
log.warn("File not found in DB: {}", fileId);
return ResponseEntity.notFound().build();
} catch (NoSuchKeyException e) {
log.warn("File not found in S3: {}", fileId);
return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
} catch (Exception e) {
log.error("Error reading file: {}", fileId, e);
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();