ItemResolveArgs.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. item = parent.GetParents().OfType<T>().FirstOrDefault();
  118. }
  119. return item != null;
  120. }
  121. return false;
  122. }
  123. /// <summary>
  124. /// Adds the additional location.
  125. /// </summary>
  126. /// <param name="path">The path.</param>
  127. /// <exception cref="System.ArgumentNullException"></exception>
  128. public void AddAdditionalLocation(string path)
  129. {
  130. if (string.IsNullOrEmpty(path))
  131. {
  132. throw new ArgumentNullException();
  133. }
  134. if (AdditionalLocations == null)
  135. {
  136. AdditionalLocations = new List<string>();
  137. }
  138. AdditionalLocations.Add(path);
  139. }
  140. /// <summary>
  141. /// Gets the physical locations.
  142. /// </summary>
  143. /// <value>The physical locations.</value>
  144. public IEnumerable<string> PhysicalLocations
  145. {
  146. get
  147. {
  148. var paths = string.IsNullOrWhiteSpace(Path) ? new string[] { } : new[] { Path };
  149. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
  150. }
  151. }
  152. /// <summary>
  153. /// Gets the name of the file system entry by.
  154. /// </summary>
  155. /// <param name="name">The name.</param>
  156. /// <returns>FileSystemInfo.</returns>
  157. /// <exception cref="System.ArgumentNullException"></exception>
  158. public FileSystemMetadata GetFileSystemEntryByName(string name)
  159. {
  160. if (string.IsNullOrEmpty(name))
  161. {
  162. throw new ArgumentNullException();
  163. }
  164. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  165. }
  166. /// <summary>
  167. /// Gets the file system entry by path.
  168. /// </summary>
  169. /// <param name="path">The path.</param>
  170. /// <returns>FileSystemInfo.</returns>
  171. /// <exception cref="System.ArgumentNullException"></exception>
  172. public FileSystemMetadata GetFileSystemEntryByPath(string path)
  173. {
  174. if (string.IsNullOrEmpty(path))
  175. {
  176. throw new ArgumentNullException();
  177. }
  178. foreach (var file in FileSystemChildren)
  179. {
  180. if (string.Equals(file.FullName, path, StringComparison.Ordinal))
  181. {
  182. return file;
  183. }
  184. }
  185. return null;
  186. }
  187. /// <summary>
  188. /// Determines whether [contains file system entry by name] [the specified name].
  189. /// </summary>
  190. /// <param name="name">The name.</param>
  191. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  192. public bool ContainsFileSystemEntryByName(string name)
  193. {
  194. return GetFileSystemEntryByName(name) != null;
  195. }
  196. public string GetCollectionType()
  197. {
  198. return CollectionType;
  199. }
  200. public string CollectionType { get; set; }
  201. #region Equality Overrides
  202. /// <summary>
  203. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  204. /// </summary>
  205. /// <param name="obj">The object to compare with the current object.</param>
  206. /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  207. public override bool Equals(object obj)
  208. {
  209. return Equals(obj as ItemResolveArgs);
  210. }
  211. /// <summary>
  212. /// Returns a hash code for this instance.
  213. /// </summary>
  214. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  215. public override int GetHashCode()
  216. {
  217. return Path.GetHashCode();
  218. }
  219. /// <summary>
  220. /// Equalses the specified args.
  221. /// </summary>
  222. /// <param name="args">The args.</param>
  223. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  224. protected bool Equals(ItemResolveArgs args)
  225. {
  226. if (args != null)
  227. {
  228. if (args.Path == null && Path == null) return true;
  229. return args.Path != null && BaseItem.FileSystem.AreEqual(args.Path, Path);
  230. }
  231. return false;
  232. }
  233. #endregion
  234. }
  235. }