ItemResolveArgs.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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 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="directoryService">The directory service.</param>
  28. public ItemResolveArgs(IServerApplicationPaths appPaths, IDirectoryService directoryService)
  29. {
  30. _appPaths = appPaths;
  31. DirectoryService = directoryService;
  32. }
  33. // TODO remove dependencies as properties, they should be injected where it makes sense
  34. public IDirectoryService DirectoryService { get; }
  35. /// <summary>
  36. /// Gets or sets the file system children.
  37. /// </summary>
  38. /// <value>The file system children.</value>
  39. public FileSystemMetadata[] FileSystemChildren { get; set; }
  40. public LibraryOptions LibraryOptions
  41. {
  42. get => _libraryOptions ??= Parent == null ? new LibraryOptions() : BaseItem.LibraryManager.GetLibraryOptions(Parent);
  43. set => _libraryOptions = value;
  44. }
  45. /// <summary>
  46. /// Gets or sets the parent.
  47. /// </summary>
  48. /// <value>The parent.</value>
  49. public Folder Parent { get; set; }
  50. /// <summary>
  51. /// Gets or sets the file info.
  52. /// </summary>
  53. /// <value>The file info.</value>
  54. public FileSystemMetadata FileInfo { get; set; }
  55. /// <summary>
  56. /// Gets the path.
  57. /// </summary>
  58. /// <value>The path.</value>
  59. public string Path => FileInfo.FullName;
  60. /// <summary>
  61. /// Gets a value indicating whether this instance is directory.
  62. /// </summary>
  63. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  64. public bool IsDirectory => FileInfo.IsDirectory;
  65. /// <summary>
  66. /// Gets a value indicating whether this instance is vf.
  67. /// </summary>
  68. /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
  69. public bool IsVf
  70. {
  71. // we should be considered a virtual folder if we are a child of one of the children of the system root folder.
  72. // this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
  73. // the root but not be equal to it
  74. get
  75. {
  76. if (!IsDirectory)
  77. {
  78. return false;
  79. }
  80. var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
  81. return parentDir.Length > _appPaths.RootFolderPath.Length
  82. && parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
  83. }
  84. }
  85. /// <summary>
  86. /// Gets a value indicating whether this instance is physical root.
  87. /// </summary>
  88. /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
  89. public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath);
  90. /// <summary>
  91. /// Gets or sets the additional locations.
  92. /// </summary>
  93. /// <value>The additional locations.</value>
  94. private List<string> AdditionalLocations { get; set; }
  95. /// <summary>
  96. /// Gets the physical locations.
  97. /// </summary>
  98. /// <value>The physical locations.</value>
  99. public string[] PhysicalLocations
  100. {
  101. get
  102. {
  103. var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : new[] { Path };
  104. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations).ToArray();
  105. }
  106. }
  107. public string CollectionType { get; set; }
  108. public bool HasParent<T>()
  109. where T : Folder
  110. {
  111. var parent = Parent;
  112. if (parent != null)
  113. {
  114. var item = parent as T;
  115. // Just in case the user decided to nest episodes.
  116. // Not officially supported but in some cases we can handle it.
  117. if (item == null)
  118. {
  119. var parents = parent.GetParents();
  120. foreach (var currentParent in parents)
  121. {
  122. if (currentParent is T)
  123. {
  124. return true;
  125. }
  126. }
  127. }
  128. return item != null;
  129. }
  130. return false;
  131. }
  132. /// <summary>
  133. /// Determines whether the specified <see cref="object" /> is equal to this instance.
  134. /// </summary>
  135. /// <param name="obj">The object to compare with the current object.</param>
  136. /// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  137. public override bool Equals(object obj)
  138. {
  139. return Equals(obj as ItemResolveArgs);
  140. }
  141. /// <summary>
  142. /// Adds the additional location.
  143. /// </summary>
  144. /// <param name="path">The path.</param>
  145. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
  146. public void AddAdditionalLocation(string path)
  147. {
  148. if (string.IsNullOrEmpty(path))
  149. {
  150. throw new ArgumentException("The path was empty or null.", nameof(path));
  151. }
  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. if (string.IsNullOrEmpty(name))
  165. {
  166. throw new ArgumentException("The name was empty or null.", nameof(name));
  167. }
  168. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  169. }
  170. /// <summary>
  171. /// Gets the file system entry by path.
  172. /// </summary>
  173. /// <param name="path">The path.</param>
  174. /// <returns>FileSystemInfo.</returns>
  175. /// <exception cref="ArgumentNullException">Throws if path is invalid.</exception>
  176. public FileSystemMetadata GetFileSystemEntryByPath(string path)
  177. {
  178. if (string.IsNullOrEmpty(path))
  179. {
  180. throw new ArgumentException("The path was empty or null.", nameof(path));
  181. }
  182. foreach (var file in FileSystemChildren)
  183. {
  184. if (string.Equals(file.FullName, path, StringComparison.Ordinal))
  185. {
  186. return file;
  187. }
  188. }
  189. return null;
  190. }
  191. /// <summary>
  192. /// Determines whether [contains file system entry by name] [the specified name].
  193. /// </summary>
  194. /// <param name="name">The name.</param>
  195. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  196. public bool ContainsFileSystemEntryByName(string name)
  197. {
  198. return GetFileSystemEntryByName(name) != null;
  199. }
  200. public string GetCollectionType()
  201. {
  202. return CollectionType;
  203. }
  204. /// <summary>
  205. /// Gets the configured content type for the path.
  206. /// </summary>
  207. /// <remarks>
  208. /// This is subject to future refactoring as it relies on a static property in BaseItem.
  209. /// </remarks>
  210. /// <returns>The configured content type.</returns>
  211. public string GetConfiguredContentType()
  212. {
  213. return BaseItem.LibraryManager.GetConfiguredContentType(Path);
  214. }
  215. /// <summary>
  216. /// Gets the file system children that do not hit the ignore file check.
  217. /// </summary>
  218. /// <remarks>
  219. /// This is subject to future refactoring as it relies on a static property in BaseItem.
  220. /// </remarks>
  221. /// <returns>The file system children that are not ignored.</returns>
  222. public IEnumerable<FileSystemMetadata> GetActualFileSystemChildren()
  223. {
  224. var numberOfChildren = FileSystemChildren.Length;
  225. for (var i = 0; i < numberOfChildren; i++)
  226. {
  227. var child = FileSystemChildren[i];
  228. if (BaseItem.LibraryManager.IgnoreFile(child, Parent))
  229. {
  230. continue;
  231. }
  232. yield return child;
  233. }
  234. }
  235. /// <summary>
  236. /// Returns a hash code for this instance.
  237. /// </summary>
  238. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  239. public override int GetHashCode()
  240. {
  241. return Path.GetHashCode(StringComparison.Ordinal);
  242. }
  243. /// <summary>
  244. /// Equals the specified args.
  245. /// </summary>
  246. /// <param name="args">The args.</param>
  247. /// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns>
  248. protected bool Equals(ItemResolveArgs args)
  249. {
  250. if (args != null)
  251. {
  252. if (args.Path == null && Path == null)
  253. {
  254. return true;
  255. }
  256. return args.Path != null && BaseItem.FileSystem.AreEqual(args.Path, Path);
  257. }
  258. return false;
  259. }
  260. }
  261. }