ItemResolveArgs.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Win32;
  3. using MediaBrowser.Controller.Entities;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using MediaBrowser.Controller.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. /// Gets the file system children.
  19. /// </summary>
  20. /// <value>The file system children.</value>
  21. public IEnumerable<WIN32_FIND_DATA> FileSystemChildren
  22. {
  23. get { return FileSystemDictionary.Values; }
  24. }
  25. /// <summary>
  26. /// Gets or sets the file system dictionary.
  27. /// </summary>
  28. /// <value>The file system dictionary.</value>
  29. public Dictionary<string, WIN32_FIND_DATA> FileSystemDictionary { get; set; }
  30. /// <summary>
  31. /// Gets or sets the parent.
  32. /// </summary>
  33. /// <value>The parent.</value>
  34. public Folder Parent { get; set; }
  35. /// <summary>
  36. /// Gets or sets the file info.
  37. /// </summary>
  38. /// <value>The file info.</value>
  39. public WIN32_FIND_DATA FileInfo { get; set; }
  40. /// <summary>
  41. /// Gets or sets the path.
  42. /// </summary>
  43. /// <value>The path.</value>
  44. public string Path { get; set; }
  45. /// <summary>
  46. /// Gets a value indicating whether this instance is directory.
  47. /// </summary>
  48. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  49. public bool IsDirectory
  50. {
  51. get
  52. {
  53. return FileInfo.dwFileAttributes.HasFlag(FileAttributes.Directory);
  54. }
  55. }
  56. /// <summary>
  57. /// Gets a value indicating whether this instance is hidden.
  58. /// </summary>
  59. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  60. public bool IsHidden
  61. {
  62. get
  63. {
  64. return FileInfo.IsHidden;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets a value indicating whether this instance is system file.
  69. /// </summary>
  70. /// <value><c>true</c> if this instance is system file; otherwise, <c>false</c>.</value>
  71. public bool IsSystemFile
  72. {
  73. get
  74. {
  75. return FileInfo.IsSystemFile;
  76. }
  77. }
  78. /// <summary>
  79. /// Gets a value indicating whether this instance is vf.
  80. /// </summary>
  81. /// <value><c>true</c> if this instance is vf; otherwise, <c>false</c>.</value>
  82. public bool IsVf
  83. {
  84. // we should be considered a virtual folder if we are a child of one of the children of the system root folder.
  85. // this is a bit of a trick to determine that... the directory name of a sub-child of the root will start with
  86. // the root but not be equal to it
  87. get
  88. {
  89. if (!IsDirectory)
  90. {
  91. return false;
  92. }
  93. var parentDir = FileInfo.Path != null ? System.IO.Path.GetDirectoryName(FileInfo.Path) ?? string.Empty : string.Empty;
  94. return (parentDir.Length > Kernel.Instance.ApplicationPaths.RootFolderPath.Length
  95. && parentDir.StartsWith(Kernel.Instance.ApplicationPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase));
  96. }
  97. }
  98. /// <summary>
  99. /// Gets a value indicating whether this instance is physical root.
  100. /// </summary>
  101. /// <value><c>true</c> if this instance is physical root; otherwise, <c>false</c>.</value>
  102. public bool IsPhysicalRoot
  103. {
  104. get
  105. {
  106. return IsDirectory && Path.Equals(Kernel.Instance.ApplicationPaths.RootFolderPath, StringComparison.OrdinalIgnoreCase);
  107. }
  108. }
  109. /// <summary>
  110. /// Gets a value indicating whether this instance is root.
  111. /// </summary>
  112. /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
  113. public bool IsRoot
  114. {
  115. get
  116. {
  117. return Parent == null;
  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. /// <summary>
  126. /// Adds the additional location.
  127. /// </summary>
  128. /// <param name="path">The path.</param>
  129. /// <exception cref="System.ArgumentNullException"></exception>
  130. public void AddAdditionalLocation(string path)
  131. {
  132. if (string.IsNullOrEmpty(path))
  133. {
  134. throw new ArgumentNullException();
  135. }
  136. if (AdditionalLocations == null)
  137. {
  138. AdditionalLocations = new List<string>();
  139. }
  140. AdditionalLocations.Add(path);
  141. }
  142. /// <summary>
  143. /// Gets the physical locations.
  144. /// </summary>
  145. /// <value>The physical locations.</value>
  146. public IEnumerable<string> PhysicalLocations
  147. {
  148. get
  149. {
  150. var paths = string.IsNullOrWhiteSpace(Path) ? new string[] {} : new[] {Path};
  151. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
  152. }
  153. }
  154. /// <summary>
  155. /// Store these to reduce disk access in Resolvers
  156. /// </summary>
  157. /// <value>The metadata file dictionary.</value>
  158. private Dictionary<string, WIN32_FIND_DATA> MetadataFileDictionary { get; set; }
  159. /// <summary>
  160. /// Gets the metadata files.
  161. /// </summary>
  162. /// <value>The metadata files.</value>
  163. public IEnumerable<WIN32_FIND_DATA> MetadataFiles
  164. {
  165. get
  166. {
  167. if (MetadataFileDictionary != null)
  168. {
  169. return MetadataFileDictionary.Values;
  170. }
  171. return new WIN32_FIND_DATA[] {};
  172. }
  173. }
  174. /// <summary>
  175. /// Adds the metadata file.
  176. /// </summary>
  177. /// <param name="path">The path.</param>
  178. /// <exception cref="System.IO.FileNotFoundException"></exception>
  179. public void AddMetadataFile(string path)
  180. {
  181. var file = FileSystem.GetFileData(path);
  182. if (!file.HasValue)
  183. {
  184. throw new FileNotFoundException(path);
  185. }
  186. AddMetadataFile(file.Value);
  187. }
  188. /// <summary>
  189. /// Adds the metadata file.
  190. /// </summary>
  191. /// <param name="fileInfo">The file info.</param>
  192. public void AddMetadataFile(WIN32_FIND_DATA fileInfo)
  193. {
  194. AddMetadataFiles(new[] { fileInfo });
  195. }
  196. /// <summary>
  197. /// Adds the metadata files.
  198. /// </summary>
  199. /// <param name="files">The files.</param>
  200. /// <exception cref="System.ArgumentNullException"></exception>
  201. public void AddMetadataFiles(IEnumerable<WIN32_FIND_DATA> files)
  202. {
  203. if (files == null)
  204. {
  205. throw new ArgumentNullException();
  206. }
  207. if (MetadataFileDictionary == null)
  208. {
  209. MetadataFileDictionary = new Dictionary<string, WIN32_FIND_DATA>(StringComparer.OrdinalIgnoreCase);
  210. }
  211. foreach (var file in files)
  212. {
  213. MetadataFileDictionary[file.cFileName] = file;
  214. }
  215. }
  216. /// <summary>
  217. /// Gets the name of the file system entry by.
  218. /// </summary>
  219. /// <param name="name">The name.</param>
  220. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  221. /// <exception cref="System.ArgumentNullException"></exception>
  222. public WIN32_FIND_DATA? GetFileSystemEntryByName(string name)
  223. {
  224. if (string.IsNullOrEmpty(name))
  225. {
  226. throw new ArgumentNullException();
  227. }
  228. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  229. }
  230. /// <summary>
  231. /// Gets the file system entry by path.
  232. /// </summary>
  233. /// <param name="path">The path.</param>
  234. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  235. /// <exception cref="System.ArgumentNullException"></exception>
  236. public WIN32_FIND_DATA? GetFileSystemEntryByPath(string path)
  237. {
  238. if (string.IsNullOrEmpty(path))
  239. {
  240. throw new ArgumentNullException();
  241. }
  242. if (FileSystemDictionary != null)
  243. {
  244. WIN32_FIND_DATA entry;
  245. if (FileSystemDictionary.TryGetValue(path, out entry))
  246. {
  247. return entry;
  248. }
  249. }
  250. return null;
  251. }
  252. /// <summary>
  253. /// Gets the meta file by path.
  254. /// </summary>
  255. /// <param name="path">The path.</param>
  256. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  257. /// <exception cref="System.ArgumentNullException"></exception>
  258. public WIN32_FIND_DATA? GetMetaFileByPath(string path)
  259. {
  260. if (string.IsNullOrEmpty(path))
  261. {
  262. throw new ArgumentNullException();
  263. }
  264. if (MetadataFileDictionary != null)
  265. {
  266. WIN32_FIND_DATA entry;
  267. if (MetadataFileDictionary.TryGetValue(System.IO.Path.GetFileName(path), out entry))
  268. {
  269. return entry;
  270. }
  271. }
  272. return GetFileSystemEntryByPath(path);
  273. }
  274. /// <summary>
  275. /// Gets the name of the meta file by.
  276. /// </summary>
  277. /// <param name="name">The name.</param>
  278. /// <returns>System.Nullable{WIN32_FIND_DATA}.</returns>
  279. /// <exception cref="System.ArgumentNullException"></exception>
  280. public WIN32_FIND_DATA? GetMetaFileByName(string name)
  281. {
  282. if (string.IsNullOrEmpty(name))
  283. {
  284. throw new ArgumentNullException();
  285. }
  286. if (MetadataFileDictionary != null)
  287. {
  288. WIN32_FIND_DATA entry;
  289. if (MetadataFileDictionary.TryGetValue(name, out entry))
  290. {
  291. return entry;
  292. }
  293. }
  294. return GetFileSystemEntryByName(name);
  295. }
  296. /// <summary>
  297. /// Determines whether [contains meta file by name] [the specified name].
  298. /// </summary>
  299. /// <param name="name">The name.</param>
  300. /// <returns><c>true</c> if [contains meta file by name] [the specified name]; otherwise, <c>false</c>.</returns>
  301. public bool ContainsMetaFileByName(string name)
  302. {
  303. return GetMetaFileByName(name).HasValue;
  304. }
  305. /// <summary>
  306. /// Determines whether [contains file system entry by name] [the specified name].
  307. /// </summary>
  308. /// <param name="name">The name.</param>
  309. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  310. public bool ContainsFileSystemEntryByName(string name)
  311. {
  312. return GetFileSystemEntryByName(name).HasValue;
  313. }
  314. #region Equality Overrides
  315. /// <summary>
  316. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  317. /// </summary>
  318. /// <param name="obj">The object to compare with the current object.</param>
  319. /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  320. public override bool Equals(object obj)
  321. {
  322. return (Equals(obj as ItemResolveArgs));
  323. }
  324. /// <summary>
  325. /// Returns a hash code for this instance.
  326. /// </summary>
  327. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  328. public override int GetHashCode()
  329. {
  330. return Path.GetHashCode();
  331. }
  332. /// <summary>
  333. /// Equalses the specified args.
  334. /// </summary>
  335. /// <param name="args">The args.</param>
  336. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  337. protected bool Equals(ItemResolveArgs args)
  338. {
  339. if (args != null)
  340. {
  341. if (args.Path == null && Path == null) return true;
  342. return args.Path != null && args.Path.Equals(Path, StringComparison.OrdinalIgnoreCase);
  343. }
  344. return false;
  345. }
  346. #endregion
  347. }
  348. }