BdInfoDirectoryInfo.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. /// <summary>
  53. /// Gets the directories.
  54. /// </summary>
  55. /// <returns>An array with all directories.</returns>
  56. public IDirectoryInfo[] GetDirectories()
  57. {
  58. return _fileSystem.GetDirectories(_impl.FullName)
  59. .Select(x => new BdInfoDirectoryInfo(_fileSystem, x))
  60. .ToArray();
  61. }
  62. /// <summary>
  63. /// Gets the files.
  64. /// </summary>
  65. /// <returns>All files of the directory.</returns>
  66. public IFileInfo[] GetFiles()
  67. {
  68. return _fileSystem.GetFiles(_impl.FullName)
  69. .Select(x => new BdInfoFileInfo(x))
  70. .ToArray();
  71. }
  72. /// <summary>
  73. /// Gets the files matching a pattern.
  74. /// </summary>
  75. /// <param name="searchPattern">The search pattern.</param>
  76. /// <returns>All files of the directory matchign the search pattern.</returns>
  77. public IFileInfo[] GetFiles(string searchPattern)
  78. {
  79. return _fileSystem.GetFiles(_impl.FullName, new[] { searchPattern }, false, false)
  80. .Select(x => new BdInfoFileInfo(x))
  81. .ToArray();
  82. }
  83. /// <summary>
  84. /// Gets the files matching a pattern and search options.
  85. /// </summary>
  86. /// <param name="searchPattern">The search pattern.</param>
  87. /// <param name="searchOption">The search optin.</param>
  88. /// <returns>All files of the directory matchign the search pattern and options.</returns>
  89. public IFileInfo[] GetFiles(string searchPattern, SearchOption searchOption)
  90. {
  91. return _fileSystem.GetFiles(
  92. _impl.FullName,
  93. new[] { searchPattern },
  94. false,
  95. (searchOption & SearchOption.AllDirectories) == SearchOption.AllDirectories)
  96. .Select(x => new BdInfoFileInfo(x))
  97. .ToArray();
  98. }
  99. /// <summary>
  100. /// Gets the bdinfo of a file system path.
  101. /// </summary>
  102. /// <param name="fs">The file system.</param>
  103. /// <param name="path">The path.</param>
  104. /// <returns>The BD directory information of the path on the file system.</returns>
  105. public static IDirectoryInfo FromFileSystemPath(IFileSystem fs, string path)
  106. {
  107. return new BdInfoDirectoryInfo(fs, path);
  108. }
  109. }