aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Controller/IO/DirectoryWatchers.cs
blob: 10d11385ee83027728e91d159ef6086aa7303bc1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using MediaBrowser.Model.Entities;

namespace MediaBrowser.Controller.IO
{
    public class DirectoryWatchers
    {
        private List<FileSystemWatcher> FileSystemWatchers = new List<FileSystemWatcher>();
        private Timer updateTimer = null;
        private List<string> affectedPaths = new List<string>();

        private const int TimerDelayInSeconds = 5;

        public void Start()
        {
            List<string> pathsToWatch = new List<string>();

            var rootFolder = Kernel.Instance.RootFolder;

            pathsToWatch.Add(rootFolder.Path);

            foreach (Folder folder in rootFolder.Children.OfType<Folder>())
            {
                foreach (Folder subFolder in folder.Children.OfType<Folder>())
                {
                    if (Path.IsPathRooted(subFolder.Path))
                    {
                        string parent = Path.GetDirectoryName(subFolder.Path);

                        if (!pathsToWatch.Contains(parent))
                        {
                            pathsToWatch.Add(parent);
                        }
                    }
                }
            }

            foreach (string path in pathsToWatch)
            {
                FileSystemWatcher watcher = new FileSystemWatcher(path, "*");

                watcher.IncludeSubdirectories = true;

                watcher.Changed += watcher_Changed;

                // All the others seem to trigger change events on the parent, so let's keep it simple for now.
                //watcher.Created += watcher_Changed;
                //watcher.Deleted += watcher_Changed;
                //watcher.Renamed += watcher_Changed;

                watcher.EnableRaisingEvents = true;
                FileSystemWatchers.Add(watcher);
            }
        }

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            if (!affectedPaths.Contains(e.FullPath))
            {
                affectedPaths.Add(e.FullPath);
            }

            if (updateTimer == null)
            {
                updateTimer = new Timer(TimerStopped, null, TimeSpan.FromSeconds(TimerDelayInSeconds), TimeSpan.FromMilliseconds(-1));
            }
            else
            {
                updateTimer.Change(TimeSpan.FromSeconds(TimerDelayInSeconds), TimeSpan.FromMilliseconds(-1));
            }
        }

        private async void TimerStopped(object stateInfo)
        {
            updateTimer.Dispose();
            updateTimer = null;

            List<string> paths = affectedPaths;
            affectedPaths = new List<string>();

            await ProcessPathChanges(paths).ConfigureAwait(false);
        }

        private Task ProcessPathChanges(IEnumerable<string> paths)
        {
            List<BaseItem> itemsToRefresh = new List<BaseItem>();

            foreach (BaseItem item in paths.Select(p => GetAffectedBaseItem(p)))
            {
                if (item != null && !itemsToRefresh.Contains(item))
                {
                    itemsToRefresh.Add(item);
                }
            }

            if (itemsToRefresh.Any(i =>
                {
                    var folder = i as Folder;

                    return folder != null && folder.IsRoot;
                }))
            {
                return Kernel.Instance.ReloadRoot();
            }
            else
            {
                return Task.WhenAll(itemsToRefresh.Select(i => Kernel.Instance.ReloadItem(i)));
            }
        }

        private BaseItem GetAffectedBaseItem(string path)
        {
            BaseItem item = null;

            while (item == null)
            {
                item = Kernel.Instance.RootFolder.FindByPath(path);

                path = Path.GetDirectoryName(path);
            }

            return item;
        }

        public void Stop()
        {
            foreach (FileSystemWatcher watcher in FileSystemWatchers)
            {
                watcher.Changed -= watcher_Changed;
                watcher.EnableRaisingEvents = false;
                watcher.Dispose();
            }

            if (updateTimer != null)
            {
                updateTimer.Dispose();
                updateTimer = null;
            }

            FileSystemWatchers.Clear();
            affectedPaths.Clear();
        }
    }
}