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 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 @@ -3,6 +3,10 @@
import dev.sunbirdrc.registry.helper.RegistryHelper;
import dev.sunbirdrc.registry.model.dto.DocumentsResponse;
import dev.sunbirdrc.registry.service.FileStorageService;
import io.minio.errors.*;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand All @@ -11,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 @@ -19,6 +27,7 @@ public class FileStorageController {
private final FileStorageService fileStorageService;
private final RegistryHelper registryHelper;

private static final Logger logger = LoggerFactory.getLogger(FileStorageController.class);
FileStorageController(FileStorageService fileStorageService, RegistryHelper registryHelper) {
this.fileStorageService = fileStorageService;
this.registryHelper = registryHelper;
Expand Down Expand Up @@ -56,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) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
chopadetejaswini marked this conversation as resolved.
Show resolved Hide resolved
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 @@ -9,6 +9,7 @@
import io.minio.messages.Bucket;
import io.minio.messages.DeleteError;
import io.minio.messages.DeleteObject;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.apache.poi.util.IOUtils;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
Expand Down Expand Up @@ -139,4 +140,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;
}
}