ItemResolveArgs.cs 9.2 KB

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