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