This commit is contained in:
@@ -4,6 +4,7 @@ import com.vrt.NoCopyFileService;
|
|||||||
import com.vrt.fileprotection.FileProtector;
|
import com.vrt.fileprotection.FileProtector;
|
||||||
import lombok.AllArgsConstructor;
|
import lombok.AllArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
import org.springframework.core.io.Resource;
|
import org.springframework.core.io.Resource;
|
||||||
import org.springframework.http.*;
|
import org.springframework.http.*;
|
||||||
import org.springframework.web.bind.annotation.*;
|
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.FileStorageService;
|
||||||
import ru.soune.nocopy.service.file.ImageResizeService;
|
import ru.soune.nocopy.service.file.ImageResizeService;
|
||||||
import ru.soune.nocopy.service.file.ZipService;
|
import ru.soune.nocopy.service.file.ZipService;
|
||||||
|
import ru.soune.nocopy.service.file.cloud.CloudStorageService;
|
||||||
import ru.soune.nocopy.util.FileUtil;
|
import ru.soune.nocopy.util.FileUtil;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
import java.io.FileNotFoundException;
|
import java.io.FileNotFoundException;
|
||||||
import java.net.URLEncoder;
|
import java.net.URLEncoder;
|
||||||
import java.nio.charset.StandardCharsets;
|
import java.nio.charset.StandardCharsets;
|
||||||
@@ -51,6 +55,8 @@ public class FileController {
|
|||||||
|
|
||||||
private ZipService zipService;
|
private ZipService zipService;
|
||||||
|
|
||||||
|
private CloudStorageService cloudStorageService;
|
||||||
|
|
||||||
@GetMapping("/public/{fileId}")
|
@GetMapping("/public/{fileId}")
|
||||||
public ResponseEntity<Resource> getPublicFile(@PathVariable String fileId) {
|
public ResponseEntity<Resource> getPublicFile(@PathVariable String fileId) {
|
||||||
try {
|
try {
|
||||||
@@ -63,7 +69,12 @@ public class FileController {
|
|||||||
return ResponseEntity.notFound().build();
|
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);
|
MediaType mediaType = getMediaType(fileEntity);
|
||||||
|
|
||||||
@@ -71,13 +82,14 @@ public class FileController {
|
|||||||
.filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8)
|
.filename(fileEntity.getOriginalFileName(), StandardCharsets.UTF_8)
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
|
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(mediaType)
|
.contentType(mediaType)
|
||||||
.contentLength(Files.size(filePath))
|
.contentLength(Files.size(filePath))
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
|
.header(HttpHeaders.CONTENT_DISPOSITION, contentDisposition.toString())
|
||||||
.header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600")
|
.header(HttpHeaders.CACHE_CONTROL, "public, max-age=3600")
|
||||||
.body(resource);
|
.body(resource);
|
||||||
|
|
||||||
} catch (FileNotFoundException e) {
|
} catch (FileNotFoundException e) {
|
||||||
log.warn("Not on disk: {}", fileId);
|
log.warn("Not on disk: {}", fileId);
|
||||||
return ResponseEntity.notFound().build();
|
return ResponseEntity.notFound().build();
|
||||||
@@ -113,9 +125,12 @@ public class FileController {
|
|||||||
FileEntity fileEntity = fileRepository.findById(fileId)
|
FileEntity fileEntity = fileRepository.findById(fileId)
|
||||||
.orElseThrow(() -> new RuntimeException("File not found"));
|
.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 ||
|
if (fileEntity.getProtectionStatus() == ProtectionStatus.NOT_PROTECTED ||
|
||||||
fileEntity.getProtectionStatus() == ProtectionStatus.PROCESSING) {
|
fileEntity.getProtectionStatus() == ProtectionStatus.PROCESSING) {
|
||||||
@@ -128,6 +143,8 @@ public class FileController {
|
|||||||
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8)
|
String encodedFileName = URLEncoder.encode(fileName, StandardCharsets.UTF_8)
|
||||||
.replace("+", "%20");
|
.replace("+", "%20");
|
||||||
|
|
||||||
|
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
|
||||||
|
|
||||||
return ResponseEntity.ok()
|
return ResponseEntity.ok()
|
||||||
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
||||||
.header(HttpHeaders.CONTENT_DISPOSITION,
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
||||||
|
|||||||
Reference in New Issue
Block a user