Skip to content

Commit

Permalink
Merge pull request #461 from molgenis/fix/#458-cannot-assign-expr-lar…
Browse files Browse the repository at this point in the history
…ge-resources

fix: retrieve filesize of resource from filepath
  • Loading branch information
marikaris authored Jul 21, 2023
2 parents f6c4a09 + e19dc5b commit 1e2ee23
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.molgenis.armadillo.audit.AuditEventPublisher;
import org.molgenis.armadillo.exceptions.FileProcessingException;
import org.molgenis.armadillo.storage.ArmadilloStorageService;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.ContentDisposition;
import org.springframework.http.HttpHeaders;
Expand Down Expand Up @@ -257,7 +256,7 @@ public void deleteObject(
@GetMapping(
value = "/projects/{project}/objects/{object}",
produces = {APPLICATION_OCTET_STREAM_VALUE})
public @ResponseBody ResponseEntity<ByteArrayResource> downloadObject(
public @ResponseBody ResponseEntity<InputStreamResource> downloadObject(
Principal principal, @PathVariable String project, @PathVariable String object) {
return auditor.audit(
() -> getObject(project, object),
Expand All @@ -267,19 +266,21 @@ public void deleteObject(
}

@PreAuthorize("hasAnyRole('ROLE_SU', 'ROLE_' + #project.toUpperCase() + '_RESEARCHER')")
private ResponseEntity<ByteArrayResource> getObject(String project, String object) {
private ResponseEntity<InputStreamResource> getObject(String project, String object) {
var inputStream = storage.loadObject(project, object);
var objectParts = object.split("/");
var fileName = objectParts[objectParts.length - 1];

try {
InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
long length = inputStream.available();
ContentDisposition contentDisposition =
ContentDisposition.attachment().filename(fileName).build();
var fileSize =
storage.getFileSizeIfObjectExists(
ArmadilloStorageService.SHARED_PREFIX + project, object);
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.setContentDisposition(contentDisposition);
httpHeaders.setContentLength(length);
httpHeaders.setContentLength(fileSize);
httpHeaders.setContentType(APPLICATION_OCTET_STREAM);
return new ResponseEntity(inputStreamResource, httpHeaders, HttpStatus.OK);
} catch (IOException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import static org.apache.commons.io.FilenameUtils.removeExtension;
import static org.springframework.http.MediaType.APPLICATION_OCTET_STREAM;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.Principal;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -229,6 +232,11 @@ static void validateProjectName(String projectName) {
}
}

public long getFileSizeIfObjectExists(String bucketName, String objectName) throws IOException {
Path filePath = storageService.getPathIfObjectExists(bucketName, objectName);
return Files.size(filePath);
}

@PreAuthorize("hasRole('ROLE_SU')")
public List<Map<String, String>> getPreview(String project, String object) {
throwIfUnknown(project, object);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ public List<Map<String, String>> preview(
}
}

private Path getPathIfObjectExists(String bucketName, String objectName) {
public Path getPathIfObjectExists(String bucketName, String objectName) {
Path bucketPath = Paths.get(rootDir, bucketName);
if (!Files.exists(bucketPath)) {
throw new StorageException(format("Bucket '%s' doesn't exist", bucketName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.minio.messages.Bucket;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.List;
Expand Down Expand Up @@ -231,4 +232,9 @@ public void delete(String projectName, String objectName) {
throw new StorageException(e);
}
}

@Override
public Path getPathIfObjectExists(String bucketName, String objectName) {
return null;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.molgenis.armadillo.storage;

import java.io.InputStream;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.springframework.http.MediaType;
Expand All @@ -24,4 +25,6 @@ List<Map<String, String>> preview(
String bucketName, String objectName, int rowLimit, int columnLimit);

void delete(String bucketName, String objectName);

public Path getPathIfObjectExists(String bucketName, String objectName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -387,7 +388,9 @@ void deleteObjectNotExists() throws Exception {
void downloadObject() throws Exception {
var content = "content".getBytes();
var inputStream = new ByteArrayInputStream(content);
Path mockPath = mock(Path.class);
when(storage.loadObject("lifecycle", "test.parquet")).thenReturn(inputStream);
when(storage.getFileSizeIfObjectExists("shared-lifecycle", "test.parquet")).thenReturn(12345L);

mockMvc
.perform(get("/storage/projects/lifecycle/objects/test.parquet").session(session))
Expand Down

0 comments on commit 1e2ee23

Please sign in to comment.