diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/FileStorageController.java b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/FileStorageController.java index e3ebca36f..afc0911df 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/controller/FileStorageController.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/controller/FileStorageController.java @@ -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 @@ -42,7 +44,33 @@ public ResponseEntity save(@RequestParam MultipartFile[] file 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 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); } @@ -72,9 +100,15 @@ public ResponseEntity deleteAFile(@PathVariable String entity, 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) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Failed deleting the document"); } - 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) @@ -92,7 +126,19 @@ public ResponseEntity get(@PathVariable String entity, 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("Failed fetching the document".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]; + } } diff --git a/java/registry/src/main/java/dev/sunbirdrc/registry/service/FileStorageService.java b/java/registry/src/main/java/dev/sunbirdrc/registry/service/FileStorageService.java index 172b3c276..455bc503b 100644 --- a/java/registry/src/main/java/dev/sunbirdrc/registry/service/FileStorageService.java +++ b/java/registry/src/main/java/dev/sunbirdrc/registry/service/FileStorageService.java @@ -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; @@ -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; @@ -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 files) { @@ -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 diff --git a/services/certificate-signer/Makefile b/services/certificate-signer/Makefile index 32100399a..1a0d49b9d 100644 --- a/services/certificate-signer/Makefile +++ b/services/certificate-signer/Makefile @@ -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: diff --git a/services/context-proxy-service/Makefile b/services/context-proxy-service/Makefile index e95d673e7..ea0e02a2e 100644 --- a/services/context-proxy-service/Makefile +++ b/services/context-proxy-service/Makefile @@ -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: diff --git a/services/public-key-service/Makefile b/services/public-key-service/Makefile index b5a58ba98..cb07da2b4 100644 --- a/services/public-key-service/Makefile +++ b/services/public-key-service/Makefile @@ -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: