Skip to content

Commit

Permalink
Bxc 4771 upper file limit (#1835)
Browse files Browse the repository at this point in the history
* BXC-4771 add download file limit

* BXC-4771 cleanup

* BXC-4771 set file limit in test

* BXC-4771 DRY up code and update limit

* BXC-4771 set limit to 99 for 100 files downloaded

* BXC-4771 set limit to 100 for 100 files downloaded
  • Loading branch information
sharonluong authored Nov 19, 2024
1 parent 597f5b0 commit 00066d1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public class DownloadBulkService {
private AccessControlService aclService;
private RepositoryObjectLoader repoObjLoader;
private Path basePath;
private int fileLimit;

public Path downloadBulk(DownloadBulkRequest request) {
var pidString = request.getWorkPidString();
Expand Down Expand Up @@ -60,7 +61,12 @@ private void zipFiles(WorkObject workObject, AccessGroupSet agentPrincipals, Pat
}

Map<String, Integer> duplicates = new HashMap<>();
int count = 0;
var loopLimit = getLoopLimit(memberObjects.size());
for (ContentObject memberObject : memberObjects ) {
if (count == loopLimit) {
break;
}
if (!(memberObject instanceof FileObject)) {
continue;
}
Expand All @@ -84,6 +90,7 @@ private void zipFiles(WorkObject workObject, AccessGroupSet agentPrincipals, Pat

IOUtils.copy(binaryStream, zipOut);
}
count++;
}
}
}
Expand All @@ -101,6 +108,9 @@ private String formatFilename(String filename, int copyNumber) {
var base = FilenameUtils.removeExtension(filename);
return base + "(" + copyNumber + ")." + extension;
}
private int getLoopLimit(int arraySize) {
return Math.min(fileLimit, arraySize);
}

public void setAclService(AccessControlService aclService) {
this.aclService = aclService;
Expand All @@ -113,4 +123,8 @@ public void setRepoObjLoader(RepositoryObjectLoader repoObjLoader) {
public void setBasePath(Path basePath) {
this.basePath = basePath;
}

public void setFileLimit(int fileLimit) {
this.fileLimit = fileLimit;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void init() throws Exception {
service.setAclService(aclService);
service.setRepoObjLoader(repoObjLoader);
service.setBasePath(zipStorageBasePath);
service.setFileLimit(5);
parentPid = PIDs.get(PARENT_UUID);
fileObject1Pid = PIDs.get(CHILD1_UUID);
fileObject2Pid = PIDs.get(CHILD2_UUID);
Expand Down Expand Up @@ -165,6 +166,22 @@ public void tombstoneTest() throws IOException {
assertZipFiles(List.of(), List.of());
}

@Test
public void fileLimitTest() throws IOException {
when(repoObjLoader.getWorkObject(any(PID.class))).thenReturn(parentWork);
when(parentWork.getMembers()).thenReturn(List.of(fileObject1, fileObject2));
makeBinaryObject(fileObject1, FILENAME1);
makeBinaryObject(fileObject2, FILENAME2);
when(aclService.hasAccess(eq(fileObject1Pid), any(),
eq(Permission.viewOriginal))).thenReturn(true);
when(aclService.hasAccess(eq(fileObject2Pid), any(),
eq(Permission.viewOriginal))).thenReturn(true);
service.setFileLimit(1);
service.downloadBulk(request);
// the zip file should have one entry
assertZipFiles(List.of(FILENAME1), List.of("flower"));
}

@Test
public void successTest() throws IOException {
when(repoObjLoader.getWorkObject(eq(parentPid))).thenReturn(parentWork);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@
<property name="aclService" ref="aclService"/>
<property name="repoObjLoader" ref="repositoryObjectLoader"/>
<property name="basePath" value="${data.dir}/zip/"/>
<property name="fileLimit" value="100" />
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void init() throws FileNotFoundException {
downloadBulkService.setAclService(aclService);
downloadBulkService.setBasePath(tmpFolder);
downloadBulkService.setRepoObjLoader(repositoryObjectLoader);
downloadBulkService.setFileLimit(5);
controller.setDownloadBulkService(downloadBulkService);
fileInputStream = new FileInputStream("src/test/resources/__files/bunny.jpg");
mvc = MockMvcBuilders.standaloneSetup(controller)
Expand Down

0 comments on commit 00066d1

Please sign in to comment.