ItemResolveArgs.cs 9.0 KB

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