From 00066d16b1738266222758638052630eac2ec750 Mon Sep 17 00:00:00 2001 From: sharonl <6546457+sharonluong@users.noreply.github.com> Date: Tue, 19 Nov 2024 12:12:44 -0500 Subject: [PATCH] Bxc 4771 upper file limit (#1835) * 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 --- .../impl/download/DownloadBulkService.java | 14 ++++++++++++++ .../impl/download/DownloadBulkServiceTest.java | 17 +++++++++++++++++ .../src/main/webapp/WEB-INF/service-context.xml | 1 + .../services/rest/DownloadBulkControllerIT.java | 1 + 4 files changed, 33 insertions(+) diff --git a/operations/src/main/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkService.java b/operations/src/main/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkService.java index bbb081e99d..e19ea83332 100644 --- a/operations/src/main/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkService.java +++ b/operations/src/main/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkService.java @@ -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(); @@ -60,7 +61,12 @@ private void zipFiles(WorkObject workObject, AccessGroupSet agentPrincipals, Pat } Map duplicates = new HashMap<>(); + int count = 0; + var loopLimit = getLoopLimit(memberObjects.size()); for (ContentObject memberObject : memberObjects ) { + if (count == loopLimit) { + break; + } if (!(memberObject instanceof FileObject)) { continue; } @@ -84,6 +90,7 @@ private void zipFiles(WorkObject workObject, AccessGroupSet agentPrincipals, Pat IOUtils.copy(binaryStream, zipOut); } + count++; } } } @@ -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; @@ -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; + } } diff --git a/operations/src/test/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkServiceTest.java b/operations/src/test/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkServiceTest.java index ffea60c4b7..30a98cf5b4 100644 --- a/operations/src/test/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkServiceTest.java +++ b/operations/src/test/java/edu/unc/lib/boxc/operations/impl/download/DownloadBulkServiceTest.java @@ -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); @@ -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); diff --git a/web-services-app/src/main/webapp/WEB-INF/service-context.xml b/web-services-app/src/main/webapp/WEB-INF/service-context.xml index 8b0e2bab89..5b5540aacb 100644 --- a/web-services-app/src/main/webapp/WEB-INF/service-context.xml +++ b/web-services-app/src/main/webapp/WEB-INF/service-context.xml @@ -498,6 +498,7 @@ + diff --git a/web-services-app/src/test/java/edu/unc/lib/boxc/web/services/rest/DownloadBulkControllerIT.java b/web-services-app/src/test/java/edu/unc/lib/boxc/web/services/rest/DownloadBulkControllerIT.java index 19f149f9fd..2ae9383c49 100644 --- a/web-services-app/src/test/java/edu/unc/lib/boxc/web/services/rest/DownloadBulkControllerIT.java +++ b/web-services-app/src/test/java/edu/unc/lib/boxc/web/services/rest/DownloadBulkControllerIT.java @@ -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)