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

updating files in a MinIO storage system #241

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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,10 @@
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.rmi.ServerException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

// TODO: Get should be viewed by both attestor and reviewer
Expand All @@ -24,7 +28,6 @@ public class FileStorageController {
private static final Logger logger = LoggerFactory.getLogger(FileStorageController.class);
private final FileStorageService fileStorageService;
private final RegistryHelper registryHelper;

FileStorageController(FileStorageService fileStorageService, RegistryHelper registryHelper) {
this.fileStorageService = fileStorageService;
this.registryHelper = registryHelper;
Expand Down Expand Up @@ -62,6 +65,23 @@ public ResponseEntity<DocumentsResponse> deleteMultipleFiles(@PathVariable Strin
return new ResponseEntity<>(documentsResponse, HttpStatus.OK);
}

@PutMapping("/api/v1/{entityName}/{entityId}/{property}/documents/{documentId}")
public ResponseEntity<DocumentsResponse> update(@RequestParam MultipartFile[] files,
@PathVariable String entityName,
@PathVariable String entityId,
@PathVariable String documentId,
@PathVariable String property,
HttpServletRequest httpServletRequest) {
try {
registryHelper.authorize(entityName, entityId, httpServletRequest);
} catch (Exception e) {
logger.error("An error occurred during authorization: {}", ExceptionUtils.getStackTrace(e));
return new ResponseEntity<>(HttpStatus.FORBIDDEN);
}
DocumentsResponse documentsResponse = fileStorageService.updateFiles(files, httpServletRequest.getRequestURI());
return new ResponseEntity<>(documentsResponse, HttpStatus.OK);
}

@DeleteMapping(value = "/api/v1/{entity}/{entityId}/{property}/documents/{documentId}")
public ResponseEntity deleteAFile(@PathVariable String entity,
@PathVariable String entityId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,4 +138,27 @@ public ComponentHealthInfo getHealthInfo() {
return new ComponentHealthInfo(getServiceName(), false, CONNECTION_FAILURE, e.getMessage());
}
}
public DocumentsResponse updateFiles(MultipartFile[] files, String requestedURI) {
String objectPath = getDirectoryPath(requestedURI);
DocumentsResponse documentsResponse = new DocumentsResponse();
for (MultipartFile file : files) {
String fileName = getFileName(file.getOriginalFilename());
try {
boolean objectExists = minioClient.statObject(
StatObjectArgs.builder().bucket(bucketName).object(objectPath).build()
) != null;

if (objectExists) {
save(file.getInputStream(),objectPath);
documentsResponse.addDocumentLocation(objectPath);
return documentsResponse;
}
} catch (Exception e) {
documentsResponse.addError(file.getOriginalFilename());
logger.error("Error has occurred while trying to update the file {}: {}", fileName, ExceptionUtils.getStackTrace(e));

}
}
return documentsResponse;
}
}
Loading