Skip to content

Commit

Permalink
refactor: pull recursive scan into function
Browse files Browse the repository at this point in the history
  • Loading branch information
anna-is-cute committed Aug 19, 2024
1 parent 8608eb5 commit ec14bed
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
3 changes: 1 addition & 2 deletions ConvertTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ internal async Task Run() {
});

// gather a list of all files in the files directory
var installedFiles = Directory.EnumerateFileSystemEntries(filesPath, "*", SearchOption.AllDirectories)
.Where(path => (File.GetAttributes(path) & FileAttributes.Directory) == 0)
var installedFiles = DirectoryHelper.GetFilesRecursive(filesPath)
.Select(Path.GetFileName)
.Where(path => path != null)
.Cast<string>()
Expand Down
18 changes: 2 additions & 16 deletions DownloadTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,14 +356,7 @@ private async Task<IEnumerable<Task>> DownloadBatchedFiles(IDownloadTask_GetVers

// get all pre-existing files and validate them, storing which file path
// is associated with each hash
var existingFiles = Directory.EnumerateFileSystemEntries(filesPath, "*", SearchOption.AllDirectories)
.Where(entry => {
try {
return (File.GetAttributes(entry) & FileAttributes.Directory) == 0;
} catch {
return false;
}
})
var existingFiles = DirectoryHelper.GetFilesRecursive(filesPath)
.Select(path => PathHelper.MakeRelativeSub(filesPath, path))
.Where(path => !string.IsNullOrEmpty(path))
.Cast<string>()
Expand Down Expand Up @@ -748,14 +741,7 @@ private void RemoveOldFiles(IDownloadTask_GetVersion info) {
expectedFiles.AddRange(lowerOutputPaths);
}

var presentFiles = Directory.EnumerateFileSystemEntries(filesPath, "*", SearchOption.AllDirectories)
.Where(path => {
try {
return (File.GetAttributes(path) & FileAttributes.Directory) == 0;
} catch {
return false;
}
})
var presentFiles = DirectoryHelper.GetFilesRecursive(filesPath)
.Select(path => PathHelper.MakeRelativeSub(filesPath, path))
.Where(path => !string.IsNullOrEmpty(path))
.Cast<string>()
Expand Down
20 changes: 20 additions & 0 deletions Util/DirectoryHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,24 @@ internal static void RemoveEmptyDirectories(string root) {
}
}
}

internal static IEnumerable<string> GetFilesRecursive(string root) {
return GetEntriesRecursive(root, true);
}

internal static IEnumerable<string> GetDirectoriesRecursive(string root) {
return GetEntriesRecursive(root, false);
}

private static IEnumerable<string> GetEntriesRecursive(string root, bool files) {
var result = files ? 0 : FileAttributes.Directory;
return Directory.EnumerateFileSystemEntries(root, "*", SearchOption.AllDirectories)
.Where(path => {
try {
return (File.GetAttributes(path) & FileAttributes.Directory) == result;
} catch {
return false;
}
});
}
}

0 comments on commit ec14bed

Please sign in to comment.