DirectoryWatchers.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using MediaBrowser.Controller.Entities;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Common.Extensions;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Threading;
  9. using System.Threading.Tasks;
  10. namespace MediaBrowser.Controller.IO
  11. {
  12. public class DirectoryWatchers
  13. {
  14. private readonly List<FileSystemWatcher> FileSystemWatchers = new List<FileSystemWatcher>();
  15. private Timer updateTimer;
  16. private List<string> affectedPaths = new List<string>();
  17. private const int TimerDelayInSeconds = 5;
  18. public void Start()
  19. {
  20. var pathsToWatch = new List<string>();
  21. var rootFolder = Kernel.Instance.RootFolder;
  22. pathsToWatch.Add(rootFolder.Path);
  23. foreach (Folder folder in rootFolder.Children.OfType<Folder>())
  24. {
  25. foreach (string path in folder.PhysicalLocations)
  26. {
  27. if (Path.IsPathRooted(path) && !pathsToWatch.ContainsStartsWith(path))
  28. {
  29. pathsToWatch.Add(path);
  30. }
  31. }
  32. }
  33. foreach (string path in pathsToWatch)
  34. {
  35. Logger.LogInfo("Watching directory " + path + " for changes.");
  36. var watcher = new FileSystemWatcher(path, "*") { };
  37. watcher.IncludeSubdirectories = true;
  38. watcher.Changed += watcher_Changed;
  39. // All the others seem to trigger change events on the parent, so let's keep it simple for now.
  40. //watcher.Created += watcher_Changed;
  41. //watcher.Deleted += watcher_Changed;
  42. //watcher.Renamed += watcher_Changed;
  43. watcher.EnableRaisingEvents = true;
  44. FileSystemWatchers.Add(watcher);
  45. }
  46. }
  47. void watcher_Changed(object sender, FileSystemEventArgs e)
  48. {
  49. if (!affectedPaths.Contains(e.FullPath))
  50. {
  51. affectedPaths.Add(e.FullPath);
  52. }
  53. if (updateTimer == null)
  54. {
  55. updateTimer = new Timer(TimerStopped, null, TimeSpan.FromSeconds(TimerDelayInSeconds), TimeSpan.FromMilliseconds(-1));
  56. }
  57. else
  58. {
  59. updateTimer.Change(TimeSpan.FromSeconds(TimerDelayInSeconds), TimeSpan.FromMilliseconds(-1));
  60. }
  61. }
  62. private async void TimerStopped(object stateInfo)
  63. {
  64. updateTimer.Dispose();
  65. updateTimer = null;
  66. List<string> paths = affectedPaths;
  67. affectedPaths = new List<string>();
  68. await ProcessPathChanges(paths).ConfigureAwait(false);
  69. }
  70. private Task ProcessPathChanges(IEnumerable<string> paths)
  71. {
  72. var itemsToRefresh = new List<BaseItem>();
  73. foreach (BaseItem item in paths.Select(p => GetAffectedBaseItem(p)))
  74. {
  75. if (item != null && !itemsToRefresh.Contains(item))
  76. {
  77. itemsToRefresh.Add(item);
  78. }
  79. }
  80. if (itemsToRefresh.Any(i =>
  81. {
  82. var folder = i as Folder;
  83. return folder != null && folder.IsRoot;
  84. }))
  85. {
  86. return Kernel.Instance.ReloadRoot();
  87. }
  88. return Task.WhenAll(itemsToRefresh.Select(i => Kernel.Instance.ReloadItem(i)));
  89. }
  90. private BaseItem GetAffectedBaseItem(string path)
  91. {
  92. BaseItem item = null;
  93. while (item == null)
  94. {
  95. item = Kernel.Instance.RootFolder.FindByPath(path);
  96. path = Path.GetDirectoryName(path);
  97. }
  98. return item;
  99. }
  100. public void Stop()
  101. {
  102. foreach (FileSystemWatcher watcher in FileSystemWatchers)
  103. {
  104. watcher.Changed -= watcher_Changed;
  105. watcher.EnableRaisingEvents = false;
  106. watcher.Dispose();
  107. }
  108. if (updateTimer != null)
  109. {
  110. updateTimer.Dispose();
  111. updateTimer = null;
  112. }
  113. FileSystemWatchers.Clear();
  114. affectedPaths.Clear();
  115. }
  116. }
  117. }