ItemResolveArgs.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 is 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 is 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 is not 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 is 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 is not 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. ArgumentException.ThrowIfNullOrEmpty(path);
  149. AdditionalLocations ??= new List<string>();
  150. AdditionalLocations.Add(path);
  151. }
  152. // REVIEW: @bond
  153. /// <summary>
  154. /// Gets the name of the file system entry by.
  155. /// </summary>
  156. /// <param name="name">The name.</param>
  157. /// <returns>FileSystemInfo.</returns>
  158. /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
  159. public FileSystemMetadata GetFileSystemEntryByName(string name)
  160. {
  161. ArgumentException.ThrowIfNullOrEmpty(name);
  162. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  163. }
  164. /// <summary>
  165. /// Gets the file system entry by path.
  166. /// </summary>
  167. /// <param name="path">The path.</param>
  168. /// <returns>FileSystemInfo.</returns>
  169. /// <exception cref="ArgumentNullException">Throws if path is invalid.</exception>
  170. public FileSystemMetadata GetFileSystemEntryByPath(string path)
  171. {
  172. ArgumentException.ThrowIfNullOrEmpty(path);
  173. foreach (var file in FileSystemChildren)
  174. {
  175. if (string.Equals(file.FullName, path, StringComparison.Ordinal))
  176. {
  177. return file;
  178. }
  179. }
  180. return null;
  181. }
  182. /// <summary>
  183. /// Determines whether [contains file system entry by name] [the specified name].
  184. /// </summary>
  185. /// <param name="name">The name.</param>
  186. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  187. public bool ContainsFileSystemEntryByName(string name)
  188. {
  189. return GetFileSystemEntryByName(name) != null;
  190. }
  191. public string GetCollectionType()
  192. {
  193. return CollectionType;
  194. }
  195. /// <summary>
  196. /// Gets the configured content type for the path.
  197. /// </summary>
  198. /// <remarks>
  199. /// This is subject to future refactoring as it relies on a static property in BaseItem.
  200. /// </remarks>
  201. /// <returns>The configured content type.</returns>
  202. public string GetConfiguredContentType()
  203. {
  204. return BaseItem.LibraryManager.GetConfiguredContentType(Path);
  205. }
  206. /// <summary>
  207. /// Gets the file system children that do not hit the ignore file check.
  208. /// </summary>
  209. /// <remarks>
  210. /// This is subject to future refactoring as it relies on a static property in BaseItem.
  211. /// </remarks>
  212. /// <returns>The file system children that are not ignored.</returns>
  213. public IEnumerable<FileSystemMetadata> GetActualFileSystemChildren()
  214. {
  215. var numberOfChildren = FileSystemChildren.Length;
  216. for (var i = 0; i < numberOfChildren; i++)
  217. {
  218. var child = FileSystemChildren[i];
  219. if (BaseItem.LibraryManager.IgnoreFile(child, Parent))
  220. {
  221. continue;
  222. }
  223. yield return child;
  224. }
  225. }
  226. /// <summary>
  227. /// Returns a hash code for this instance.
  228. /// </summary>
  229. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  230. public override int GetHashCode()
  231. {
  232. return Path.GetHashCode(StringComparison.Ordinal);
  233. }
  234. /// <summary>
  235. /// Equals the specified args.
  236. /// </summary>
  237. /// <param name="args">The args.</param>
  238. /// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns>
  239. protected bool Equals(ItemResolveArgs args)
  240. {
  241. if (args is not null)
  242. {
  243. if (args.Path is null && Path is null)
  244. {
  245. return true;
  246. }
  247. return args.Path is not null && BaseItem.FileSystem.AreEqual(args.Path, Path);
  248. }
  249. return false;
  250. }
  251. }
  252. }