Folder.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. using Newtonsoft.Json;
  8. namespace MediaBrowser.Model.Entities
  9. {
  10. public class Folder : BaseItem
  11. {
  12. public bool IsRoot { get; set; }
  13. public bool IsVirtualFolder
  14. {
  15. get
  16. {
  17. return Parent != null && Parent.IsRoot;
  18. }
  19. }
  20. [JsonIgnore]
  21. public BaseItem[] Children { get; set; }
  22. [JsonIgnore]
  23. public IEnumerable<Folder> FolderChildren { get { return Children.OfType<Folder>(); } }
  24. public Folder GetFolderByName(string name)
  25. {
  26. return FolderChildren.FirstOrDefault(f => System.IO.Path.GetFileName(f.Path).Equals(name, StringComparison.OrdinalIgnoreCase));
  27. }
  28. /// <summary>
  29. /// Finds an item by ID, recursively
  30. /// </summary>
  31. public BaseItem FindById(Guid id)
  32. {
  33. if (Id == id)
  34. {
  35. return this;
  36. }
  37. foreach (BaseItem item in Children)
  38. {
  39. if (item.Id == id)
  40. {
  41. return item;
  42. }
  43. }
  44. foreach (Folder folder in FolderChildren)
  45. {
  46. BaseItem item = folder.FindById(id);
  47. if (item != null)
  48. {
  49. return item;
  50. }
  51. }
  52. return null;
  53. }
  54. /// <summary>
  55. /// Finds an item by path, recursively
  56. /// </summary>
  57. public BaseItem FindByPath(string path)
  58. {
  59. if (Path.Equals(path, StringComparison.OrdinalIgnoreCase))
  60. {
  61. return this;
  62. }
  63. foreach (BaseItem item in Children)
  64. {
  65. if (item.Path.Equals(path, StringComparison.OrdinalIgnoreCase))
  66. {
  67. return item;
  68. }
  69. }
  70. foreach (Folder folder in FolderChildren)
  71. {
  72. BaseItem item = folder.FindByPath(path);
  73. if (item != null)
  74. {
  75. return item;
  76. }
  77. }
  78. return null;
  79. }
  80. }
  81. }