ItemResolveArgs.cs 10 KB

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