Repository navigation
Expand file tree
/
Copy pathProgram.cs
More file actions
335 lines (302 loc) · 11.4 KB
/
Copy pathProgram.cs
File metadata and controls
335 lines (302 loc) · 11.4 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using Microsoft.Extensions.Configuration;
using Lucene.Net.Analysis.Standard;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.QueryParsers.Classic;
using Lucene.Net.Search;
using Lucene.Net.Store;
using Lucene.Net.Util;
using LuceneVersion = Lucene.Net.Util.LuceneVersion;
using Document = Lucene.Net.Documents.Document;
using System.Linq;
using LuceneDirectory = Lucene.Net.Store.Directory;
class Program
{
private static readonly string _indexPath = Path.Combine(Environment.CurrentDirectory, "LuceneIndex");
private static LuceneDirectory? _directory;
private static IndexWriter? _writer;
private static readonly List<string> _monitoredFolders = new();
private static bool _isRunning = true;
private const LuceneVersion AppLuceneVersion = LuceneVersion.LUCENE_48;
static void Main(string[] args)
{
Console.WriteLine("Document Monitor and Search Application");
Console.WriteLine("------------------------------------");
// Initialize Lucene
InitializeLucene();
// Load configuration
var configuration = new ConfigurationBuilder()
.SetBasePath(System.IO.Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.Build();
// Get monitored folders from configuration or use default
var folders = configuration.GetSection("MonitoredFolders").GetChildren()
.Where(x => !string.IsNullOrEmpty(x.Value))
.Select(x => x.Value!)
.ToList();
if (folders.Count == 0)
{
folders = new List<string> { System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "Monitor") };
}
_monitoredFolders.AddRange(folders);
// Ensure the monitored folders exist
foreach (var folder in _monitoredFolders)
{
if (!System.IO.Directory.Exists(folder))
{
System.IO.Directory.CreateDirectory(folder);
Console.WriteLine($"Created monitored folder: {folder}");
}
}
Console.WriteLine("\nMonitored folders:");
foreach (var folder in _monitoredFolders)
{
Console.WriteLine($"- {folder}");
}
// Start monitoring
Console.WriteLine("\nStarting file system watchers...");
var watchers = new List<FileSystemWatcher>();
foreach (var folder in _monitoredFolders)
{
var watcher = new FileSystemWatcher(folder);
watcher.Created += OnFileCreated;
watcher.Changed += OnFileChanged;
watcher.EnableRaisingEvents = true;
watchers.Add(watcher);
Console.WriteLine($"Watching: {folder}");
}
// Initial index of existing files
Console.WriteLine("\nIndexing existing files...");
foreach (var folder in _monitoredFolders)
{
IndexExistingFiles(folder);
}
Console.WriteLine("\nDocument Monitor is running. Press 'S' to search, 'Q' to quit.");
// Main loop
while (_isRunning)
{
// Check for user input
try
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true).Key;
if (key == ConsoleKey.S)
{
Console.Write("\nEnter search query: ");
var query = Console.ReadLine();
if (!string.IsNullOrWhiteSpace(query))
{
SearchDocuments(query);
Console.WriteLine("\nPress any key to continue...");
Console.ReadKey(true);
}
}
else if (key == ConsoleKey.Q)
{
_isRunning = false;
}
}
}
catch (InvalidOperationException)
{
// Console input not available, wait a bit and continue
Thread.Sleep(1000);
}
Thread.Sleep(100);
}
// Cleanup
foreach (var watcher in watchers)
{
watcher.Dispose();
}
_writer?.Dispose();
_directory?.Dispose();
Console.WriteLine("\nDocument Monitor has been stopped.");
}
private static void InitializeLucene()
{
// Create Lucene index directory if it doesn't exist
if (!System.IO.Directory.Exists(_indexPath))
{
System.IO.Directory.CreateDirectory(_indexPath);
}
var dirInfo = new System.IO.DirectoryInfo(_indexPath);
_directory = Lucene.Net.Store.FSDirectory.Open(dirInfo);
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var config = new IndexWriterConfig(AppLuceneVersion, analyzer)
{
OpenMode = OpenMode.CREATE_OR_APPEND
};
_writer = new IndexWriter(_directory, config);
}
private static void IndexExistingFiles(string folderPath)
{
try
{
var extensions = new[] { ".docx", ".pptx", ".xlsx", ".pdf", ".md", ".txt", ".eml" };
foreach (var file in System.IO.Directory.GetFiles(folderPath, "*.*", SearchOption.AllDirectories))
{
if (extensions.Contains(System.IO.Path.GetExtension(file).ToLower()))
{
IndexFile(file);
}
}
_writer?.Commit();
}
catch (Exception ex)
{
Console.WriteLine($"Error indexing files: {ex.Message}");
}
}
private static void IndexFile(string filePath)
{
try
{
var fileInfo = new FileInfo(filePath);
var extension = Path.GetExtension(filePath).ToLower();
string content = string.Empty;
// Extract text based on file type
switch (extension)
{
case ".txt":
case ".md":
content = File.ReadAllText(filePath);
break;
case ".pdf":
content = PdfTextExtractor.ExtractText(filePath);
break;
case ".docx":
case ".pptx":
case ".xlsx":
content = OfficeTextExtractor.ExtractText(filePath);
break;
case ".eml":
content = EmailTextExtractor.ExtractText(filePath);
break;
}
// Create or update document in index
var searchQuery = new TermQuery(new Term("path", filePath));
_writer?.DeleteDocuments(searchQuery);
var doc = new Document
{
new TextField("content", ExtractTextFromFile(filePath), Field.Store.YES),
new StringField("path", filePath, Field.Store.YES),
new StringField("filename", fileInfo.Name, Field.Store.YES),
new StringField("extension", fileInfo.Extension.ToLower(), Field.Store.YES),
new StringField("modified",
DateTools.TimeToString(
(long)(fileInfo.LastWriteTimeUtc - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds,
DateResolution.MILLISECOND),
Field.Store.YES)
};
_writer?.AddDocument(doc);
_writer?.Commit();
Console.WriteLine($"Indexed: {filePath}");
}
catch (Exception ex)
{
Console.WriteLine($"Error indexing {filePath}: {ex.Message}");
}
}
private static string ExtractTextFromFile(string filePath)
{
var fileInfo = new FileInfo(filePath);
var extension = Path.GetExtension(filePath).ToLower();
string content = string.Empty;
// Extract text based on file type
try
{
switch (extension)
{
case ".txt":
case ".md":
content = File.ReadAllText(filePath);
break;
case ".pdf":
content = PdfTextExtractor.ExtractText(filePath);
break;
case ".docx":
case ".pptx":
case ".xlsx":
case ".doc":
case ".xls":
case ".ppt":
content = OfficeTextExtractor.ExtractText(filePath);
break;
case ".eml":
content = EmailTextExtractor.ExtractText(filePath);
break;
}
}
catch (Exception ex)
{
Console.WriteLine($"Error extracting text from {filePath}: {ex.Message}");
}
return content ?? string.Empty;
}
private static void SearchDocuments(string queryText)
{
if (_writer == null)
{
Console.WriteLine("Error: Index writer is not initialized.");
return;
}
try
{
using (var reader = _writer.GetReader(false))
{
var searcher = new IndexSearcher(reader);
var analyzer = new StandardAnalyzer(AppLuceneVersion);
var parser = new MultiFieldQueryParser(
AppLuceneVersion,
new[] { "filename", "content" },
analyzer);
var query = parser.Parse(queryText);
var hits = Array.Empty<ScoreDoc>();
if (query != null && searcher != null)
{
var searchResults = searcher.Search(query, null, 20, Sort.RELEVANCE);
hits = searchResults?.ScoreDocs ?? Array.Empty<ScoreDoc>();
}
Console.WriteLine($"\nFound {hits.Length} results:");
if (searcher != null)
{
foreach (var hit in hits)
{
var foundDoc = searcher.Doc(hit.Doc);
if (foundDoc != null)
{
var modifiedDate = foundDoc.Get("modified");
var modified = !string.IsNullOrEmpty(modifiedDate) ? DateTools.StringToDate(modifiedDate).ToString() : "Unknown";
var filename = foundDoc.Get("filename") ?? "Unknown";
Console.WriteLine($"- {filename} (Modified: {modified}) - Score: {hit.Score:F2}");
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine($"Error searching: {ex.Message}");
}
}
private static void OnFileCreated(object sender, FileSystemEventArgs e)
{
if (e.ChangeType == WatcherChangeTypes.Created || e.ChangeType == WatcherChangeTypes.Changed)
{
IndexFile(e.FullPath);
_writer?.Commit();
}
}
private static void OnFileChanged(object sender, FileSystemEventArgs e)
{
// Small delay to ensure the file is no longer locked
Thread.Sleep(500);
OnFileCreated(sender, e);
}
}