Skip to content

Commit

Permalink
fix: Refactor file validation logic
Browse files Browse the repository at this point in the history
Simplify the file validation process by moving exclusion logic to `FileUtils`. Combined multiple conditions into a single method for clarity and maintainability.
  • Loading branch information
DineshSolanki committed Jul 25, 2024
1 parent b467955 commit aa013d1
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 11 deletions.
14 changes: 4 additions & 10 deletions FoliCon/Modules/Extension/StreamExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ public static void ExtractPngAndIcoToDirectory(this Stream archiveStream, string
IProgress<ProgressInfo> progressCallback)
{
using var reader = ArchiveFactory.Open(archiveStream, ReaderOptions);
var pngAndIcoEntries = reader.Entries.Where(entry =>
!IsUnwantedDirectoryOrFileType(entry) && FileUtils.IsPngOrIco(entry.Key)).ToList();
var pngAndIcoEntries = reader.Entries.Where(IsValidFile).ToList();


var totalCount = pngAndIcoEntries.Count;
Expand All @@ -45,14 +44,9 @@ public static void ExtractPngAndIcoToDirectory(this Stream archiveStream, string
progressCallback.Report(extractionProgress);
}
}

private static bool IsUnwantedDirectoryOrFileType(IEntry entry)
private static bool IsValidFile(IEntry entry)
{
return entry.Key != null && (entry.IsDirectory ||
entry.Key.Contains("ResourceForks") ||
entry.Key.Contains("__MACOSX") ||
entry.Key.StartsWith("._") ||
entry.Key.Equals(".DS_Store") ||
entry.Key.Equals("Thumbs.db"));
return !entry.IsDirectory && !FileUtils.isExcludedFileIdentifier(entry.Key) && FileUtils.IsPngOrIco(entry.Key);
}
}
13 changes: 12 additions & 1 deletion FoliCon/Modules/utils/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ public static class FileUtils
".7z",
".tar"
];

private static readonly HashSet<string> ExcludedFileIdentifiers =
[
"ResourceForks",
"__MACOSX",
"._",
".DS_Store",
"Thumbs.db"
];

/// <summary>
/// Determines whether a given string value ends with any string within a collection of file extensions.
/// </summary>
Expand All @@ -39,6 +47,9 @@ private static bool EndsIn(string value, IEnumerable<string> fileExtensions)
public static bool IsPngOrIco(string fileName) =>
fileName != null && EndsIn(fileName, new[] { ".png", ".ico" });

public static bool isExcludedFileIdentifier(string fileName) =>
fileName != null && ExcludedFileIdentifiers.Any(fileName.Contains);

/// <summary>
/// Deletes Icons (.ico and Desktop.ini files) from all subfolders of given path.
/// </summary>
Expand Down

0 comments on commit aa013d1

Please sign in to comment.