BdInfoDirectoryInfo.cs 3.6 KB

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