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;
|
2026-01-28 23:25:16 +07:00
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
|
import ru.soune.nocopy.dto.file.CheckStatus;
|
2026-01-27 20:27:39 +07:00
|
|
|
import ru.soune.nocopy.entity.file.FileEntity;
|
2026-01-28 18:38:43 +07:00
|
|
|
import ru.soune.nocopy.entity.file.ProtectionStatus;
|
2026-01-28 23:25:16 +07:00
|
|
|
import ru.soune.nocopy.entity.user.ProtectedFileCheck;
|
|
|
|
|
import ru.soune.nocopy.entity.user.User;
|
2026-01-27 20:27:39 +07:00
|
|
|
import ru.soune.nocopy.repository.FileEntityRepository;
|
2026-01-28 23:25:16 +07:00
|
|
|
import ru.soune.nocopy.repository.UserRepository;
|
|
|
|
|
import ru.soune.nocopy.service.file.CheckCounterService;
|
2026-01-27 20:27:39 +07:00
|
|
|
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;
|
|
|
|
|
|
2026-01-28 23:25:16 +07:00
|
|
|
@Autowired
|
|
|
|
|
private CheckCounterService checkCounterService;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private UserRepository userRepository;
|
|
|
|
|
|
2026-01-27 20:27:39 +07:00
|
|
|
@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-29 13:08:43 +07:00
|
|
|
if (fileEntity.getProtectionStatus() == ProtectionStatus.NOT_PROTECTED) {
|
|
|
|
|
throw new RuntimeException("File is not protected");
|
2026-01-28 18:38:43 +07:00
|
|
|
}
|
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);
|
|
|
|
|
}
|
2026-01-28 23:25:16 +07:00
|
|
|
|
|
|
|
|
@GetMapping("check-file/status/{userId}")
|
|
|
|
|
public ResponseEntity<CheckStatus> getStatus(@PathVariable Long userId) {
|
|
|
|
|
CheckStatus status = checkCounterService.getCurrentStatus(userId);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity.ok(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@PostMapping("check-file/update-limit/{userId}/{limit}")
|
|
|
|
|
public ResponseEntity<ProtectedFileCheck> getStatus(@PathVariable Long userId, @PathVariable Integer limit) {
|
|
|
|
|
User user = userRepository.findById(userId).orElseThrow();
|
|
|
|
|
ProtectedFileCheck protectedFileCheck = checkCounterService.updateLimit(user, limit);
|
|
|
|
|
|
|
|
|
|
return ResponseEntity
|
|
|
|
|
.ok()
|
|
|
|
|
.body(protectedFileCheck);
|
|
|
|
|
}
|
2026-01-27 20:27:39 +07:00
|
|
|
}
|