-
Notifications
You must be signed in to change notification settings - Fork 0
/
WatchFolder.cs
166 lines (135 loc) · 4.86 KB
/
WatchFolder.cs
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#region License Information (GPL v3)
/*
ShareX - A program that allows you to take screenshots and share any file type
Copyright (c) 2007-2019 ShareX Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Optionally you can also view the license at <http://www.gnu.org/licenses/>.
*/
#endregion License Information (GPL v3)
using ShareX.HelpersLib;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading;
namespace ShareX
{
public class WatchFolder : IDisposable
{
public WatchFolderSettings Settings { get; set; }
public TaskSettings TaskSettings { get; set; }
public delegate void FileWatcherTriggerEventHandler(string path);
public event FileWatcherTriggerEventHandler FileWatcherTrigger;
private SynchronizationContext context;
private FileSystemWatcher fileWatcher;
private List<WatchFolderDuplicateEventTimer> timers = new List<WatchFolderDuplicateEventTimer>();
public virtual void Enable()
{
Dispose();
string folderPath = Helpers.ExpandFolderVariables(Settings.FolderPath);
if (!string.IsNullOrEmpty(folderPath) && Directory.Exists(folderPath))
{
context = SynchronizationContext.Current ?? new SynchronizationContext();
fileWatcher = new FileSystemWatcher(folderPath);
if (!string.IsNullOrEmpty(Settings.Filter)) fileWatcher.Filter = Settings.Filter;
fileWatcher.IncludeSubdirectories = Settings.IncludeSubdirectories;
fileWatcher.Created += fileWatcher_Created;
fileWatcher.EnableRaisingEvents = true;
}
}
protected void OnFileWatcherTrigger(string path)
{
if (FileWatcherTrigger != null)
{
FileWatcherTrigger(path);
}
}
private async void fileWatcher_Created(object sender, FileSystemEventArgs e)
{
CleanElapsedTimers();
string path = e.FullPath;
foreach (WatchFolderDuplicateEventTimer timer in timers)
{
if (timer.IsDuplicateEvent(path))
{
return;
}
}
timers.Add(new WatchFolderDuplicateEventTimer(path));
int successCount = 0;
long previousSize = -1;
await Helpers.WaitWhileAsync(() =>
{
if (!Helpers.IsFileLocked(path))
{
long currentSize = Helpers.GetFileSize(path);
if (currentSize > 0 && currentSize == previousSize)
{
successCount++;
}
previousSize = currentSize;
return successCount < 4;
}
previousSize = -1;
return true;
}, 250, 5000, () =>
{
context.Post(state => OnFileWatcherTrigger(path), null);
}, 1000);
}
protected void CleanElapsedTimers()
{
for (int i = 0; i < timers.Count; i++)
{
if (timers[i].IsElapsed)
{
timers.Remove(timers[i]);
}
}
}
public void Dispose()
{
if (fileWatcher != null)
{
fileWatcher.Dispose();
}
}
}
public class WatchFolderDuplicateEventTimer
{
private const int expireTime = 1000;
private Stopwatch timer;
private string path;
public bool IsElapsed
{
get
{
return timer.ElapsedMilliseconds >= expireTime;
}
}
public WatchFolderDuplicateEventTimer(string path)
{
timer = Stopwatch.StartNew();
this.path = path;
}
public bool IsDuplicateEvent(string path)
{
bool result = path == this.path && !IsElapsed;
if (result)
{
timer = Stopwatch.StartNew();
}
return result;
}
}
}