ItemResolveArgs.cs 9.0 KB

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