BdInfoDirectoryInfo.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.IO;
  3. using System.Linq;
  4. using BDInfo.IO;
  5. using MediaBrowser.Model.IO;
  6. namespace MediaBrowser.MediaEncoding.BdInfo;
  7. /// <summary>
  8. /// Class BdInfoDirectoryInfo.
  9. /// </summary>
  10. public class BdInfoDirectoryInfo : IDirectoryInfo
  11. {
  12. private readonly IFileSystem _fileSystem;
  13. private readonly FileSystemMetadata _impl;
  14. /// <summary>
  15. /// Initializes a new instance of the <see cref="BdInfoDirectoryInfo" /> class.
  16. /// </summary>
  17. /// <param name="fileSystem">The filesystem.</param>
  18. /// <param name="path">The path.</param>
  19. public BdInfoDirectoryInfo(IFileSystem fileSystem, string path)
  20. {
  21. _fileSystem = fileSystem;
  22. _impl = _fileSystem.GetDirectoryInfo(path);
  23. }
  24. private BdInfoDirectoryInfo(IFileSystem fileSystem, FileSystemMetadata impl)
  25. {
  26. _fileSystem = fileSystem;
  27. _impl = impl;
  28. }
  29. /// <summary>
  30. /// Gets the name.
  31. /// </summary>
  32. public string Name => _impl.Name;
  33. /// <summary>
  34. /// Gets the full name.
  35. /// </summary>
  36. public string FullName => _impl.FullName;
  37. /// <summary>
  38. /// Gets the parent directory information.
  39. /// </summary>
  40. public IDirectoryInfo? Parent
  41. {
  42. get
  43. {
  44. var parentFolder = Path.GetDirectoryName(_impl.FullName);
  45. if (parentFolder is not null)
  46. {
  47. return new BdInfoDirectoryInfo(_fileSystem, parentFolder);
  48. }
  49. return null;
  50. }
  51. }
  52. private static bool IsHidden(ReadOnlySpan<char> name) => name.StartsWith('.');
  53. /// <summary>
  54. /// Gets the directories.
  55. /// </summary>
  56. /// <returns>An array with all directories.</returns>
  57. public IDirectoryInfo[] GetDirectories()
  58. {
  59. return _fileSystem.GetDirectories(_impl.FullName)
  60. .Where(d => !IsHidden(d.Name))
  61. .Select(x => new BdInfoDirectoryInfo(_fileSystem, x))
  62. .ToArray();
  63. }
  64. /// <summary>
  65. /// Gets the files.
  66. /// </summary>
  67. /// <returns>All files of the directory.</returns>
  68. public IFileInfo[] GetFiles()
  69. {
  70. return _fileSystem.GetFiles(_impl.FullName)
  71. .Where(d => !IsHidden(d.Name))
  72. .Select(x => new BdInfoFileInfo(x))
  73. .ToArray();
  74. }
  75. /// <summary>
  76. /// Gets the files matching a pattern.
  77. /// </summary>
  78. /// <param name="searchPattern">The search pattern.</param>
  79. /// <returns>All files of the directory matching the search pattern.</returns>
  80. public IFileInfo[] GetFiles(string searchPattern)
  81. {
  82. return _fileSystem.GetFiles(_impl.FullName, new[] { searchPattern }, false, false)
  83. .Where(d => !IsHidden(d.Name))
  84. .Select(x => new BdInfoFileInfo(x))
  85. .ToArray();
  86. }
  87. /// <summary>
  88. /// Gets the files matching a pattern and search options.
  89. /// </summary>
  90. /// <param name="searchPattern">The search pattern.</param>
  91. /// <param name="searchOption">The search option.</param>
  92. /// <returns>All files of the directory matching the search pattern and options.</returns>
  93. public IFileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
  94. {
  95. return _fileSystem.GetFiles(
  96. _impl.FullName,
  97. new[] { searchPattern },
  98. false,
  99. searchOption == SearchOption.AllDirectories)
  100. .Where(d => !IsHidden(d.Name))
  101. .Select(x => new BdInfoFileInfo(x))
  102. .ToArray();
  103. }
  104. /// <summary>
  105. /// Gets the bdinfo of a file system path.
  106. /// </summary>
  107. /// <param name="fs">The file system.</param>
  108. /// <param name="path">The path.</param>
  109. /// <returns>The BD directory information of the path on the file system.</returns>
  110. public static IDirectoryInfo FromFileSystemPath(IFileSystem fs, string path)
  111. {
  112. return new BdInfoDirectoryInfo(fs, path);
  113. }
  114. }