ItemResolveArgs.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using MediaBrowser.Controller.Entities;
  5. using MediaBrowser.Controller.Providers;
  6. using MediaBrowser.Model.Configuration;
  7. using MediaBrowser.Model.IO;
  8. namespace MediaBrowser.Controller.Library
  9. {
  10. /// <summary>
  11. /// These are arguments relating to the file system that are collected once and then referred to
  12. /// whenever needed. Primarily for entity resolution.
  13. /// </summary>
  14. public class ItemResolveArgs : EventArgs
  15. {
  16. /// <summary>
  17. /// The _app paths.
  18. /// </summary>
  19. private readonly IServerApplicationPaths _appPaths;
  20. public IDirectoryService DirectoryService { get; private set; }
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="ItemResolveArgs" /> class.
  23. /// </summary>
  24. /// <param name="appPaths">The app paths.</param>
  25. /// <param name="directoryService">The directory service.</param>
  26. public ItemResolveArgs(IServerApplicationPaths appPaths, IDirectoryService directoryService)
  27. {
  28. _appPaths = appPaths;
  29. DirectoryService = directoryService;
  30. }
  31. /// <summary>
  32. /// Gets the file system children.
  33. /// </summary>
  34. /// <value>The file system children.</value>
  35. public FileSystemMetadata[] FileSystemChildren { get; set; }
  36. public LibraryOptions LibraryOptions { get; set; }
  37. public LibraryOptions GetLibraryOptions()
  38. {
  39. return LibraryOptions ?? (LibraryOptions = Parent == null ? new LibraryOptions() : BaseItem.LibraryManager.GetLibraryOptions(Parent));
  40. }
  41. /// <summary>
  42. /// Gets or sets the parent.
  43. /// </summary>
  44. /// <value>The parent.</value>
  45. public Folder Parent { get; set; }
  46. /// <summary>
  47. /// Gets or sets the file info.
  48. /// </summary>
  49. /// <value>The file info.</value>
  50. public FileSystemMetadata FileInfo { get; set; }
  51. /// <summary>
  52. /// Gets or sets the path.
  53. /// </summary>
  54. /// <value>The path.</value>
  55. public string Path { get; set; }
  56. /// <summary>
  57. /// Gets a value indicating whether this instance is directory.
  58. /// </summary>
  59. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  60. public bool IsDirectory => FileInfo.IsDirectory;
  61. /// <summary>
  62. /// Gets a value indicating whether this instance is vf.
  63. /// </summary>
  64. /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
  65. public bool IsVf
  66. {
  67. // we should be considered a virtual folder if we are a child of one of the children of the system root folder.
  68. // this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
  69. // the root but not be equal to it
  70. get
  71. {
  72. if (!IsDirectory)
  73. {
  74. return false;
  75. }
  76. var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
  77. return parentDir.Length > _appPaths.RootFolderPath.Length
  78. && parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
  79. }
  80. }
  81. /// <summary>
  82. /// Gets a value indicating whether this instance is physical root.
  83. /// </summary>
  84. /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
  85. public bool IsPhysicalRoot => IsDirectory && BaseItem.FileSystem.AreEqual(Path, _appPaths.RootFolderPath);
  86. /// <summary>
  87. /// Gets or sets the additional locations.
  88. /// </summary>
  89. /// <value>The additional locations.</value>
  90. private List<string> AdditionalLocations { get; set; }
  91. public bool HasParent<T>()
  92. where T : Folder
  93. {
  94. var parent = Parent;
  95. if (parent != null)
  96. {
  97. var item = parent as T;
  98. // Just in case the user decided to nest episodes.
  99. // Not officially supported but in some cases we can handle it.
  100. if (item == null)
  101. {
  102. var parents = parent.GetParents();
  103. foreach (var currentParent in parents)
  104. {
  105. if (currentParent is T)
  106. {
  107. return true;
  108. }
  109. }
  110. }
  111. return item != null;
  112. }
  113. return false;
  114. }
  115. /// <summary>
  116. /// Adds the additional location.
  117. /// </summary>
  118. /// <param name="path">The path.</param>
  119. /// <exception cref="ArgumentNullException"></exception>
  120. public void AddAdditionalLocation(string path)
  121. {
  122. if (string.IsNullOrEmpty(path))
  123. {
  124. throw new ArgumentException("The path was empty or null.", nameof(path));
  125. }
  126. if (AdditionalLocations == null)
  127. {
  128. AdditionalLocations = new List<string>();
  129. }
  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"></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();
  210. }
  211. /// <summary>
  212. /// Equals the specified args.
  213. /// </summary>
  214. /// <param name="args">The args.</param>
  215. /// <returns><c>true</c> if XXXX, <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. }