ItemResolveArgs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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 system file.
  89. /// </summary>
  90. /// <value><c>true</c> if this instance is system file; otherwise, <c>false</c>.</value>
  91. public bool IsSystemFile
  92. {
  93. get
  94. {
  95. return (FileInfo.Attributes & FileAttributes.System) == FileAttributes.System;
  96. }
  97. }
  98. /// <summary>
  99. /// Gets a value indicating whether this instance is vf.
  100. /// </summary>
  101. /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
  102. public bool IsVf
  103. {
  104. // we should be considered a virtual folder if we are a child of one of the children of the system root folder.
  105. // this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
  106. // the root but not be equal to it
  107. get
  108. {
  109. if (!IsDirectory)
  110. {
  111. return false;
  112. }
  113. var parentDir = System.IO.Path.GetDirectoryName(Path) ?? string.Empty;
  114. return (parentDir.Length > _appPaths.RootFolderPath.Length
  115. && parentDir.StartsWith(_appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase));
  116. }
  117. }
  118. /// <summary>
  119. /// Gets a value indicating whether this instance is physical root.
  120. /// </summary>
  121. /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
  122. public bool IsPhysicalRoot
  123. {
  124. get
  125. {
  126. return IsDirectory && string.Equals(Path, _appPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
  127. }
  128. }
  129. /// <summary>
  130. /// Gets a value indicating whether this instance is root.
  131. /// </summary>
  132. /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
  133. public bool IsRoot
  134. {
  135. get
  136. {
  137. return Parent == null;
  138. }
  139. }
  140. /// <summary>
  141. /// Gets or sets the additional locations.
  142. /// </summary>
  143. /// <value>The additional locations.</value>
  144. private List<string> AdditionalLocations { get; set; }
  145. /// <summary>
  146. /// Adds the additional location.
  147. /// </summary>
  148. /// <param name="path">The path.</param>
  149. /// <exception cref="System.ArgumentNullException"></exception>
  150. public void AddAdditionalLocation(string path)
  151. {
  152. if (string.IsNullOrEmpty(path))
  153. {
  154. throw new ArgumentNullException();
  155. }
  156. if (AdditionalLocations == null)
  157. {
  158. AdditionalLocations = new List<string>();
  159. }
  160. AdditionalLocations.Add(path);
  161. }
  162. /// <summary>
  163. /// Gets the physical locations.
  164. /// </summary>
  165. /// <value>The physical locations.</value>
  166. public IEnumerable<string> PhysicalLocations
  167. {
  168. get
  169. {
  170. var paths = string.IsNullOrWhiteSpace(Path) ? new string[] {} : new[] {Path};
  171. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
  172. }
  173. }
  174. /// <summary>
  175. /// Gets the name of the file system entry by.
  176. /// </summary>
  177. /// <param name="name">The name.</param>
  178. /// <returns>FileSystemInfo.</returns>
  179. /// <exception cref="System.ArgumentNullException"></exception>
  180. public FileSystemInfo GetFileSystemEntryByName(string name)
  181. {
  182. if (string.IsNullOrEmpty(name))
  183. {
  184. throw new ArgumentNullException();
  185. }
  186. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  187. }
  188. /// <summary>
  189. /// Gets the file system entry by path.
  190. /// </summary>
  191. /// <param name="path">The path.</param>
  192. /// <returns>FileSystemInfo.</returns>
  193. /// <exception cref="System.ArgumentNullException"></exception>
  194. public FileSystemInfo GetFileSystemEntryByPath(string path)
  195. {
  196. if (string.IsNullOrEmpty(path))
  197. {
  198. throw new ArgumentNullException();
  199. }
  200. if (FileSystemDictionary != null)
  201. {
  202. FileSystemInfo entry;
  203. if (FileSystemDictionary.TryGetValue(path, out entry))
  204. {
  205. return entry;
  206. }
  207. }
  208. return null;
  209. }
  210. /// <summary>
  211. /// Gets the meta file by path.
  212. /// </summary>
  213. /// <param name="path">The path.</param>
  214. /// <returns>FileSystemInfo.</returns>
  215. /// <exception cref="System.ArgumentNullException"></exception>
  216. public FileSystemInfo GetMetaFileByPath(string path)
  217. {
  218. if (string.IsNullOrEmpty(path))
  219. {
  220. throw new ArgumentNullException();
  221. }
  222. return GetFileSystemEntryByPath(path);
  223. }
  224. /// <summary>
  225. /// Gets the name of the meta file by.
  226. /// </summary>
  227. /// <param name="name">The name.</param>
  228. /// <returns>FileSystemInfo.</returns>
  229. /// <exception cref="System.ArgumentNullException"></exception>
  230. public FileSystemInfo GetMetaFileByName(string name)
  231. {
  232. if (string.IsNullOrEmpty(name))
  233. {
  234. throw new ArgumentNullException();
  235. }
  236. return GetFileSystemEntryByName(name);
  237. }
  238. /// <summary>
  239. /// Determines whether [contains meta file by name] [the specified name].
  240. /// </summary>
  241. /// <param name="name">The name.</param>
  242. /// <returns><c>true</c> if [contains meta file by name] [the specified name]; otherwise, <c>false</c>.</returns>
  243. public bool ContainsMetaFileByName(string name)
  244. {
  245. return GetMetaFileByName(name) != null;
  246. }
  247. /// <summary>
  248. /// Determines whether [contains file system entry by name] [the specified name].
  249. /// </summary>
  250. /// <param name="name">The name.</param>
  251. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  252. public bool ContainsFileSystemEntryByName(string name)
  253. {
  254. return GetFileSystemEntryByName(name) != null;
  255. }
  256. private bool _collectionTypeDiscovered;
  257. private string _collectionType;
  258. public string GetCollectionType()
  259. {
  260. if (!_collectionTypeDiscovered)
  261. {
  262. _collectionType = Parent == null ? null : _libraryManager.FindCollectionType(Parent);
  263. _collectionTypeDiscovered = true;
  264. }
  265. return _collectionType;
  266. }
  267. #region Equality Overrides
  268. /// <summary>
  269. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  270. /// </summary>
  271. /// <param name="obj">The object to compare with the current object.</param>
  272. /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  273. public override bool Equals(object obj)
  274. {
  275. return (Equals(obj as ItemResolveArgs));
  276. }
  277. /// <summary>
  278. /// Returns a hash code for this instance.
  279. /// </summary>
  280. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  281. public override int GetHashCode()
  282. {
  283. return Path.GetHashCode();
  284. }
  285. /// <summary>
  286. /// Equalses the specified args.
  287. /// </summary>
  288. /// <param name="args">The args.</param>
  289. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  290. protected bool Equals(ItemResolveArgs args)
  291. {
  292. if (args != null)
  293. {
  294. if (args.Path == null && Path == null) return true;
  295. return args.Path != null && args.Path.Equals(Path, StringComparison.OrdinalIgnoreCase);
  296. }
  297. return false;
  298. }
  299. #endregion
  300. }
  301. }