diff --git a/src/main/java/ru/soune/nocopy/controller/FileController.java b/src/main/java/ru/soune/nocopy/controller/FileController.java index 38cf43e..4063e77 100644 --- a/src/main/java/ru/soune/nocopy/controller/FileController.java +++ b/src/main/java/ru/soune/nocopy/controller/FileController.java @@ -4,6 +4,7 @@ import com.vrt.NoCopyFileService; import com.vrt.fileprotection.FileProtector; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.core.io.InputStreamResource; import org.springframework.core.io.Resource; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; @@ -18,8 +19,11 @@ import ru.soune.nocopy.service.file.CheckCounterService; import ru.soune.nocopy.service.file.FileStorageService; 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 java.io.File; +import java.io.FileInputStream; import java.io.FileNotFoundException; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; @@ -51,6 +55,8 @@ public class FileController { private ZipService zipService; + private CloudStorageService cloudStorageService; + @GetMapping("/public/{fileId}") public ResponseEntity getPublicFile(@PathVariable String fileId) { try { @@ -63,7 +69,12 @@ public class FileController { return ResponseEntity.notFound().build(); } - Resource resource = fileStorageService.loadFileAsResource(fileEntity.getFilePath()); + File file = cloudStorageService.readFileFromStorageByPath(fileEntity.getFilePath()); + + if (file == null || !file.exists()) { + log.error("File not found in storage: {}", fileEntity.getFilePath()); + return ResponseEntity.notFound().build(); + } MediaType mediaType = getMediaType(fileEntity); @@ -71,13 +82,14 @@ public class FileController { .filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8) .build(); + InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); + return ResponseEntity.ok() .contentType(mediaType) .contentLength(Files.size(filePath)) .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString()) .header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600") .body(resource); - } catch (FileNotFoundException e) { log.warn("Not on disk: {}", fileId); return ResponseEntity.notFound().build(); @@ -113,9 +125,12 @@ public class FileController { FileEntity fileEntity = fileRepository.findById(fileId) .orElseThrow(() -> new RuntimeException("File not found")); - String path = imageResizeService.getPath(fileEntity, type); + File file = cloudStorageService.readFileFromStorageByPath(fileEntity.getFilePath()); - Resource resource = fileStorageService.loadFileAsResource(path); + if (file == null || !file.exists()) { + log.error("File not found in storage: {}", fileEntity.getFilePath()); + return ResponseEntity.notFound().build(); + } if (fileEntity.getProtectionStatus() == ProtectionStatus.NOT_PROTECTED || fileEntity.getProtectionStatus() == ProtectionStatus.PROCESSING) { @@ -128,6 +143,8 @@ public class FileController { String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8) .replace("+", "%20"); + InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); + return ResponseEntity.ok() .contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension())) .header(HttpHeaders.CONTENT_DISPOSITION,