ItemResolveEventArgs.cs 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using MediaBrowser.Model.Entities;
  5. using MediaBrowser.Controller.IO;
  6. namespace MediaBrowser.Controller.Events
  7. {
  8. /// <summary>
  9. /// This is an EventArgs object used when resolving a Path into a BaseItem
  10. /// </summary>
  11. public class ItemResolveEventArgs : PreBeginResolveEventArgs
  12. {
  13. public KeyValuePair<string, WIN32_FIND_DATA>[] FileSystemChildren { get; set; }
  14. public KeyValuePair<string, WIN32_FIND_DATA>? GetFileSystemEntry(string path, bool? isFolder)
  15. {
  16. foreach (KeyValuePair<string, WIN32_FIND_DATA> entry in FileSystemChildren)
  17. {
  18. if (isFolder.HasValue)
  19. {
  20. if (isFolder.Value && entry.Value.IsDirectory)
  21. {
  22. continue;
  23. }
  24. else if (!isFolder.Value && !entry.Value.IsDirectory)
  25. {
  26. continue;
  27. }
  28. }
  29. if (entry.Key.Equals(path, StringComparison.OrdinalIgnoreCase))
  30. {
  31. return entry;
  32. }
  33. }
  34. return null;
  35. }
  36. public KeyValuePair<string, WIN32_FIND_DATA>? GetFileSystemEntryByName(string name, bool? isFolder)
  37. {
  38. foreach (KeyValuePair<string, WIN32_FIND_DATA> entry in FileSystemChildren)
  39. {
  40. if (isFolder.HasValue)
  41. {
  42. if (isFolder.Value && entry.Value.IsDirectory)
  43. {
  44. continue;
  45. }
  46. else if (!isFolder.Value && !entry.Value.IsDirectory)
  47. {
  48. continue;
  49. }
  50. }
  51. if (System.IO.Path.GetFileName(entry.Key).Equals(name, StringComparison.OrdinalIgnoreCase))
  52. {
  53. return entry;
  54. }
  55. }
  56. return null;
  57. }
  58. public bool ContainsFile(string name)
  59. {
  60. return GetFileSystemEntryByName(name, false) != null;
  61. }
  62. public bool ContainsFolder(string name)
  63. {
  64. return GetFileSystemEntryByName(name, true) != null;
  65. }
  66. }
  67. /// <summary>
  68. /// This is an EventArgs object used before we begin resolving a Path into a BaseItem
  69. /// File system children have not been collected yet, but consuming events will
  70. /// have a chance to cancel resolution based on the Path, Parent and FileAttributes
  71. /// </summary>
  72. public class PreBeginResolveEventArgs : EventArgs
  73. {
  74. public string Path { get; set; }
  75. public Folder Parent { get; set; }
  76. public bool Cancel { get; set; }
  77. public FileAttributes FileAttributes { get { return FileData.dwFileAttributes; } }
  78. public WIN32_FIND_DATA FileData { get; set; }
  79. public bool IsFolder
  80. {
  81. get
  82. {
  83. return FileAttributes.HasFlag(FileAttributes.Directory);
  84. }
  85. }
  86. public bool IsHidden
  87. {
  88. get
  89. {
  90. return FileAttributes.HasFlag(FileAttributes.Hidden);
  91. }
  92. }
  93. public bool IsSystemFile
  94. {
  95. get
  96. {
  97. return FileAttributes.HasFlag(FileAttributes.System);
  98. }
  99. }
  100. public VirtualFolder VirtualFolder
  101. {
  102. get
  103. {
  104. if (Parent != null)
  105. {
  106. return Parent.VirtualFolder;
  107. }
  108. return null;
  109. }
  110. }
  111. public string VirtualFolderCollectionType
  112. {
  113. get
  114. {
  115. VirtualFolder vf = VirtualFolder;
  116. if (vf == null)
  117. {
  118. return null;
  119. }
  120. return vf.CollectionType;
  121. }
  122. }
  123. }
  124. }