2
0

ItemResolveArgs.cs 10 KB

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