ItemResolveArgs.cs 11 KB

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