ItemResolveArgs.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. using MediaBrowser.Controller.Entities;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. using System.Linq;
  6. namespace MediaBrowser.Controller.Library
  7. {
  8. /// <summary>
  9. /// These are arguments relating to the file system that are collected once and then referred to
  10. /// whenever needed. Primarily for entity resolution.
  11. /// </summary>
  12. public class ItemResolveArgs : EventArgs
  13. {
  14. /// <summary>
  15. /// The _app paths
  16. /// </summary>
  17. private readonly IServerApplicationPaths _appPaths;
  18. private readonly ILibraryManager _libraryManager;
  19. /// <summary>
  20. /// Initializes a new instance of the <see cref="ItemResolveArgs" /> class.
  21. /// </summary>
  22. /// <param name="appPaths">The app paths.</param>
  23. /// <param name="libraryManager">The library manager.</param>
  24. public ItemResolveArgs(IServerApplicationPaths appPaths, ILibraryManager libraryManager)
  25. {
  26. _appPaths = appPaths;
  27. _libraryManager = libraryManager;
  28. }
  29. /// <summary>
  30. /// Gets the file system children.
  31. /// </summary>
  32. /// <value>The file system children.</value>
  33. public IEnumerable<FileSystemInfo> FileSystemChildren
  34. {
  35. get
  36. {
  37. var dict = FileSystemDictionary;
  38. if (dict == null)
  39. {
  40. return new List<FileSystemInfo>();
  41. }
  42. return dict.Values;
  43. }
  44. }
  45. /// <summary>
  46. /// Gets or sets the file system dictionary.
  47. /// </summary>
  48. /// <value>The file system dictionary.</value>
  49. public Dictionary<string, FileSystemInfo> FileSystemDictionary { get; set; }
  50. /// <summary>
  51. /// Gets or sets the parent.
  52. /// </summary>
  53. /// <value>The parent.</value>
  54. public Folder Parent { get; set; }
  55. /// <summary>
  56. /// Gets or sets the file info.
  57. /// </summary>
  58. /// <value>The file info.</value>
  59. public FileSystemInfo FileInfo { get; set; }
  60. /// <summary>
  61. /// Gets or sets the path.
  62. /// </summary>
  63. /// <value>The path.</value>
  64. public string Path { get; set; }
  65. /// <summary>
  66. /// Gets a value indicating whether this instance is directory.
  67. /// </summary>
  68. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  69. public bool IsDirectory
  70. {
  71. get
  72. {
  73. return (FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  74. }
  75. }
  76. /// <summary>
  77. /// Gets a value indicating whether this instance is hidden.
  78. /// </summary>
  79. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  80. public bool IsHidden
  81. {
  82. get
  83. {
  84. return (FileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  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 a value indicating whether this instance is root.
  120. /// </summary>
  121. /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
  122. public bool IsRoot
  123. {
  124. get
  125. {
  126. return Parent == null;
  127. }
  128. }
  129. /// <summary>
  130. /// Gets or sets the additional locations.
  131. /// </summary>
  132. /// <value>The additional locations.</value>
  133. private List<string> AdditionalLocations { get; set; }
  134. /// <summary>
  135. /// Adds the additional location.
  136. /// </summary>
  137. /// <param name="path">The path.</param>
  138. /// <exception cref="System.ArgumentNullException"></exception>
  139. public void AddAdditionalLocation(string path)
  140. {
  141. if (string.IsNullOrEmpty(path))
  142. {
  143. throw new ArgumentNullException();
  144. }
  145. if (AdditionalLocations == null)
  146. {
  147. AdditionalLocations = new List<string>();
  148. }
  149. AdditionalLocations.Add(path);
  150. }
  151. /// <summary>
  152. /// Gets the physical locations.
  153. /// </summary>
  154. /// <value>The physical locations.</value>
  155. public IEnumerable<string> PhysicalLocations
  156. {
  157. get
  158. {
  159. var paths = string.IsNullOrWhiteSpace(Path) ? new string[] {} : new[] {Path};
  160. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
  161. }
  162. }
  163. /// <summary>
  164. /// Gets the name of the file system entry by.
  165. /// </summary>
  166. /// <param name="name">The name.</param>
  167. /// <returns>FileSystemInfo.</returns>
  168. /// <exception cref="System.ArgumentNullException"></exception>
  169. public FileSystemInfo GetFileSystemEntryByName(string name)
  170. {
  171. if (string.IsNullOrEmpty(name))
  172. {
  173. throw new ArgumentNullException();
  174. }
  175. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  176. }
  177. /// <summary>
  178. /// Gets the file system entry by path.
  179. /// </summary>
  180. /// <param name="path">The path.</param>
  181. /// <returns>FileSystemInfo.</returns>
  182. /// <exception cref="System.ArgumentNullException"></exception>
  183. public FileSystemInfo GetFileSystemEntryByPath(string path)
  184. {
  185. if (string.IsNullOrEmpty(path))
  186. {
  187. throw new ArgumentNullException();
  188. }
  189. if (FileSystemDictionary != null)
  190. {
  191. FileSystemInfo entry;
  192. if (FileSystemDictionary.TryGetValue(path, out entry))
  193. {
  194. return entry;
  195. }
  196. }
  197. return null;
  198. }
  199. /// <summary>
  200. /// Gets the name of the meta file by.
  201. /// </summary>
  202. /// <param name="name">The name.</param>
  203. /// <returns>FileSystemInfo.</returns>
  204. /// <exception cref="System.ArgumentNullException"></exception>
  205. public FileSystemInfo GetMetaFileByName(string name)
  206. {
  207. if (string.IsNullOrEmpty(name))
  208. {
  209. throw new ArgumentNullException();
  210. }
  211. return GetFileSystemEntryByName(name);
  212. }
  213. /// <summary>
  214. /// Determines whether [contains meta file by name] [the specified name].
  215. /// </summary>
  216. /// <param name="name">The name.</param>
  217. /// <returns><c>true</c> if [contains meta file by name] [the specified name]; otherwise, <c>false</c>.</returns>
  218. public bool ContainsMetaFileByName(string name)
  219. {
  220. return GetMetaFileByName(name) != null;
  221. }
  222. /// <summary>
  223. /// Determines whether [contains file system entry by name] [the specified name].
  224. /// </summary>
  225. /// <param name="name">The name.</param>
  226. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  227. public bool ContainsFileSystemEntryByName(string name)
  228. {
  229. return GetFileSystemEntryByName(name) != null;
  230. }
  231. private bool _collectionTypeDiscovered;
  232. private string _collectionType;
  233. public string GetCollectionType()
  234. {
  235. if (!_collectionTypeDiscovered)
  236. {
  237. _collectionType = Parent == null ? null : _libraryManager.FindCollectionType(Parent);
  238. _collectionTypeDiscovered = true;
  239. }
  240. return _collectionType;
  241. }
  242. #region Equality Overrides
  243. /// <summary>
  244. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  245. /// </summary>
  246. /// <param name="obj">The object to compare with the current object.</param>
  247. /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  248. public override bool Equals(object obj)
  249. {
  250. return (Equals(obj as ItemResolveArgs));
  251. }
  252. /// <summary>
  253. /// Returns a hash code for this instance.
  254. /// </summary>
  255. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  256. public override int GetHashCode()
  257. {
  258. return Path.GetHashCode();
  259. }
  260. /// <summary>
  261. /// Equalses the specified args.
  262. /// </summary>
  263. /// <param name="args">The args.</param>
  264. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  265. protected bool Equals(ItemResolveArgs args)
  266. {
  267. if (args != null)
  268. {
  269. if (args.Path == null && Path == null) return true;
  270. return args.Path != null && args.Path.Equals(Path, StringComparison.OrdinalIgnoreCase);
  271. }
  272. return false;
  273. }
  274. #endregion
  275. }
  276. }