diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java b/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java index 1967cc22962e7b5756116eff85dd19dd93649229..8b6a80e61c811a7ba7cc1d83aacd5520c3900ae3 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangController.java @@ -23,7 +23,9 @@ */ package de.ozgcloud.alfa.attachment; -import java.io.OutputStream; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; import org.springframework.hateoas.CollectionModel; import org.springframework.hateoas.EntityModel; @@ -62,18 +64,28 @@ public class AttachmentByVorgangController { @GetMapping(produces = APPLICATION_ZIP_VALUE) public ResponseEntity<StreamingResponseBody> download(@PathVariable String vorgangId) { + var zipFile = createZipFile(vorgangId); return ResponseEntity.ok() .header(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s", buildZipName(vorgangId))) .contentType(MediaType.valueOf(APPLICATION_ZIP_VALUE)) - .body(out -> createZipFile(out, vorgangId)); + .contentLength(getFileSize(zipFile)) + .body(out -> zipDownloadService.writeZipFileContent(zipFile, out)); } String buildZipName(String vorgangId) { return vorgangController.getVorgang(vorgangId).getNummer() + "_Anhaenge.zip"; } - void createZipFile(OutputStream out, String vorgangId) { + File createZipFile(String vorgangId) { var ozgFiles = fileService.getAttachments(vorgangId); - zipDownloadService.write(ozgFiles.toList(), out); + return zipDownloadService.createZipFile(ozgFiles.toList()); + } + + long getFileSize(File file) { + try { + return Files.size(file.toPath()); + } catch (IOException e) { + throw new RuntimeException(e); + } } } diff --git a/alfa-service/src/main/java/de/ozgcloud/alfa/common/zipdownload/ZipDownloadService.java b/alfa-service/src/main/java/de/ozgcloud/alfa/common/zipdownload/ZipDownloadService.java index 11cc784f97b2dc4937fe06b3172fd865fb154466..a2d523be9a83ff91be789c14cf33f9e2b9b050d5 100644 --- a/alfa-service/src/main/java/de/ozgcloud/alfa/common/zipdownload/ZipDownloadService.java +++ b/alfa-service/src/main/java/de/ozgcloud/alfa/common/zipdownload/ZipDownloadService.java @@ -50,7 +50,7 @@ public class ZipDownloadService { writeZipFileContent(createZipFile(ozgFiles), out); } - File createZipFile(List<OzgFile> ozgFiles) { + public File createZipFile(List<OzgFile> ozgFiles) { var file = TempFileUtils.createTmpFile().toFile(); try (var zipOutputStream = new ZipOutputStream(new FileOutputStream(file))) { ozgFiles.forEach(ozgFile -> addOzgFileToZip(ozgFile, zipOutputStream)); @@ -70,7 +70,7 @@ public class ZipDownloadService { } } - void writeZipFileContent(File zipFile, OutputStream outputStream) { + public void writeZipFileContent(File zipFile, OutputStream outputStream) { try (var fileInputStream = new FileInputStream(zipFile)) { fileInputStream.transferTo(outputStream); } catch (Exception e) { diff --git a/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java b/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java index a24e03e69012ef646a33ebf16c4a537de56edc05..5c3ed501267851d27633e931cec02fcfaaea422a 100644 --- a/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java +++ b/alfa-service/src/test/java/de/ozgcloud/alfa/attachment/AttachmentByVorgangControllerTest.java @@ -29,8 +29,10 @@ import static org.mockito.Mockito.*; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; +import java.io.File; import java.io.OutputStream; import java.util.List; +import java.util.Random; import java.util.UUID; import java.util.stream.Stream; @@ -117,11 +119,15 @@ class AttachmentByVorgangControllerTest { @DisplayName("Download") @Nested class TestDownload { - private final String filename = LoremIpsum.getInstance().getWords(1); + private final String zipFileName = LoremIpsum.getInstance().getWords(1); + private final long zipFileSize = Math.abs(new Random().nextInt()); + private final File zipFile = new File(UUID.randomUUID().toString() + ".zip"); @BeforeEach void setUpMock() { - doReturn(filename).when(controller).buildZipName(VorgangHeaderTestFactory.ID); + doReturn(zipFileName).when(controller).buildZipName(VorgangHeaderTestFactory.ID); + doReturn(zipFileSize).when(controller).getFileSize(zipFile); + doReturn(zipFile).when(controller).createZipFile(VorgangHeaderTestFactory.ID); } @SneakyThrows @@ -145,7 +151,7 @@ class AttachmentByVorgangControllerTest { void shouldHaveContentDispositonHeader() { var response = doRequest(); - response.andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename)); + response.andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + zipFileName)); } @SneakyThrows @@ -156,12 +162,28 @@ class AttachmentByVorgangControllerTest { response.andExpect(header().string(HttpHeaders.CONTENT_TYPE, AttachmentByVorgangController.APPLICATION_ZIP_VALUE)); } + @SneakyThrows + @Test + void shouldHaveContentLength() { + var response = doRequest(); + + response.andExpect(header().longValue(HttpHeaders.CONTENT_LENGTH, zipFileSize)); + } + + @SneakyThrows + @Test + void shouldCreateZipFile() { + doRequest(); + + verify(controller).createZipFile(VorgangHeaderTestFactory.ID); + } + @SneakyThrows @Test void shouldWriteZipFile() { doRequest(); - verify(controller).createZipFile(any(), eq(VorgangHeaderTestFactory.ID)); + verify(zipDownloadService).writeZipFileContent(eq(zipFile), any(OutputStream.class)); } @SneakyThrows @@ -206,8 +228,6 @@ class AttachmentByVorgangControllerTest { @Nested class TestCreateZipFile { - @Mock - private OutputStream outputStream; @Test void shouldGetAttachments() { @@ -223,11 +243,11 @@ class AttachmentByVorgangControllerTest { callController(); - verify(zipDownloadService).write(List.of(ozgFile), outputStream); + verify(zipDownloadService).createZipFile(List.of(ozgFile)); } private void callController() { - controller.createZipFile(outputStream, VorgangHeaderTestFactory.ID); + controller.createZipFile(VorgangHeaderTestFactory.ID); } } }