From 285968d97aa1435e8fff1330e148019df43b1685 Mon Sep 17 00:00:00 2001 From: Lucas Welscher Date: Sat, 7 Oct 2023 17:47:18 +0200 Subject: [PATCH] Development: Fix unclosed input streams (#7325) --- .../tum/in/www1/artemis/service/FileService.java | 4 ++-- .../service/FileUploadSubmissionService.java | 2 +- .../www1/artemis/service/RepositoryService.java | 2 -- .../web/rest/repository/RepositoryResource.java | 16 +++++++++------- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/main/java/de/tum/in/www1/artemis/service/FileService.java b/src/main/java/de/tum/in/www1/artemis/service/FileService.java index 814b47092fd6..4de0fa0b9d23 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/FileService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/FileService.java @@ -181,7 +181,7 @@ public URI handleSaveFile(MultipartFile file, boolean keepFilename, boolean mark filePath = generateFilePath(filenamePrefix, fileExtension, path); } try { - FileUtils.copyToFile(file.getInputStream(), filePath.toFile()); + FileUtils.copyInputStreamToFile(file.getInputStream(), filePath.toFile()); return generateResponsePath(filePath, markdown); } @@ -344,7 +344,7 @@ public void copyResource(final Resource resource, final Path prefix, final Path return; } - FileUtils.copyToFile(resource.getInputStream(), targetPath.toFile()); + FileUtils.copyInputStreamToFile(resource.getInputStream(), targetPath.toFile()); if (targetPath.endsWith("gradlew")) { targetPath.toFile().setExecutable(true); diff --git a/src/main/java/de/tum/in/www1/artemis/service/FileUploadSubmissionService.java b/src/main/java/de/tum/in/www1/artemis/service/FileUploadSubmissionService.java index df4454fddcc9..3849af1419d6 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/FileUploadSubmissionService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/FileUploadSubmissionService.java @@ -201,7 +201,7 @@ private Path saveFileForSubmission(final MultipartFile file, final Submission su final Path filePath = dirPath.resolve(filename); final File savedFile = filePath.toFile(); - FileUtils.copyToFile(file.getInputStream(), savedFile); + FileUtils.copyInputStreamToFile(file.getInputStream(), savedFile); return filePath; } diff --git a/src/main/java/de/tum/in/www1/artemis/service/RepositoryService.java b/src/main/java/de/tum/in/www1/artemis/service/RepositoryService.java index 8b294f0bdddb..ef39326d10da 100644 --- a/src/main/java/de/tum/in/www1/artemis/service/RepositoryService.java +++ b/src/main/java/de/tum/in/www1/artemis/service/RepositoryService.java @@ -188,7 +188,6 @@ public void createFile(Repository repository, String filePath, InputStream input File file = checkIfPathAndFileAreValidAndReturnSafeFile(repository, safePath); FileUtils.copyToFile(inputStream, file); repository.setContent(null); // invalidate cache - inputStream.close(); } /** @@ -207,7 +206,6 @@ public void createFolder(Repository repository, String folderPath, InputStream i File keep = new File(repository.getLocalPath().resolve(safePath).resolve(".keep"), repository); FileUtils.copyToFile(inputStream, keep); repository.setContent(null); // invalidate cache - inputStream.close(); } /** diff --git a/src/main/java/de/tum/in/www1/artemis/web/rest/repository/RepositoryResource.java b/src/main/java/de/tum/in/www1/artemis/web/rest/repository/RepositoryResource.java index 5cce9e0333c0..5f44f6581daf 100644 --- a/src/main/java/de/tum/in/www1/artemis/web/rest/repository/RepositoryResource.java +++ b/src/main/java/de/tum/in/www1/artemis/web/rest/repository/RepositoryResource.java @@ -170,8 +170,9 @@ public ResponseEntity createFile(Long domainId, String filePath, HttpServl return executeAndCheckForExceptions(() -> { Repository repository = getRepository(domainId, RepositoryActionType.WRITE, true); - InputStream inputStream = request.getInputStream(); - repositoryService.createFile(repository, filePath, inputStream); + try (var inputStream = request.getInputStream()) { + repositoryService.createFile(repository, filePath, inputStream); + } return new ResponseEntity<>(HttpStatus.OK); }); } @@ -189,8 +190,9 @@ public ResponseEntity createFolder(Long domainId, String folderPath, HttpS return executeAndCheckForExceptions(() -> { Repository repository = getRepository(domainId, RepositoryActionType.WRITE, true); - InputStream inputStream = request.getInputStream(); - repositoryService.createFolder(repository, folderPath, inputStream); + try (InputStream inputStream = request.getInputStream()) { + repositoryService.createFolder(repository, folderPath, inputStream); + } return new ResponseEntity<>(HttpStatus.OK); }); } @@ -389,8 +391,8 @@ private void fetchAndUpdateFile(FileSubmission submission, Repository repository throw new IOException("File could not be found."); } - InputStream inputStream = new ByteArrayInputStream(submission.getFileContent().getBytes(StandardCharsets.UTF_8)); - FileUtils.copyToFile(inputStream, file.get()); - inputStream.close(); + try (var inputStream = new ByteArrayInputStream(submission.getFileContent().getBytes(StandardCharsets.UTF_8))) { + FileUtils.copyToFile(inputStream, file.get()); + } } }