Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue-504]: Added update document support #275

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

// TODO: Get should be viewed by both attestor and reviewer
Expand Down Expand Up @@ -42,7 +44,33 @@
logger.error("Authorizing entity failed: {}", ExceptionUtils.getStackTrace(e));
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
DocumentsResponse documentsResponse = fileStorageService.saveAndFetchFileNames(files, httpServletRequest.getRequestURI());
String objectPath = getDirectoryPath(httpServletRequest.getRequestURI());
DocumentsResponse documentsResponse = fileStorageService.saveAndFetchFileNames(files, objectPath);
return new ResponseEntity<>(documentsResponse, HttpStatus.OK);
}

@PutMapping("/api/v1/{entity}/{entityId}/{property}/documents/{documentId}")
public ResponseEntity<DocumentsResponse> update(@RequestParam MultipartFile file,
@PathVariable String entity,
@PathVariable String entityId,
HttpServletRequest httpServletRequest) {
try {
registryHelper.authorize(entity, entityId, httpServletRequest);
} catch (Exception e) {
logger.error("Authorizing entity failed: {}", ExceptionUtils.getStackTrace(e));
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
String objectPath = getDirectoryPath(httpServletRequest.getRequestURI());
try {
fileStorageService.deleteDocument(objectPath);
} catch (Exception e) {
DocumentsResponse documentsResponse = new DocumentsResponse();
documentsResponse.setErrors(Collections.singletonList(e.getMessage()));
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(documentsResponse);
}
objectPath = objectPath.substring(0, objectPath.lastIndexOf("/"));
DocumentsResponse documentsResponse = fileStorageService.saveAndFetchFileNames(new MultipartFile[] {file}, objectPath);
return new ResponseEntity<>(documentsResponse, HttpStatus.OK);
}

Expand Down Expand Up @@ -72,9 +100,16 @@
registryHelper.authorize(entity, entityId, httpServletRequest);
} catch (Exception e) {
logger.error("Authorizing entity failed: {}", ExceptionUtils.getStackTrace(e));
return new ResponseEntity(HttpStatus.FORBIDDEN);
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
String objectName = getDirectoryPath(httpServletRequest.getRequestURI());
try {
fileStorageService.deleteDocument(objectName);
} catch (Exception e) {
String errorMsg = e.getMessage();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg);
}
return fileStorageService.deleteDocument(httpServletRequest.getRequestURI());
return ResponseEntity.ok(HttpStatus.OK);
}

@GetMapping(value = "/api/v1/{entity}/{entityId}/{property}/documents/{documentId}", produces = MediaType.APPLICATION_OCTET_STREAM_VALUE)
Expand All @@ -92,7 +127,19 @@
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
}
byte[] document = fileStorageService.getDocument(httpServletRequest.getRequestURI());
String objectName = getDirectoryPath(httpServletRequest.getRequestURI());
byte[] document;
try {
document = fileStorageService.getDocument(objectName);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage().getBytes(StandardCharsets.UTF_8));
}
return ResponseEntity.ok().body(document);
}

private String getDirectoryPath(String requestedURI) {
String versionDelimiter = "/v1/";
String[] split = requestedURI.split(versionDelimiter);
return split[1];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
Expand All @@ -52,19 +54,17 @@ public void save(InputStream inputStream, String objectName) throws Exception {
logger.info("File has successfully saved");
}

public DocumentsResponse saveAndFetchFileNames(MultipartFile[] files, String requestedURI) {
String objectPath = getDirectoryPath(requestedURI);
public DocumentsResponse saveAndFetchFileNames(MultipartFile[] files, String objectPath) {

DocumentsResponse documentsResponse = new DocumentsResponse();
for (MultipartFile file : files) {
String fileName = getFileName(file.getOriginalFilename());
for (MultipartFile file: files) {
String objectName = objectPath + "/" + getFileName(Objects.requireNonNull(file.getOriginalFilename()));
try {
String objectName = objectPath + "/" + fileName;
save(file.getInputStream(), objectName);
documentsResponse.addDocumentLocation(objectName);
} catch (Exception e) {
documentsResponse.addError(file.getOriginalFilename());
logger.error("Error has occurred while trying to save the file {}: {}", fileName, ExceptionUtils.getStackTrace(e));
logger.error("Error has occurred while trying to save the file {}: {}", file.getOriginalFilename(), ExceptionUtils.getStackTrace(e));
}
}
return documentsResponse;
Expand All @@ -79,7 +79,7 @@ private String getDirectoryPath(String requestedURI) {
@NotNull
private String getFileName(String file) {
String uuid = UUID.randomUUID().toString();
return uuid + "-" + file;
return uuid + "-" + file.replaceAll(" ", "_");
}

public DocumentsResponse deleteFiles(List<String> files) {
Expand All @@ -100,27 +100,23 @@ public String getSignedUrl(String objectName) throws ServerException, Insufficie
return minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().method(Method.GET).bucket(bucketName).object(objectName).expiry(2, TimeUnit.HOURS).build());
}

public byte[] getDocument(String requestedURI) {
String objectName = getDirectoryPath(requestedURI);
byte[] bytes = new byte[0];
public byte[] getDocument(String objectName) throws Exception {
try {
InputStream inputStream = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(objectName).build());
bytes = IOUtils.toByteArray(inputStream);
return IOUtils.toByteArray(inputStream);
} catch (Exception e) {
logger.error("Error has occurred while fetching the document {} {}", objectName, ExceptionUtils.getStackTrace(e));
throw e;
}
return bytes;
}

public ResponseEntity deleteDocument(String requestedURI) {
String objectName = getDirectoryPath(requestedURI);
public void deleteDocument(String objectName) throws Exception {
try {
minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucketName).object(objectName).build());
} catch (Exception e) {
logger.error("Error has occurred while deleting the document {}", objectName);
return new ResponseEntity(HttpStatus.BAD_REQUEST);
throw e;
}
return new ResponseEntity(HttpStatus.OK);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion services/certificate-signer/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test: docker
@docker-compose -f api-test-docker-compose.yml up -d
@docker-compose -f api-test-docker-compose.yml run test-runner
#@papermill --log-output certificate-signer.ipynb out
@docker-compose down
@docker-compose -f api-test-docker-compose.yml down
docker:
@docker build -t $(IMAGE) .
publish:
Expand Down
2 changes: 1 addition & 1 deletion services/context-proxy-service/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test: docker
@docker-compose -f api-test-docker-compose.yml up -d context-proxy-service
@docker-compose -f api-test-docker-compose.yml run test-runner
#@papermill --log-output certificate-signer.ipynb out
@docker-compose down
@docker-compose -f api-test-docker-compose.yml down
docker:
@docker build -t $(IMAGE) .
publish:
Expand Down
2 changes: 1 addition & 1 deletion services/public-key-service/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ test: docker
@docker-compose -f api-test-docker-compose.yml up -d
@docker-compose -f api-test-docker-compose.yml run test-runner
#@papermill --log-output certificate-signer.ipynb out
@docker-compose down
@docker-compose -f api-test-docker-compose.yml down
docker:
@docker build -t $(IMAGE) .
publish:
Expand Down
Loading