ItemResolveEventArgs.cs 4.2 KB

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