ProcessFiles.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. using ProcessFiles.Interfaces;
  2. using ProcessFiles.Models;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.IO;
  6. using System.Linq;
  7. namespace ProcessFiles
  8. {
  9. public class ProcessFiles(IFileSystem? fileSystem = null)
  10. {
  11. private readonly IFileSystem fileSystem = fileSystem ?? new DefaultFileSystem();
  12. private List<string> errors = [];
  13. private Result WhatIsIt(string path)
  14. {
  15. try
  16. {
  17. var attr = fileSystem.File.GetAttributes(path);
  18. if (attr.HasFlag(FileAttributes.Directory))
  19. return Result.Directory;
  20. return Result.File;
  21. }
  22. catch (Exception e)
  23. {
  24. errors.Add($"Problem getting {path} attributes:\n" +
  25. $"{e.Message} - {e.Source} - {e.TargetSite}");
  26. return Result.Failure;
  27. }
  28. }
  29. private string? GetExtension(string path)
  30. {
  31. var extension = fileSystem.Path.GetExtension(path)?.TrimStart('.');
  32. if (!string.IsNullOrWhiteSpace(extension)) return extension;
  33. errors.Add($"Can't establish extension of {path}!");
  34. return null;
  35. }
  36. private static bool CheckExtension(string extension, string[] validExtensions)
  37. => validExtensions.Any(x => x.Equals(extension, StringComparison.InvariantCultureIgnoreCase));
  38. private bool IsValid(string path, string[] fileExtensions)
  39. {
  40. if (!fileSystem.File.Exists(path))
  41. {
  42. errors.Add($"{path} doesn't exist!");
  43. return false;
  44. }
  45. var extension = GetExtension(path);
  46. if (extension == null)
  47. return false;
  48. if (!CheckExtension(extension, fileExtensions))
  49. {
  50. errors.Add($"Extension of {path} doesn't match any extension ({string.Join(", ", fileExtensions)})!");
  51. return false;
  52. }
  53. return true;
  54. }
  55. private void PerformAction(string path, Action<string> action)
  56. {
  57. try
  58. {
  59. action(path);
  60. }
  61. catch (Exception e)
  62. {
  63. errors.Add($"{path}:\n{e.Message} - {e.Source} - {e.TargetSite}");
  64. }
  65. }
  66. private void ProcessFile(string path, string[] fileExtensions, Action<string> action)
  67. {
  68. if (!IsValid(path, fileExtensions))
  69. return;
  70. PerformAction(path, action);
  71. }
  72. private void ProcessDir(string path, string[] fileExtensions, Action<string> action, bool recursive = false)
  73. {
  74. if (!fileSystem.Directory.Exists(path))
  75. {
  76. errors.Add($"{path} doesn't exist!");
  77. return;
  78. }
  79. var searchOption = recursive switch
  80. {
  81. false => SearchOption.TopDirectoryOnly,
  82. true => SearchOption.AllDirectories,
  83. };
  84. List<string> files = [];
  85. foreach (var extension in fileExtensions)
  86. {
  87. files.AddRange(fileSystem.Directory.GetFiles(path, $"*.{extension}", searchOption));
  88. }
  89. if (files.Count == 0)
  90. {
  91. errors.Add($"There are no files in {path} with given extensions ({string.Join(", ", fileExtensions)})!");
  92. return;
  93. }
  94. foreach (var file in files)
  95. ProcessFile(file, fileExtensions, action);
  96. }
  97. public IEnumerable<string> Process(IEnumerable<string> arguments, string fileExtension, Action<string> action, bool recursive = false)
  98. {
  99. return Process(arguments, [fileExtension], action, recursive);
  100. }
  101. public IEnumerable<string> Process(IEnumerable<string> arguments, string[] fileExtensions, Action<string> action, bool recursive = false)
  102. {
  103. errors = [];
  104. foreach (var argument in arguments)
  105. {
  106. switch (WhatIsIt(argument))
  107. {
  108. case Result.File:
  109. ProcessFile(argument, fileExtensions, action);
  110. break;
  111. case Result.Directory:
  112. ProcessDir(argument, fileExtensions, action, recursive);
  113. break;
  114. case Result.Failure:
  115. continue;
  116. }
  117. }
  118. return errors;
  119. }
  120. }
  121. }