Remove unnecessary checks for directory, throw if more than 1 file found
@@ -89,10 +89,12 @@ public class FileDownloader implements AutoCloseable {
return downloadDirectory;
}
- // Files are moved atomically, so if file reference exists and is accessible we can use it
private Optional<File> getFileFromFileSystem(FileReference fileReference) {
File[] files = new File(downloadDirectory, fileReference.value()).listFiles();
- if (downloadDirectory.exists() && downloadDirectory.isDirectory() && files != null && files.length > 0) {
+ if (files == null) return Optional.empty();
+ if (files.length == 0) return Optional.empty();
+ if (files.length > 1) throw new RuntimeException("More than one file reference found for " + fileReference);
+
File file = files[0];
if (!file.exists()) {
throw new RuntimeException("File reference '" + fileReference.value() + "' does not exist");
@@ -104,8 +106,6 @@ public class FileDownloader implements AutoCloseable {
return Optional.of(file);
}
}
- return Optional.empty();
- }
boolean isDownloading(FileReference fileReference) {
return downloads.get(fileReference).isPresent();
