ItemResolveArgs.cs 10 KB

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