ItemResolveArgs.cs 9.0 KB

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