ItemResolveArgs.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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. public bool HasParent<T>()
  138. where T : Folder
  139. {
  140. var parent = Parent;
  141. if (parent != null)
  142. {
  143. var item = parent as T;
  144. // Just in case the user decided to nest episodes.
  145. // Not officially supported but in some cases we can handle it.
  146. if (item == null)
  147. {
  148. item = parent.Parents.OfType<T>().FirstOrDefault();
  149. }
  150. return item != null;
  151. }
  152. return false;
  153. }
  154. /// <summary>
  155. /// Adds the additional location.
  156. /// </summary>
  157. /// <param name="path">The path.</param>
  158. /// <exception cref="System.ArgumentNullException"></exception>
  159. public void AddAdditionalLocation(string path)
  160. {
  161. if (string.IsNullOrEmpty(path))
  162. {
  163. throw new ArgumentNullException();
  164. }
  165. if (AdditionalLocations == null)
  166. {
  167. AdditionalLocations = new List<string>();
  168. }
  169. AdditionalLocations.Add(path);
  170. }
  171. /// <summary>
  172. /// Gets the physical locations.
  173. /// </summary>
  174. /// <value>The physical locations.</value>
  175. public IEnumerable<string> PhysicalLocations
  176. {
  177. get
  178. {
  179. var paths = string.IsNullOrWhiteSpace(Path) ? new string[] { } : new[] { Path };
  180. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
  181. }
  182. }
  183. /// <summary>
  184. /// Gets the name of the file system entry by.
  185. /// </summary>
  186. /// <param name="name">The name.</param>
  187. /// <returns>FileSystemInfo.</returns>
  188. /// <exception cref="System.ArgumentNullException"></exception>
  189. public FileSystemInfo GetFileSystemEntryByName(string name)
  190. {
  191. if (string.IsNullOrEmpty(name))
  192. {
  193. throw new ArgumentNullException();
  194. }
  195. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  196. }
  197. /// <summary>
  198. /// Gets the file system entry by path.
  199. /// </summary>
  200. /// <param name="path">The path.</param>
  201. /// <returns>FileSystemInfo.</returns>
  202. /// <exception cref="System.ArgumentNullException"></exception>
  203. public FileSystemInfo GetFileSystemEntryByPath(string path)
  204. {
  205. if (string.IsNullOrEmpty(path))
  206. {
  207. throw new ArgumentNullException();
  208. }
  209. if (FileSystemDictionary != null)
  210. {
  211. FileSystemInfo entry;
  212. if (FileSystemDictionary.TryGetValue(path, out entry))
  213. {
  214. return entry;
  215. }
  216. }
  217. return null;
  218. }
  219. /// <summary>
  220. /// Determines whether [contains meta file by name] [the specified name].
  221. /// </summary>
  222. /// <param name="name">The name.</param>
  223. /// <returns><c>true</c> if [contains meta file by name] [the specified name]; otherwise, <c>false</c>.</returns>
  224. public bool ContainsMetaFileByName(string name)
  225. {
  226. if (string.IsNullOrEmpty(name))
  227. {
  228. throw new ArgumentNullException();
  229. }
  230. return GetFileSystemEntryByName(name) != null;
  231. }
  232. /// <summary>
  233. /// Determines whether [contains file system entry by name] [the specified name].
  234. /// </summary>
  235. /// <param name="name">The name.</param>
  236. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  237. public bool ContainsFileSystemEntryByName(string name)
  238. {
  239. return GetFileSystemEntryByName(name) != null;
  240. }
  241. public string GetCollectionType()
  242. {
  243. return CollectionType;
  244. }
  245. public string CollectionType { get; set; }
  246. #region Equality Overrides
  247. /// <summary>
  248. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  249. /// </summary>
  250. /// <param name="obj">The object to compare with the current object.</param>
  251. /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  252. public override bool Equals(object obj)
  253. {
  254. return (Equals(obj as ItemResolveArgs));
  255. }
  256. /// <summary>
  257. /// Returns a hash code for this instance.
  258. /// </summary>
  259. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  260. public override int GetHashCode()
  261. {
  262. return Path.GetHashCode();
  263. }
  264. /// <summary>
  265. /// Equalses the specified args.
  266. /// </summary>
  267. /// <param name="args">The args.</param>
  268. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  269. protected bool Equals(ItemResolveArgs args)
  270. {
  271. if (args != null)
  272. {
  273. if (args.Path == null && Path == null) return true;
  274. return args.Path != null && args.Path.Equals(Path, StringComparison.OrdinalIgnoreCase);
  275. }
  276. return false;
  277. }
  278. #endregion
  279. }
  280. }