ItemResolveArgs.cs 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. #nullable disable
  2. #pragma warning disable CS1591
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using MediaBrowser.Controller.Entities;
  7. using MediaBrowser.Model.Configuration;
  8. using MediaBrowser.Model.IO;
  9. namespace MediaBrowser.Controller.Library
  10. {
  11. /// <summary>
  12. /// These are arguments relating to the file system that are collected once and then referred to
  13. /// whenever needed. Primarily for entity resolution.
  14. /// </summary>
  15. public class ItemResolveArgs
  16. {
  17. /// <summary>
  18. /// The _app paths.
  19. /// </summary>
  20. private readonly IServerApplicationPaths _appPaths;
  21. private readonly ILibraryManager _libraryManager;
  22. private LibraryOptions _libraryOptions;
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="ItemResolveArgs" /> class.
  25. /// </summary>
  26. /// <param name="appPaths">The app paths.</param>
  27. /// <param name="libraryManager">The library manager.</param>
  28. public ItemResolveArgs(IServerApplicationPaths appPaths, ILibraryManager libraryManager)
  29. {
  30. _appPaths = appPaths;
  31. _libraryManager = libraryManager;
  32. }
  33. /// <summary>
  34. /// Gets or sets the file system children.
  35. /// </summary>
  36. /// <value>The file system children.</value>
  37. public FileSystemMetadata[] FileSystemChildren { get; set; }
  38. public LibraryOptions LibraryOptions
  39. {
  40. get => _libraryOptions ??= Parent is null ? new LibraryOptions() : _libraryManager.GetLibraryOptions(Parent);
  41. set => _libraryOptions = value;
  42. }
  43. /// <summary>
  44. /// Gets or sets the parent.
  45. /// </summary>
  46. /// <value>The parent.</value>
  47. public Folder Parent { get; set; }
  48. /// <summary>
  49. /// Gets or sets the file info.
  50. /// </summary>
  51. /// <value>The file info.</value>
  52. public FileSystemMetadata FileInfo { get; set; }
  53. /// <summary>
  54. /// Gets the path.
  55. /// </summary>
  56. /// <value>The path.</value>
  57. public string Path => FileInfo.FullName;
  58. /// <summary>
  59. /// Gets a value indicating whether this instance is directory.
  60. /// </summary>
  61. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  62. public bool IsDirectory => FileInfo.IsDirectory;
  63. /// <summary>
  64. /// Gets a value indicating whether this instance is vf.
  65. /// </summary>
  66. /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
  67. public bool IsVf
  68. {
  69. // we should be considered a virtual folder if we are a child of one of the children of the system root folder.
  70. // this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
  71. // the root but not be equal to it
  72. get
  73. {
  74. if (!IsDirectory)
  75. {
  76. return false;
  77. }
  78. var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
  79. return parentDir.Length > _appPaths.RootFolderPath.Length
  80. && parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
  81. }
  82. }
  83. /// <summary>
  84. /// Gets a value indicating whether this instance is physical root.
  85. /// </summary>
  86. /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
  87. public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath);
  88. /// <summary>
  89. /// Gets or sets the additional locations.
  90. /// </summary>
  91. /// <value>The additional locations.</value>
  92. private List<string> AdditionalLocations { get; set; }
  93. /// <summary>
  94. /// Gets the physical locations.
  95. /// </summary>
  96. /// <value>The physical locations.</value>
  97. public string[] PhysicalLocations
  98. {
  99. get
  100. {
  101. var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : new[] { Path };
  102. return AdditionalLocations is null ? paths : paths.Concat(AdditionalLocations).ToArray();
  103. }
  104. }
  105. public string CollectionType { get; set; }
  106. public bool HasParent<T>()
  107. where T : Folder
  108. {
  109. var parent = Parent;
  110. if (parent is not null)
  111. {
  112. var item = parent as T;
  113. // Just in case the user decided to nest episodes.
  114. // Not officially supported but in some cases we can handle it.
  115. if (item is null)
  116. {
  117. var parents = parent.GetParents();
  118. foreach (var currentParent in parents)
  119. {
  120. if (currentParent is T)
  121. {
  122. return true;
  123. }
  124. }
  125. }
  126. return item is not null;
  127. }
  128. return false;
  129. }
  130. /// <summary>
  131. /// Determines whether the specified <see cref="object" /> is equal to this instance.
  132. /// </summary>
  133. /// <param name="obj">The object to compare with the current object.</param>
  134. /// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  135. public override bool Equals(object obj)
  136. {
  137. return Equals(obj as ItemResolveArgs);
  138. }
  139. /// <summary>
  140. /// Adds the additional location.
  141. /// </summary>
  142. /// <param name="path">The path.</param>
  143. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
  144. public void AddAdditionalLocation(string path)
  145. {
  146. ArgumentException.ThrowIfNullOrEmpty(path);
  147. AdditionalLocations ??= new List<string>();
  148. AdditionalLocations.Add(path);
  149. }
  150. // REVIEW: @bond
  151. /// <summary>
  152. /// Gets the name of the file system entry by.
  153. /// </summary>
  154. /// <param name="name">The name.</param>
  155. /// <returns>FileSystemInfo.</returns>
  156. /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
  157. public FileSystemMetadata GetFileSystemEntryByName(string name)
  158. {
  159. ArgumentException.ThrowIfNullOrEmpty(name);
  160. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  161. }
  162. /// <summary>
  163. /// Gets the file system entry by path.
  164. /// </summary>
  165. /// <param name="path">The path.</param>
  166. /// <returns>FileSystemInfo.</returns>
  167. /// <exception cref="ArgumentNullException">Throws if path is invalid.</exception>
  168. public FileSystemMetadata GetFileSystemEntryByPath(string path)
  169. {
  170. ArgumentException.ThrowIfNullOrEmpty(path);
  171. foreach (var file in FileSystemChildren)
  172. {
  173. if (string.Equals(file.FullName, path, StringComparison.Ordinal))
  174. {
  175. return file;
  176. }
  177. }
  178. return null;
  179. }
  180. /// <summary>
  181. /// Determines whether [contains file system entry by name] [the specified name].
  182. /// </summary>
  183. /// <param name="name">The name.</param>
  184. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  185. public bool ContainsFileSystemEntryByName(string name)
  186. {
  187. return GetFileSystemEntryByName(name) is not null;
  188. }
  189. public string GetCollectionType()
  190. {
  191. return CollectionType;
  192. }
  193. /// <summary>
  194. /// Gets the configured content type for the path.
  195. /// </summary>
  196. /// <returns>The configured content type.</returns>
  197. public string GetConfiguredContentType()
  198. {
  199. return _libraryManager.GetConfiguredContentType(Path);
  200. }
  201. /// <summary>
  202. /// Gets the file system children that do not hit the ignore file check.
  203. /// </summary>
  204. /// <returns>The file system children that are not ignored.</returns>
  205. public IEnumerable<FileSystemMetadata> GetActualFileSystemChildren()
  206. {
  207. var numberOfChildren = FileSystemChildren.Length;
  208. for (var i = 0; i < numberOfChildren; i++)
  209. {
  210. var child = FileSystemChildren[i];
  211. if (_libraryManager.IgnoreFile(child, Parent))
  212. {
  213. continue;
  214. }
  215. yield return child;
  216. }
  217. }
  218. /// <summary>
  219. /// Returns a hash code for this instance.
  220. /// </summary>
  221. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  222. public override int GetHashCode()
  223. {
  224. return Path.GetHashCode(StringComparison.Ordinal);
  225. }
  226. /// <summary>
  227. /// Equals the specified args.
  228. /// </summary>
  229. /// <param name="args">The args.</param>
  230. /// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns>
  231. protected bool Equals(ItemResolveArgs args)
  232. {
  233. if (args is not null)
  234. {
  235. if (args.Path is null && Path is null)
  236. {
  237. return true;
  238. }
  239. return args.Path is not null && BaseItem.FileSystem.AreEqual(args.Path, Path);
  240. }
  241. return false;
  242. }
  243. }
  244. }