ItemResolveArgs.cs 13 KB

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