From fdc17b83258762d417c3214aa50a10a08598f029 Mon Sep 17 00:00:00 2001 From: backdev-1 Date: Tue, 21 Apr 2026 02:21:33 +0700 Subject: [PATCH] dev delete check file on disk --- .../nocopy/controller/FileController.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/main/java/ru/soune/nocopy/controller/FileController.java b/src/main/java/ru/soune/nocopy/controller/FileController.java index 50a49ce..fa8edc0 100644 --- a/src/main/java/ru/soune/nocopy/controller/FileController.java +++ b/src/main/java/ru/soune/nocopy/controller/FileController.java @@ -22,13 +22,10 @@ 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.io.*; import java.net.URLEncoder; import java.nio.charset.StandardCharsets; -import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -58,17 +55,12 @@ public class FileController { private CloudStorageService cloudStorageService; @GetMapping("/public/{fileId}") - public ResponseEntity getPublicFile(@PathVariable String fileId) { + public ResponseEntity getPublicFile(@PathVariable String fileId) { try { FileEntity fileEntity = fileRepository.findById(fileId) .orElseThrow(() -> new FileNotFoundException("Not found: " + fileId)); - File file = cloudStorageService.readFileFromStorageByPath(fileEntity.getFilePath()); - - if (file == null || !file.exists()) { - log.error("File not found in storage: {}", fileEntity.getFilePath()); - return ResponseEntity.notFound().build(); - } + InputStream inputStream = cloudStorageService.readFileFromStorage(fileEntity.getFilePath()); MediaType mediaType = getMediaType(fileEntity); @@ -76,19 +68,17 @@ public class FileController { .filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8) .build(); - InputStreamResource resource = new InputStreamResource(new FileInputStream(file)); - return ResponseEntity.ok() .contentType(mediaType) - .contentLength(file.length()) .header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString()) .header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600") - .body(resource); + .body(new InputStreamResource(inputStream)); + } catch (FileNotFoundException e) { - log.warn("Not on disk: {}", fileId); + log.warn("File not found in DB: {}", fileId); return ResponseEntity.notFound().build(); - } catch (IOException e) { - log.error("Read file exception: {}", fileId, e); + } catch (Exception e) { + log.error("Error reading file: {}", fileId, e); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build(); } }