Refactored to optional type
This commit is contained in:
parent
f30961f1f0
commit
66d33e7e56
@ -14,6 +14,8 @@ import org.springframework.stereotype.Repository;
|
|||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.NoSuchElementException;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Repository
|
@Repository
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@ -44,11 +46,17 @@ public class FileUploadRepository {
|
|||||||
r.db("sharepulse").table("file_uploads").insert(map).run(connection);
|
r.db("sharepulse").table("file_uploads").insert(map).run(connection);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileUpload retrieveFileUploadByFileId(String fileId) {
|
public Optional<FileUpload> retrieveFileUploadByFileId(String fileId) {
|
||||||
return r.db("sharepulse").table("file_uploads")
|
try {
|
||||||
.filter(r.hashMap("fileId", fileId))
|
FileUpload fileUpload = r.db("sharepulse").table("file_uploads")
|
||||||
.run(connection, FileUpload.class)
|
.filter(r.hashMap("fileId", fileId))
|
||||||
.next();
|
.run(connection, FileUpload.class)
|
||||||
|
.next();
|
||||||
|
return Optional.ofNullable(fileUpload);
|
||||||
|
} catch (NoSuchElementException e) {
|
||||||
|
return Optional.empty();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateFileUpload(FileUpload updatedFileUpload) {
|
public void updateFileUpload(FileUpload updatedFileUpload) {
|
||||||
|
@ -28,9 +28,9 @@ public class TestRestResource extends ApiRestController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("test")
|
@GetMapping("test")
|
||||||
public String test(@RequestParam String id) {
|
public FileUpload test(@RequestParam String id) {
|
||||||
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id);
|
FileUpload fileUpload = fileUploadRepository.retrieveFileUploadByFileId(id).orElse(null);
|
||||||
return fileUpload != null ? fileUpload.toString() : "FileUpload not found for id: " + id;
|
return fileUpload;
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("test/download")
|
@GetMapping("test/download")
|
||||||
|
@ -22,6 +22,7 @@ import java.io.FileInputStream;
|
|||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@RestController
|
@RestController
|
||||||
@ -41,7 +42,11 @@ public class Download extends ApiRestController {
|
|||||||
@RequestParam(value = "password", required = false) String password) throws IOException {
|
@RequestParam(value = "password", required = false) String password) throws IOException {
|
||||||
|
|
||||||
|
|
||||||
FileUpload fileUpload = fileService.getFileUploadByFileId(fileId);
|
Optional<FileUpload> optionalFileUpload = fileService.getFileUploadByFileId(fileId);
|
||||||
|
FileUpload fileUpload = optionalFileUpload.orElse(null);
|
||||||
|
if(optionalFileUpload.isEmpty()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
fileSecurityService.verifyDownloadPermission(fileUpload, password);
|
fileSecurityService.verifyDownloadPermission(fileUpload, password);
|
||||||
@ -73,7 +78,11 @@ public class Download extends ApiRestController {
|
|||||||
|
|
||||||
@GetMapping("/download-info")
|
@GetMapping("/download-info")
|
||||||
public ResponseEntity<Object> getDownloadInfo(@RequestParam String fileId) {
|
public ResponseEntity<Object> getDownloadInfo(@RequestParam String fileId) {
|
||||||
FileUpload fileUpload = fileService.getFileUploadByFileId(fileId);
|
Optional<FileUpload> optionalFileUpload = fileService.getFileUploadByFileId(fileId);
|
||||||
|
FileUpload fileUpload = optionalFileUpload.orElse(null);
|
||||||
|
if(optionalFileUpload.isEmpty()) {
|
||||||
|
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
|
||||||
|
}
|
||||||
|
|
||||||
boolean downloadable = !fileUpload.isSingleDownload() || fileUpload.getDownloadCount() == 0;
|
boolean downloadable = !fileUpload.isSingleDownload() || fileUpload.getDownloadCount() == 0;
|
||||||
|
|
||||||
|
@ -17,6 +17,7 @@ import java.nio.file.Files;
|
|||||||
import java.nio.file.Path;
|
import java.nio.file.Path;
|
||||||
import java.nio.file.Paths;
|
import java.nio.file.Paths;
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
@ -104,7 +105,7 @@ public class FileService {
|
|||||||
return new File(getTempDirPath() + File.separator + fileId);
|
return new File(getTempDirPath() + File.separator + fileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public FileUpload getFileUploadByFileId(String fileId) {
|
public Optional<FileUpload> getFileUploadByFileId(String fileId) {
|
||||||
return fileUploadRepository.retrieveFileUploadByFileId(fileId);
|
return fileUploadRepository.retrieveFileUploadByFileId(fileId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user