ItemResolveArgs.cs 10 KB

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