ItemResolveArgs.cs 9.8 KB

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