Skip to content

Commit

Permalink
Added update document in file storage service
Browse files Browse the repository at this point in the history
  • Loading branch information
holashchand committed Jan 25, 2024
1 parent cdf08c0 commit 8bd1622
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 20 deletions.
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 @@ public ResponseEntity<DocumentsResponse> 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<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,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(e.getMessage());

Check warning

Code scanning / CodeQL

Information exposure through a stack trace Medium

Error information
can be exposed to an external user.
}
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 +126,19 @@ public ResponseEntity<byte[]> 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(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

0 comments on commit 8bd1622

Please sign in to comment.