ItemResolveArgs.cs 9.1 KB

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