2
0

ItemResolveArgs.cs 14 KB

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