2026-01-27 20:27:39 +07:00
|
|
|
package ru.soune.nocopy.controller;
|
|
|
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.core.io.Resource;
|
|
|
|
|
import org.springframework.http.HttpHeaders;
|
|
|
|
|
import org.springframework.http.MediaType;
|
|
|
|
|
import org.springframework.http.ResponseEntity;
|
|
|
|
|
import org.springframework.web.bind.annotation.GetMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
2026-01-28 18:38:43 +07:00
|
|
|
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
2026-01-27 20:27:39 +07:00
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
|
|
|
|
import ru.soune.nocopy.service.file.FileStorageService;
|
|
|
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/api/files")
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class FileController {
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private FileStorageService fileStorageService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private FileEntityRepository fileRepository;
|
|
|
|
|
|
|
|
|
|
@GetMapping("/public/{fileId}")
|
|
|
|
|
public ResponseEntity<Resource> getPublicFile(
|
|
|
|
|
@PathVariable String fileId) throws IOException {
|
|
|
|
|
|
|
|
|
|
FileEntity fileEntity = fileRepository.findById(fileId)
|
|
|
|
|
.orElseThrow(() -> new RuntimeException("File not found"));
|
|
|
|
|
|
2026-01-28 18:38:43 +07:00
|
|
|
if (fileEntity.getProtectionStatus() != ProtectionStatus.NOT_PROTECTED) {
|
|
|
|
|
throw new RuntimeException("File is protected");
|
|
|
|
|
}
|
2026-01-27 20:27:39 +07:00
|
|
|
|
2026-01-28 18:38:43 +07:00
|
|
|
Resource resource = fileStorageService.loadFileAsResource(fileEntity.getProtectedFilePath());
|
2026-01-27 20:27:39 +07:00
|
|
|
|
|
|
|
|
return ResponseEntity.ok()
|
|
|
|
|
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
|
|
|
|
|
.header(HttpHeaders.CONTENT_DISPOSITION,
|
|
|
|
|
"inline; filename=\"" + fileEntity.getOriginalFileName() + "\"")
|
|
|
|
|
.body(resource);
|
|
|
|
|
}
|
|
|
|
|
}
|