dev download only protected files
Test Workflow / test (push) Successful in 3s

This commit is contained in:
vladp
2026-01-28 18:38:43 +07:00
parent 02bf7868ee
commit c019f23777
4 changed files with 7 additions and 25 deletions
@@ -11,6 +11,7 @@ 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;
import ru.soune.nocopy.entity.file.ProtectionStatus;
import ru.soune.nocopy.repository.FileEntityRepository;
import ru.soune.nocopy.service.file.FileStorageService;
@@ -34,11 +35,11 @@ public class FileController {
FileEntity fileEntity = fileRepository.findById(fileId)
.orElseThrow(() -> new RuntimeException("File not found"));
// if (fileEntity.getProtectionStatus() != ProtectionStatus.NOT_PROTECTED) {
// throw new RuntimeException("File is protected");
// }
if (fileEntity.getProtectionStatus() != ProtectionStatus.NOT_PROTECTED) {
throw new RuntimeException("File is protected");
}
Resource resource = fileStorageService.loadFileAsResource(fileEntity.getFilePath());
Resource resource = fileStorageService.loadFileAsResource(fileEntity.getProtectedFilePath());
return ResponseEntity.ok()
.contentType(new MediaType(fileEntity.getMimeType(), fileEntity.getFileExtension()))
@@ -46,19 +47,4 @@ public class FileController {
"inline; filename=\"" + fileEntity.getOriginalFileName() + "\"")
.body(resource);
}
@GetMapping("/download/{fileId}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileId) throws IOException {
FileEntity fileEntity = fileRepository.findById(fileId)
.orElseThrow(() -> new RuntimeException("File not found"));
Resource resource = fileStorageService.loadFileAsResource(fileEntity.getFilePath());
return ResponseEntity.ok()
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + fileEntity.getOriginalFileName() + "\"")
.body(resource);
}
}