ItemResolveArgs.cs 13 KB

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