ItemResolveArgs.cs 10 KB

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