Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewBemis committed Dec 13, 2024
1 parent ff916ba commit 91a8999
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 108 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package bio.terra.pearl.api.participant.controller.fileupload;
package bio.terra.pearl.api.participant.controller.file;

import bio.terra.pearl.api.participant.api.ParticipantFileApi;
import bio.terra.pearl.api.participant.service.RequestUtilService;
import bio.terra.pearl.api.participant.service.fileupload.ParticipantFileExtService;
import bio.terra.pearl.core.model.fileupload.ParticipantFile;
import bio.terra.pearl.api.participant.service.file.ParticipantFileExtService;
import bio.terra.pearl.core.model.file.ParticipantFile;
import bio.terra.pearl.core.model.participant.ParticipantUser;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
Expand Down Expand Up @@ -41,10 +41,10 @@ public ResponseEntity<Resource> download(
String enrolleeShortcode,
String fileName) {
ParticipantUser participantUser = requestUtilService.requireUser(request);

// todo verify portalShortcode, studyShortcode, envName

ParticipantFile participantFile =
participantFileExtService.get(participantUser, enrolleeShortcode, fileName);

InputStream content =
participantFileExtService.downloadFile(participantUser, enrolleeShortcode, fileName);

Expand All @@ -67,7 +67,6 @@ public ResponseEntity<Object> upload(
MultipartFile participantFile) {
ParticipantUser participantUser = requestUtilService.requireUser(request);

// todo verify portalShortcode, studyShortcode, envName
ParticipantFile created =
participantFileExtService.uploadFile(participantUser, enrolleeShortcode, participantFile);

Expand All @@ -79,7 +78,6 @@ public ResponseEntity<Object> list(
String portalShortcode, String envName, String studyShortcode, String enrolleeShortcode) {
ParticipantUser participantUser = requestUtilService.requireUser(request);

// todo verify portalShortcode, studyShortcode, envName
return ResponseEntity.ok(participantFileExtService.list(participantUser, enrolleeShortcode));
}

Expand All @@ -92,7 +90,6 @@ public ResponseEntity<Object> delete(
String fileName) {
ParticipantUser participantUser = requestUtilService.requireUser(request);

// todo verify portalShortcode, studyShortcode, envName
participantFileExtService.delete(participantUser, enrolleeShortcode, fileName);

return ResponseEntity.noContent().build();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package bio.terra.pearl.api.participant.service.file;

import bio.terra.pearl.api.participant.service.AuthUtilService;
import bio.terra.pearl.core.model.EnvironmentName;
import bio.terra.pearl.core.model.file.ParticipantFile;
import bio.terra.pearl.core.model.participant.Enrollee;
import bio.terra.pearl.core.model.participant.ParticipantUser;
import bio.terra.pearl.core.service.exception.NotFoundException;
import bio.terra.pearl.core.service.file.ParticipantFileService;
import bio.terra.pearl.core.service.file.backends.FileStorageBackend;
import bio.terra.pearl.core.service.file.backends.FileStorageBackendProvider;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

@Service
@Slf4j
public class ParticipantFileExtService {

private final ParticipantFileService participantFileService;
private final AuthUtilService authUtilService;
private final FileStorageBackend fileStorageBackend;

public ParticipantFileExtService(
ParticipantFileService participantFileService,
AuthUtilService authUtilService,
FileStorageBackendProvider fileStorageBackendProvider) {
this.participantFileService = participantFileService;
this.authUtilService = authUtilService;
this.fileStorageBackend = fileStorageBackendProvider.get();
}

public ParticipantFile get(
ParticipantUser participantUser, String enrolleeShortcode, String fileName) {
Enrollee enrollee =
authUtilService.authParticipantUserToEnrollee(participantUser.getId(), enrolleeShortcode);
return participantFileService
.findByEnrolleeIdAndFileName(enrollee.getId(), fileName)
.orElseThrow(() -> new NotFoundException("Could not find file"));
}

public InputStream downloadFile(
ParticipantUser participantUser, String enrolleeShortcode, String fileName) {
Enrollee enrollee =
authUtilService.authParticipantUserToEnrollee(participantUser.getId(), enrolleeShortcode);
ParticipantFile participantFile =
participantFileService
.findByEnrolleeIdAndFileName(enrollee.getId(), fileName)
.orElseThrow(() -> new NotFoundException("Could not find file"));

return fileStorageBackend.downloadFile(participantFile.getExternalFileId());
}

public ParticipantFile uploadFile(
ParticipantUser participantUser, String enrolleeShortcode, MultipartFile file) {
Enrollee enrollee =
authUtilService.authParticipantUserToEnrollee(participantUser.getId(), enrolleeShortcode);

try {
return participantFileService.uploadFileAndCreate(
ParticipantFile.builder()
.enrolleeId(enrollee.getId())
.fileName(cleanFileName(file.getOriginalFilename()))
.fileType(file.getContentType())
.build(),
file.getInputStream());
} catch (IOException e) {
throw new RuntimeException("Error uploading file");
}
}

public List<ParticipantFile> list(ParticipantUser participantUser, String enrolleeShortcode) {
Enrollee enrollee =
authUtilService.authParticipantUserToEnrollee(participantUser.getId(), enrolleeShortcode);
return participantFileService.findByEnrolleeId(enrollee.getId());
}

public String cleanFileName(String fileName) {
if (fileName == null) {
return "";
}
String[] split = fileName.split("\\[/\\\\]");
return split[split.length - 1];
}

public void delete(ParticipantUser participantUser, String enrolleeShortcode, String fileName) {
Enrollee enrollee =
authUtilService.authParticipantUserToEnrollee(participantUser.getId(), enrolleeShortcode);
ParticipantFile participantFile =
participantFileService
.findByEnrolleeIdAndFileName(enrollee.getId(), fileName)
.orElseThrow(() -> new NotFoundException("Could not find file"));

participantFileService.delete(participantFile.getId(), Set.of());
fileStorageBackend.deleteFile(participantFile.getExternalFileId());
}
}

This file was deleted.

0 comments on commit 91a8999

Please sign in to comment.