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