ItemResolveArgs.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  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 { return FileSystemDictionary.Values; }
  36. }
  37. /// <summary>
  38. /// Gets or sets the file system dictionary.
  39. /// </summary>
  40. /// <value>The file system dictionary.</value>
  41. public Dictionary<string, FileSystemInfo> FileSystemDictionary { get; set; }
  42. /// <summary>
  43. /// Gets or sets the parent.
  44. /// </summary>
  45. /// <value>The parent.</value>
  46. public Folder Parent { get; set; }
  47. /// <summary>
  48. /// Gets or sets the file info.
  49. /// </summary>
  50. /// <value>The file info.</value>
  51. public FileSystemInfo FileInfo { get; set; }
  52. /// <summary>
  53. /// Gets or sets the path.
  54. /// </summary>
  55. /// <value>The path.</value>
  56. public string Path { get; set; }
  57. /// <summary>
  58. /// Gets a value indicating whether this instance is directory.
  59. /// </summary>
  60. /// <value><c>true</c> if this instance is directory; otherwise, <c>false</c>.</value>
  61. public bool IsDirectory
  62. {
  63. get
  64. {
  65. return (FileInfo.Attributes & FileAttributes.Directory) == FileAttributes.Directory;
  66. }
  67. }
  68. /// <summary>
  69. /// Gets a value indicating whether this instance is hidden.
  70. /// </summary>
  71. /// <value><c>true</c> if this instance is hidden; otherwise, <c>false</c>.</value>
  72. public bool IsHidden
  73. {
  74. get
  75. {
  76. return (FileInfo.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden;
  77. }
  78. }
  79. /// <summary>
  80. /// Gets a value indicating whether this instance is system file.
  81. /// </summary>
  82. /// <value><c>true</c> if this instance is system file; otherwise, <c>false</c>.</value>
  83. public bool IsSystemFile
  84. {
  85. get
  86. {
  87. return (FileInfo.Attributes & FileAttributes.System) == FileAttributes.System;
  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 a value indicating whether this instance is root.
  123. /// </summary>
  124. /// <value><c>true</c> if this instance is root; otherwise, <c>false</c>.</value>
  125. public bool IsRoot
  126. {
  127. get
  128. {
  129. return Parent == null;
  130. }
  131. }
  132. /// <summary>
  133. /// Gets or sets the additional locations.
  134. /// </summary>
  135. /// <value>The additional locations.</value>
  136. private List<string> AdditionalLocations { get; set; }
  137. /// <summary>
  138. /// Adds the additional location.
  139. /// </summary>
  140. /// <param name="path">The path.</param>
  141. /// <exception cref="System.ArgumentNullException"></exception>
  142. public void AddAdditionalLocation(string path)
  143. {
  144. if (string.IsNullOrEmpty(path))
  145. {
  146. throw new ArgumentNullException();
  147. }
  148. if (AdditionalLocations == null)
  149. {
  150. AdditionalLocations = new List<string>();
  151. }
  152. AdditionalLocations.Add(path);
  153. }
  154. /// <summary>
  155. /// Gets the physical locations.
  156. /// </summary>
  157. /// <value>The physical locations.</value>
  158. public IEnumerable<string> PhysicalLocations
  159. {
  160. get
  161. {
  162. var paths = string.IsNullOrWhiteSpace(Path) ? new string[] {} : new[] {Path};
  163. return AdditionalLocations == null ? paths : paths.Concat(AdditionalLocations);
  164. }
  165. }
  166. /// <summary>
  167. /// Store these to reduce disk access in Resolvers
  168. /// </summary>
  169. /// <value>The metadata file dictionary.</value>
  170. private Dictionary<string, FileSystemInfo> MetadataFileDictionary { get; set; }
  171. /// <summary>
  172. /// Gets the metadata files.
  173. /// </summary>
  174. /// <value>The metadata files.</value>
  175. public IEnumerable<FileSystemInfo> MetadataFiles
  176. {
  177. get
  178. {
  179. if (MetadataFileDictionary != null)
  180. {
  181. return MetadataFileDictionary.Values;
  182. }
  183. return new FileSystemInfo[] { };
  184. }
  185. }
  186. /// <summary>
  187. /// Adds the metadata file.
  188. /// </summary>
  189. /// <param name="path">The path.</param>
  190. /// <exception cref="System.IO.FileNotFoundException"></exception>
  191. public void AddMetadataFile(string path)
  192. {
  193. var file = new FileInfo(path);
  194. if (!file.Exists)
  195. {
  196. throw new FileNotFoundException(path);
  197. }
  198. AddMetadataFile(file);
  199. }
  200. /// <summary>
  201. /// Adds the metadata file.
  202. /// </summary>
  203. /// <param name="fileInfo">The file info.</param>
  204. public void AddMetadataFile(FileSystemInfo fileInfo)
  205. {
  206. AddMetadataFiles(new[] { fileInfo });
  207. }
  208. /// <summary>
  209. /// Adds the metadata files.
  210. /// </summary>
  211. /// <param name="files">The files.</param>
  212. /// <exception cref="System.ArgumentNullException"></exception>
  213. public void AddMetadataFiles(IEnumerable<FileSystemInfo> files)
  214. {
  215. if (files == null)
  216. {
  217. throw new ArgumentNullException();
  218. }
  219. if (MetadataFileDictionary == null)
  220. {
  221. MetadataFileDictionary = new Dictionary<string, FileSystemInfo>(StringComparer.OrdinalIgnoreCase);
  222. }
  223. foreach (var file in files)
  224. {
  225. MetadataFileDictionary[file.Name] = file;
  226. }
  227. }
  228. /// <summary>
  229. /// Gets the name of the file system entry by.
  230. /// </summary>
  231. /// <param name="name">The name.</param>
  232. /// <returns>FileSystemInfo.</returns>
  233. /// <exception cref="System.ArgumentNullException"></exception>
  234. public FileSystemInfo GetFileSystemEntryByName(string name)
  235. {
  236. if (string.IsNullOrEmpty(name))
  237. {
  238. throw new ArgumentNullException();
  239. }
  240. return GetFileSystemEntryByPath(System.IO.Path.Combine(Path, name));
  241. }
  242. /// <summary>
  243. /// Gets the file system entry by path.
  244. /// </summary>
  245. /// <param name="path">The path.</param>
  246. /// <returns>FileSystemInfo.</returns>
  247. /// <exception cref="System.ArgumentNullException"></exception>
  248. public FileSystemInfo GetFileSystemEntryByPath(string path)
  249. {
  250. if (string.IsNullOrEmpty(path))
  251. {
  252. throw new ArgumentNullException();
  253. }
  254. if (FileSystemDictionary != null)
  255. {
  256. FileSystemInfo entry;
  257. if (FileSystemDictionary.TryGetValue(path, out entry))
  258. {
  259. return entry;
  260. }
  261. }
  262. return null;
  263. }
  264. /// <summary>
  265. /// Gets the meta file by path.
  266. /// </summary>
  267. /// <param name="path">The path.</param>
  268. /// <returns>FileSystemInfo.</returns>
  269. /// <exception cref="System.ArgumentNullException"></exception>
  270. public FileSystemInfo GetMetaFileByPath(string path)
  271. {
  272. if (string.IsNullOrEmpty(path))
  273. {
  274. throw new ArgumentNullException();
  275. }
  276. if (MetadataFileDictionary != null)
  277. {
  278. FileSystemInfo entry;
  279. if (MetadataFileDictionary.TryGetValue(System.IO.Path.GetFileName(path), out entry))
  280. {
  281. return entry;
  282. }
  283. }
  284. return GetFileSystemEntryByPath(path);
  285. }
  286. /// <summary>
  287. /// Gets the name of the meta file by.
  288. /// </summary>
  289. /// <param name="name">The name.</param>
  290. /// <returns>FileSystemInfo.</returns>
  291. /// <exception cref="System.ArgumentNullException"></exception>
  292. public FileSystemInfo GetMetaFileByName(string name)
  293. {
  294. if (string.IsNullOrEmpty(name))
  295. {
  296. throw new ArgumentNullException();
  297. }
  298. if (MetadataFileDictionary != null)
  299. {
  300. FileSystemInfo entry;
  301. if (MetadataFileDictionary.TryGetValue(name, out entry))
  302. {
  303. return entry;
  304. }
  305. }
  306. return GetFileSystemEntryByName(name);
  307. }
  308. /// <summary>
  309. /// Determines whether [contains meta file by name] [the specified name].
  310. /// </summary>
  311. /// <param name="name">The name.</param>
  312. /// <returns><c>true</c> if [contains meta file by name] [the specified name]; otherwise, <c>false</c>.</returns>
  313. public bool ContainsMetaFileByName(string name)
  314. {
  315. return GetMetaFileByName(name) != null;
  316. }
  317. /// <summary>
  318. /// Determines whether [contains file system entry by name] [the specified name].
  319. /// </summary>
  320. /// <param name="name">The name.</param>
  321. /// <returns><c>true</c> if [contains file system entry by name] [the specified name]; otherwise, <c>false</c>.</returns>
  322. public bool ContainsFileSystemEntryByName(string name)
  323. {
  324. return GetFileSystemEntryByName(name) != null;
  325. }
  326. private bool _collectionTypeDiscovered;
  327. private string _collectionType;
  328. public string GetCollectionType()
  329. {
  330. if (!_collectionTypeDiscovered)
  331. {
  332. _collectionType = Parent == null ? null : _libraryManager.FindCollectionType(Parent);
  333. _collectionTypeDiscovered = true;
  334. }
  335. return _collectionType;
  336. }
  337. #region Equality Overrides
  338. /// <summary>
  339. /// Determines whether the specified <see cref="System.Object" /> is equal to this instance.
  340. /// </summary>
  341. /// <param name="obj">The object to compare with the current object.</param>
  342. /// <returns><c>true</c> if the specified <see cref="System.Object" /> is equal to this instance; otherwise, <c>false</c>.</returns>
  343. public override bool Equals(object obj)
  344. {
  345. return (Equals(obj as ItemResolveArgs));
  346. }
  347. /// <summary>
  348. /// Returns a hash code for this instance.
  349. /// </summary>
  350. /// <returns>A hash code for this instance, suitable for use in hashing algorithms and data structures like a hash table.</returns>
  351. public override int GetHashCode()
  352. {
  353. return Path.GetHashCode();
  354. }
  355. /// <summary>
  356. /// Equalses the specified args.
  357. /// </summary>
  358. /// <param name="args">The args.</param>
  359. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  360. protected bool Equals(ItemResolveArgs args)
  361. {
  362. if (args != null)
  363. {
  364. if (args.Path == null && Path == null) return true;
  365. return args.Path != null && args.Path.Equals(Path, StringComparison.OrdinalIgnoreCase);
  366. }
  367. return false;
  368. }
  369. #endregion
  370. }
  371. }