Skip to content

Commit

Permalink
Development: Fix unclosed input streams (#7325)
Browse files Browse the repository at this point in the history
  • Loading branch information
Strohgelaender authored Oct 7, 2023
1 parent c92c995 commit 285968d
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/main/java/de/tum/in/www1/artemis/service/FileService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand All @@ -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();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ public ResponseEntity<Void> 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);
});
}
Expand All @@ -189,8 +190,9 @@ public ResponseEntity<Void> 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);
});
}
Expand Down Expand Up @@ -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());
}
}
}

0 comments on commit 285968d

Please sign in to comment.