-
Notifications
You must be signed in to change notification settings - Fork 0
Modernize and secure temp file creation #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.Map; | ||
|
@@ -99,7 +100,7 @@ public static Integer countLines(String fileLocation) { | |
public static String downloadFile(String urlString) throws IOException { | ||
LOGGER.info("Downloading contents from url: {}", urlString); | ||
var url = new URL(urlString); | ||
var file = File.createTempFile("promise_pattern", null); | ||
var file = Files.createTempFile("promise_pattern", null).toFile(); | ||
try (var bufferedReader = new BufferedReader(new InputStreamReader(url.openStream())); | ||
var writer = new FileWriter(file)) { | ||
String line; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Review:
Suggested Improvement:// Better handling of exception for Files.createTempFile
public static String downloadFile(String urlString) throws IOException {
LOGGER.info("Downloading contents from url: {}", urlString);
URL url = new URL(urlString);
// Create a temporary file
Path tempFilePath;
try {
tempFilePath = Files.createTempFile("promise_pattern", null);
} catch (IOException e) {
LOGGER.error("Error creating temporary file", e);
throw e; // Handle or rethrow as necessary
}
File file = tempFilePath.toFile();
// Properly close resources with try-with-resources
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()));
FileWriter writer = new FileWriter(file)) {
// Read lines from URL and write to file
String line;
while ((line = bufferedReader.readLine()) != null) {
writer.write(line);
}
}
return file.getAbsolutePath();
} Overall, the reviewed code seems functional but requires some improvements and error handling enhancements to strengthen its robustness. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The method
downloadFile
creates a temporary file but does not ensure that this file is deleted after use. This can lead to resource leakage and potentially fill up the temporary directory over time.Recommended Solution:
Ensure that the temporary file is deleted after it is no longer needed, either by using
file.deleteOnExit()
or by explicitly deleting the file in afinally
block.