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