-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
783 lines (720 loc) · 22.8 KB
/
Program.cs
File metadata and controls
783 lines (720 loc) · 22.8 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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
using MimeKit;
using Org.BouncyCastle.Asn1.X509;
using Org.BouncyCastle.Tls.Crypto;
using System;
using System.Data.SQLite;
using System.IO;
using System.Net.Mail;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml.Linq;
using static System.Net.Mime.MediaTypeNames;
#region Prompts & text
string _promptSourceFolder = "Enter source folder:";
string _promptSourceFolderNotFound = "Source folder not found, enter valid source folder:";
string _promptFolderNotFound = "Folder not found, enter valid path:";
string _promptDestinationFolder = "Enter destination folder:";
string _promptDestionationFolderNotFound = "Destination folder not found, enter valid source folder:";
string _titleStartExtentions = "Processing files to set extensions";
string _promptExtension = "Enter extension for files:";
string _promptExtensionIsEmpty = "Please give an extension";
string _promptDatabaseFolder = "Enter path for database file:";
string _promptDatabaseName = "Enter name for database file:";
string getDatabaseFolder()
{
Console.WriteLine();
Console.WriteLine(_promptDatabaseFolder);
string? _target = Console.ReadLine();
while (!System.IO.Directory.Exists(_target))
{
Console.WriteLine(_promptFolderNotFound);
_target = getDatabaseFolder();
}
return _target;
}
string getDatabaseName()
{
Console.WriteLine();
Console.WriteLine(_promptDatabaseName);
string? _target = Console.ReadLine();
while (_target == null || _target.Length<=0)
{
Console.WriteLine(_promptDatabaseName);
_target = getDatabaseName();
}
return _target;
}
string getSourceFolder()
{
Console.WriteLine();
Console.WriteLine(_promptSourceFolder);
string? _target = Console.ReadLine();
while (!System.IO.Directory.Exists(_target))
{
Console.WriteLine(_promptSourceFolderNotFound);
_target = getSourceFolder();
}
return _target;
}
string getDestinationFolder()
{
Console.WriteLine();
Console.Write(_promptDestinationFolder);
string? _destination = Console.ReadLine();
while (!System.IO.Directory.Exists(_destination))
{
Console.WriteLine(_promptDestionationFolderNotFound);
_destination = getDestinationFolder();
}
return _destination;
}
string getExtension()
{
Console.WriteLine();
Console.WriteLine(_promptExtension);
string? _extension = Console.ReadLine();
while (_extension == null || _extension?.Length <= 0)
{
Console.WriteLine(_promptExtensionIsEmpty);
_extension = getExtension();
}
return _extension ?? "";
}
#endregion
#region Menu
async void showMenu()
{
Console.WriteLine("Set [D]atabase file");
Console.WriteLine("[Q]uery database");
Console.WriteLine("[E]mail");
Console.WriteLine("m[B]oxes");
Console.WriteLine("[M]ove to flat folder structure");
Console.WriteLine("Exten[S]ion[S]");
Console.WriteLine("e[X]it");
ConsoleKeyInfo _input = Console.ReadKey();
switch (_input.Key.ToString().ToUpper())
{
case "Q":
ShowSqlEmails();
showMenu();
break;
case "D":
StartCreateDatabase();
showMenu();
break;
case "E": // E:\Email\Test
StartEmail(true);
showMenu();
break;
case "M":
StartMove();
showMenu();
break;
case "S":
StartExtensions();
showMenu();
break;
case "B":
StartMboxes();
showMenu();
break;
case "X":
Environment.Exit(0);
break;
default:
showMenu();
break;
}
}
#endregion
#region Extensions
async Task StartExtensions()
{
Console.WriteLine(_titleStartExtentions);
string _extension = getExtension();
string _target = getSourceFolder();
string _destination = getDestinationFolder();
Console.WriteLine($"Going from {_target} to {_destination}");
await processDirectoryExtensions(_target, _destination, _extension);
}
async Task processDirectoryExtensions(string Path, String Destination, String Extension)
{
List<Task> TaskList = new List<Task>();
Console.WriteLine($"Processing directory {Path}");
if (System.IO.Directory.Exists(Path))
{
foreach (string f in System.IO.Directory.GetFiles(Path))
{
var LastTask = Task.Run(async () => await ProcessExtension(f, Destination, Extension));
TaskList.Add(LastTask);
}
if (System.IO.Directory.GetDirectories(Path).Length > 0)
{
foreach (var v in System.IO.Directory.GetDirectories(Path))
{
await processDirectoryExtensions(v, Destination, Extension);
}
}
}
await Task.WhenAll(TaskList.ToArray());
}
async Task ProcessExtension(string File, String Destination, String Extension)
{
string _tagetExtension = Extension.Replace(".", "");
System.IO.FileInfo _fi = new System.IO.FileInfo(File);
if (_fi.Extension == null || _fi.Extension.Length <= 0)// || _fi.Extension.Replace(".","") != _tagetExtension)
{
String _Destionation = System.IO.Path.Combine(Destination, $"{_fi.Name}.{_tagetExtension}");
int _rename = 0;
while (System.IO.File.Exists(_Destionation))
{
_Destionation = System.IO.Path.Combine(Destination, $"{_fi.Name}_{_rename}.{_tagetExtension}");
_rename += 1;
}
System.IO.File.Move(_fi.FullName, _Destionation);
Console.WriteLine($"Processing file {File} - {_Destionation}");
}
}
#endregion
#region FileMove
void StartMove()
{
string _target = getSourceFolder();
string _destination = getDestinationFolder();
Console.WriteLine($"Going from {_target} to {_destination}");
processDirectory(_target, _destination);
showMenu();
}
void processDirectory(string Path, String Destination)
{
Console.WriteLine($"Processing directory {Path}");
if (System.IO.Directory.Exists(Path))
{
foreach(string f in System.IO.Directory.GetFiles(Path))
{
ProcessFile(f, Destination);
}
if (System.IO.Directory.GetDirectories(Path).Length > 0)
{
foreach(var v in System.IO.Directory.GetDirectories(Path))
{
processDirectory(v, Destination);
}
}
}
}
void ProcessFile(String FilePath, String Destination)
{
Console.WriteLine($"Processing file {FilePath}");
if (System.IO.File.Exists(FilePath))
{
String _fileName = System.IO.Path.GetFileName(FilePath);
String _destinationPath = System.IO.Path.Combine(Destination, _fileName);
int _rename = 0;
while(System.IO.File.Exists(_destinationPath))
{
_destinationPath = System.IO.Path.Combine(Destination, $"{_rename}_{_fileName}");
_rename += 1;
}
try
{
Console.WriteLine($"Move {_fileName} to {_destinationPath}");
System.IO.File.Move(FilePath, _destinationPath);
}
catch(System.Exception e) {
Console.WriteLine(e.Message);
}
}
}
#endregion
#region Database process
async Task StartCreateDatabase()
{
Console.WriteLine("Creating Database");
string _target = getDatabaseFolder();
string _databaseName = getDatabaseName();
Console.WriteLine($"Creating {_databaseName} at path {_target}");
await processStartCreateDatabase(_target, _databaseName);
}
string _databasePath = "";
async Task processStartCreateDatabase(string Path, String filename)
{
string _dbPath = System.IO.Path.Combine(Path, filename);
if (!System.IO.File.Exists(_dbPath))
{
// this creates a zero-byte file
SQLiteConnection.CreateFile(_dbPath);
}
try
{
string connectionString = $"Data Source={_dbPath};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = @"CREATE TABLE IF NOT EXISTS ""MBoxes"" (
""id"" INTEGER NOT NULL UNIQUE,
""MigrationStartDate"" datetime,
""MigrationEndDate"" datetime,
""SourcePath"" TEXT,
""FileName"" TEXT,
""IsMigrated"" BOOLEAN,
PRIMARY KEY(""id"" AUTOINCREMENT)
)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = @"CREATE TABLE IF NOT EXISTS ""Emails"" (
""id"" INTEGER NOT NULL UNIQUE,
""MboxId"" INTEGER,
""MigrationDate"" datetime,
""MailDate"" datetime,
""Subject"" TEXT,
""HtmlBody"" TEXT,
""TextBody"" TEXT,
""From"" TEXT,
""To"" TEXT,
""CC"" TEXT,
""BCC"" TEXT,
PRIMARY KEY(""id"" AUTOINCREMENT),
CONSTRAINT ""FK_MBOXID"" FOREIGN KEY(""MboxId"") REFERENCES ""MBoxes""(""id"")
);";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
sql = @"CREATE TABLE IF NOT EXISTS ""Attachments"" (
""id"" INTEGER NOT NULL UNIQUE,
""MailId"" INTEGER NOT NULL,
""MigrationDate"" datetime,
""DestinationPath"" TEXT,
""FileName"" TEXT,
""OriginalFileName"" TEXT,
PRIMARY KEY(""Id""),
CONSTRAINT ""FK_EMAILID"" FOREIGN KEY(""MailId"") REFERENCES ""Emails""(""id"")
);";
command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
m_dbConnection.Close();
}
_databasePath = _dbPath;
}
catch(System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
await Task.CompletedTask;
}
#endregion
#region Email Process
async Task StartEmail(bool skipDuplicate)
{
Console.WriteLine("Processing Email");
string _target = getSourceFolder();
string _destination = getDestinationFolder();
Console.WriteLine($"Going from {_target} to {_destination}");
await processDirectoryMails(_target, _destination, skipDuplicate);
}
async Task processDirectoryMails(string Path, String Destination, bool skipDuplicate)
{
List<Task> TaskList = new List<Task>();
Console.WriteLine($"Processing directory {Path}");
if (System.IO.Directory.Exists(Path))
{
foreach (string f in System.IO.Directory.GetFiles(Path))
{
if(f.EndsWith(".eml"))
{
var LastTask = Task.Run(async() => await ProcessEmailFile(f, Destination, skipDuplicate));
TaskList.Add(LastTask);
}
}
if (System.IO.Directory.GetDirectories(Path).Length > 0)
{
foreach (var v in System.IO.Directory.GetDirectories(Path))
{
await processDirectoryMails(v, Destination, skipDuplicate);
}
}
}
await Task.WhenAll(TaskList.ToArray());
}
async Task<long> ProcessEmailFile(String FilePath, String Destination, bool skipDuplicate)
{
if (System.IO.File.Exists(FilePath))
{
var message = MimeMessage.Load(FilePath);
if (message != null)
{
return await ProcessEmail(message, Destination, skipDuplicate);
}
}
return 0;
}
async Task<long> ProcessEmail(MimeMessage message, string Destination, bool skipDuplicate)
{
long _mailID = 0;
if (message != null)
{
if (System.IO.Path.Exists(_databasePath))
{
_mailID = await SaveMessageToDB(_databasePath,message);
}
foreach (var v in message.BodyParts)
{
try
{
if( v is MessagePart)
{
MessagePart part = (MessagePart)v;
if(part != null) // this is an attachment that is also an email
{
await ProcessEmail(part.Message, Destination, skipDuplicate);
}
}
else if (v is MimePart && ((MimeKit.MimePart)v).IsAttachment)
{
string _fileName = ((MimeKit.MimePart)v).FileName;
if (_fileName!=null && _fileName.Length>0)
{
_fileName = SanitizeFileName(_fileName);
}
else
{
}
byte[] _fileContents = null;
if (_fileName != null && _fileName.Length > 0)
{
using (var ms = new MemoryStream())
{
if (v is MessagePart)
{
var part = (MessagePart)v;
await part.Message.WriteToAsync(ms);
}
if (v is MimePart)
{
var part = (MimePart)v;
await part.Content.DecodeToAsync(ms);
_fileContents = ms.ToArray();
}
String _destinationPath = System.IO.Path.Combine(Destination, $"{_fileName}");
if (!skipDuplicate)
{
int _rename = 0;
while (System.IO.File.Exists(_destinationPath))
{
_destinationPath = System.IO.Path.Combine(Destination, $"{_rename}_{_fileName}");
_rename += 1;
}
}
if (!System.IO.File.Exists(_destinationPath))
{
if (_fileContents != null)
{
Console.WriteLine($"Processing email {_fileName}");
using (FileStream fs = new FileStream(_destinationPath, FileMode.OpenOrCreate))
{
await fs.WriteAsync(_fileContents, 0, _fileContents.Length);
fs.Flush();
}
}
}
else
{
Console.WriteLine($"skipped email {_fileName}");
}
if(_mailID>0)
{
await SaveAttachmentToDB(_databasePath, _mailID, Destination, _fileName, System.IO.Path.GetFileName(_destinationPath));
}
}
}
}
else if (v.IsAttachment)
{
}
else
{
// just text
}
}
catch (System.Exception e)
{
Console.WriteLine($"[ERROR] : {e.Message}");
}
}
}
return _mailID;
}
async Task<long> SaveAttachmentToDB(String Path, long mailID, string destinationPath,string OriginalFileName, string FileName)
{
long RowID = 0;
try
{
Thread.Sleep(100);
string connectionString = $"Data Source={Path};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = $@"INSERT INTO Attachments ('MailId', 'MigrationDate', 'DestinationPath', 'OriginalFileName', 'FileName')
VALUES (@mailID, @migrationdate, @destinationpath, @originalfilename, @filename) RETURNING id";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@migrationdate", DateTime.Now);
cmd.Parameters.AddWithValue("@mailID", mailID);
cmd.Parameters.AddWithValue("@destinationpath", destinationPath);
cmd.Parameters.AddWithValue("@originalfilename", OriginalFileName);
cmd.Parameters.AddWithValue("@filename", FileName);
RowID = (long)cmd.ExecuteScalar();
m_dbConnection.Close();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
return RowID;
}
async Task ShowSqlEmails()
{
if (_databasePath != null && _databasePath.Length > 0)
{
try
{
Thread.Sleep(100);
string connectionString = $"Data Source={_databasePath};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = $@"SELECT e.MailDate, e.Subject, count(a.id) 'Attachments' FROM EMAILS e
join Attachments a on a.MailId = e.Id
join MBoxes m on e.MboxId = m.Id
GROUP BY e.MailDate, E.Subject
order by MailDate desc, Subject asc,m.FileName asc";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.CommandText = sql;
using (SQLiteDataReader reader = cmd.ExecuteReader())
{
bool _hasHeader = false;
while (reader != null && reader.Read())
{
if (!_hasHeader)
{
foreach (var v in reader.GetValues())
{
Console.Write(v.ToString() + "|");
}
_hasHeader = true;
}
for(int c=0;c< reader.FieldCount;c++)
{
Console.Write(reader.GetValue(c).ToString() + "|");
}
Console.WriteLine();
}
}
m_dbConnection.Close();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
}
else
{
Console.Write("NO database path set");
}
}
async Task SaveEmailMboxId(string DbPath, long EmailId, long MboxId)
{
try
{
Thread.Sleep(100);
string connectionString = $"Data Source={DbPath};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = $@"UPDATE Emails SET MboxID =@mboxid WHERE id=@mailid;";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@mboxid", MboxId);
cmd.Parameters.AddWithValue("@mailid", EmailId);
cmd.ExecuteNonQuery();
m_dbConnection.Close();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
}
async Task<long> SaveMessageToDB(String Path, MimeMessage message)
{
long RowID = 0;
try
{
Thread.Sleep(100);
string connectionString = $"Data Source={Path};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = $@"INSERT INTO Emails ('MigrationDate', 'MailDate', 'Subject', 'HtmlBody', 'TextBody', 'from', 'To', 'CC', 'BCC')
VALUES (@migrationdate, @maildate, @subject, @htmlbody, @textbody, @mailfrom, @mailto,@mailcc, @mailbcc) RETURNING id;";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@migrationdate", DateTime.Now);
cmd.Parameters.AddWithValue("@maildate", message.Date.DateTime);
cmd.Parameters.AddWithValue("@subject", message.Subject);
cmd.Parameters.AddWithValue("@htmlbody", message.HtmlBody);
cmd.Parameters.AddWithValue("@textbody", message.TextBody);
cmd.Parameters.AddWithValue("@mailfrom", string.Join(",", message.From));
cmd.Parameters.AddWithValue("@mailto", string.Join(",", message.To));
cmd.Parameters.AddWithValue("@mailcc", string.Join(",", message.Cc));
cmd.Parameters.AddWithValue("@mailbcc", string.Join(",", message.Bcc));
RowID = (long)cmd.ExecuteScalar();
m_dbConnection.Close();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
return RowID;
}
async Task SaveMboxSetEndtime(String DBPath, long MboxId, DateTime EndTime)
{
try
{
string connectionString = $"Data Source={DBPath};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = $@"UPDATE MBoxes SET 'IsMigrated'=@ismigrated,'MigrationendDate'=@migrationenddate WHERE id=@mboxid";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@mboxid", MboxId);
cmd.Parameters.AddWithValue("@migrationenddate", EndTime);
cmd.Parameters.AddWithValue("@ismigrated", true);
cmd.ExecuteNonQuery();
m_dbConnection.Close();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
}
async Task<long> SaveMbox(string DBPath, string Path, string FileName, DateTime StartTime)
{
long RowID = 0;
try
{
string connectionString = $"Data Source={DBPath};Version=3;";
using (SQLiteConnection m_dbConnection = new SQLiteConnection(connectionString))
{
m_dbConnection.Open();
string sql = $@"INSERT INTO MBoxes ('MigrationStartDate', 'SourcePath', 'FileName', 'IsMigrated')
VALUES (@migrationdate, @sourcepath, @filename, @ismigrated) RETURNING id;";
SQLiteCommand cmd = new SQLiteCommand(sql, m_dbConnection);
cmd.CommandText = sql;
cmd.Parameters.AddWithValue("@migrationdate", StartTime);
// cmd.Parameters.AddWithValue("@migrationenddate", EndTime);
cmd.Parameters.AddWithValue("@sourcepath", Path);
cmd.Parameters.AddWithValue("@filename", FileName);
cmd.Parameters.AddWithValue("@ismigrated", false);
RowID = (long)cmd.ExecuteScalar();
m_dbConnection.Close();
}
}
catch (System.Exception ex)
{
Console.WriteLine($"[ERROR] {ex.Message}");
}
return RowID;
}
string SanitizeFileName(string fileName)
{
string _cleanString = "";
try
{
if(fileName != null && fileName.Length > 0)
{
// there can be attachmments with slashes or network paths as name. Which will throw off the destination path.
_cleanString = Regex.Replace(fileName, "[^a-zA-Z0-9_.]+", "_", RegexOptions.Compiled);
}
if (_cleanString.Length <= 0)
{
string _extension = fileName != null ? System.IO.Path.GetExtension(fileName) : "";
_cleanString = $"{DateTime.Now.ToString("yyyymmddHHmmssffff")}.{_extension}";
}
}
catch(System.Exception e)
{
// something went really wrong
}
return _cleanString;
}
#endregion
#region Mbox
async Task StartMboxes()
{
string _target = getSourceFolder();
string _destination = getDestinationFolder();
Console.WriteLine($"Going from {_target} to {_destination}");
await processDirectoryMboxes(_target, _destination, true);
}
async Task processDirectoryMboxes(string Path, String Destination, bool skipDuplicate)
{
List<Task> TaskList = new List<Task>();
Console.WriteLine($"Processing directory {Path}");
if (System.IO.Directory.Exists(Path))
{
foreach (string f in System.IO.Directory.GetFiles(Path))
{
if (f.EndsWith(".mbox"))
{
var LastTask = Task.Run(async () =>
{
await ProcessMboxFile(f, Destination, skipDuplicate);
});
TaskList.Add(LastTask);
}
}
if (System.IO.Directory.GetDirectories(Path).Length > 0)
{
foreach (var v in System.IO.Directory.GetDirectories(Path))
{
await processDirectoryMboxes(v, Destination, skipDuplicate);
}
}
}
await Task.WhenAll(TaskList.ToArray());
}
async Task ProcessMboxFile(string FilePath, string DestionationPath, bool skipDuplicate)
{
if (System.IO.File.Exists(FilePath))
{
long _mboxId = await SaveMbox(_databasePath, DestionationPath, System.IO.Path.GetFileName(FilePath), DateTime.Now);
using (System.IO.FileStream fs = new FileStream(FilePath, FileMode.Open))
{
var parser = new MimeParser(fs, MimeFormat.Mbox);
while (!parser.IsEndOfStream)
{
MimeMessage _mboxMessage = parser.ParseMessage();
if (_mboxMessage != null)
{
long _emailId = await ProcessEmail(_mboxMessage, DestionationPath, skipDuplicate);
if (_emailId > 0)
{
if(_mboxId>0)
{
await SaveEmailMboxId(_databasePath, _emailId, _mboxId);
}
}
}
}
}
await SaveMboxSetEndtime(_databasePath, _mboxId, DateTime.Now);
}
}
#endregion
showMenu();