ItemResolveArgs.cs 9.2 KB

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