ItemResolveArgs.cs 10 KB

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