ItemResolveArgs.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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.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. public IDirectoryService DirectoryService { get; }
  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 == null ? new LibraryOptions() : BaseItem.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. public bool HasParent<T>()
  95. where T : Folder
  96. {
  97. var parent = Parent;
  98. if (parent != null)
  99. {
  100. var item = parent as T;
  101. // Just in case the user decided to nest episodes.
  102. // Not officially supported but in some cases we can handle it.
  103. if (item == null)
  104. {
  105. var parents = parent.GetParents();
  106. foreach (var currentParent in parents)
  107. {
  108. if (currentParent is T)
  109. {
  110. return true;
  111. }
  112. }
  113. }
  114. return item != null;
  115. }
  116. return false;
  117. }
  118. /// <summary>
  119. /// Adds the additional location.
  120. /// </summary>
  121. /// <param name="path">The path.</param>
  122. /// <exception cref="ArgumentNullException"><paramref name="path"/> is <c>null</c> or empty.</exception>
  123. public void AddAdditionalLocation(string path)
  124. {
  125. if (string.IsNullOrEmpty(path))
  126. {
  127. throw new ArgumentException("The path was empty or null.", nameof(path));
  128. }
  129. AdditionalLocations ??= new List<string>();
  130. AdditionalLocations.Add(path);
  131. }
  132. // REVIEW: @bond
  133. /// <summary>
  134. /// Gets the physical locations.
  135. /// </summary>
  136. /// <value>The physical locations.</value>
  137. public string[] PhysicalLocations
  138. {
  139. get
  140. {
  141. var paths = string.IsNullOrEmpty(Path) ? Array.Empty<string>() : new[] { Path };
  142. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations).ToArray();
  143. }
  144. }
  145. /// <summary>
  146. /// Gets the name of the file system entry by.
  147. /// </summary>
  148. /// <param name="name">The name.</param>
  149. /// <returns>FileSystemInfo.</returns>
  150. /// <exception cref="ArgumentNullException"><paramref name="name"/> is <c>null</c> or empty.</exception>
  151. public FileSystemMetadata GetFileSystemEntryByName(string name)
  152. {
  153. if (string.IsNullOrEmpty(name))
  154. {
  155. throw new ArgumentException("The name was empty or null.", nameof(name));
  156. }
  157. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  158. }
  159. /// <summary>
  160. /// Gets the file system entry by path.
  161. /// </summary>
  162. /// <param name="path">The path.</param>
  163. /// <returns>FileSystemInfo.</returns>
  164. /// <exception cref="ArgumentNullException"></exception>
  165. public FileSystemMetadata GetFileSystemEntryByPath(string path)
  166. {
  167. if (string.IsNullOrEmpty(path))
  168. {
  169. throw new ArgumentException("The path was empty or null.", nameof(path));
  170. }
  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) != null;
  188. }
  189. public string GetCollectionType()
  190. {
  191. return CollectionType;
  192. }
  193. public string CollectionType { get; set; }
  194. /// <summary>
  195. /// Determines whether the specified <see cref="object" /> is equal to this instance.
  196. /// </summary>
  197. /// <param name="obj">The object to compare with the current object.</param>
  198. /// <returns><c>true</c> if the specified <see cref="object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  199. public override bool Equals(object obj)
  200. {
  201. return Equals(obj as ItemResolveArgs);
  202. }
  203. /// <summary>
  204. /// Returns a hash code for this instance.
  205. /// </summary>
  206. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  207. public override int GetHashCode()
  208. {
  209. return Path.GetHashCode(StringComparison.Ordinal);
  210. }
  211. /// <summary>
  212. /// Equals the specified args.
  213. /// </summary>
  214. /// <param name="args">The args.</param>
  215. /// <returns><c>true</c> if the arguments are the same, <c>false</c> otherwise.</returns>
  216. protected bool Equals(ItemResolveArgs args)
  217. {
  218. if (args != null)
  219. {
  220. if (args.Path == null && Path == null)
  221. {
  222. return true;
  223. }
  224. return args.Path != null && BaseItem.FileSystem.AreEqual(args.Path, Path);
  225. }
  226. return false;
  227. }
  228. }
  229. }