-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathDecryptor.cs
More file actions
612 lines (528 loc) · 23.5 KB
/
Decryptor.cs
File metadata and controls
612 lines (528 loc) · 23.5 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
using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Runtime.InteropServices.ComTypes;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Decoder.Encryption;
using Decoder.Models;
using Decoder.Option;
using static System.String;
using static Decoder.Option.Utils;
using SearchOption = System.IO.SearchOption;
namespace Decoder
{
public class Decryptor
{
#region Fields
//private VirtualFileStream playingFileStream;
private List<char> InvalidPathCharacters = new List<char>(), InvalidFileCharacters = new List<char>();
private SQLiteConnection DatabaseConnection;
public DecryptorOptions Options = new DecryptorOptions();
private ConsoleColor color_default;
private List<Task> TaskList = new List<Task>();
private SemaphoreSlim Semaphore = new SemaphoreSlim(5);
private object SemaphoreLock = new object();
#endregion Fields
/// <summary>
/// Constructor Of Decryptor Class. Init invalid characters of path and console colors.
/// </summary>
public Decryptor()
{
InvalidPathCharacters.AddRange(Path.GetInvalidPathChars());
//InvalidPathCharacters.AddRange(new char[] {':', '?', '"', '\\', '/'});
InvalidFileCharacters.AddRange(Path.GetInvalidFileNameChars());
//InvalidFileCharacters.AddRange(new char[] {':', '?', '"', '\\', '/'});
color_default = Console.ForegroundColor;
}
/// <summary>
/// Constructor Of Decryptor Class. Init options from the console.
/// </summary>
/// <param name="options">Decryptor Options</param>
public Decryptor(DecryptorOptions options) : this()
{
Options = options;
if (options.UseDatabase)
Options.UseDatabase = InitDB(options.DatabasePath);
}
/// <summary>
/// Clean the input string and remove all invalid chars
/// </summary>
/// <param name="path">input path</param>
/// <returns></returns>
public string CleanName(string path)
{
foreach (var invalidChar in InvalidFileCharacters)
path = path.Replace(invalidChar, '-');
return path;
}
/// <summary>
/// Encrypt two string to become folder name.
/// </summary>
/// <param name="moduleName">Name of Module</param>
/// <param name="moduleAuthorName">Name of Author in the course</param>
/// <returns>String has been encrypted</returns>
public string ModuleHash(string moduleName, string moduleAuthorName)
{
string s = moduleName + "|" + moduleAuthorName;
using (MD5 md5 = MD5.Create())
return Convert.ToBase64String(md5.ComputeHash(Encoding.UTF8.GetBytes(s))).Replace('/', '_');
}
/// <summary>
/// Decryption and rename course, module path.
/// </summary>
/// <param name="folderPath">Source path contains all courses</param>
/// <param name="outputFolder">Destination of output course</param>
public async Task DecryptAllFolders(string folderPath, string outputFolder = "")
{
if (!Directory.Exists(folderPath))
{
throw new DirectoryNotFoundException();
}
if (IsNullOrWhiteSpace(outputFolder))
{
outputFolder = folderPath;
}
else
{
if (!Directory.Exists(outputFolder))
{
System.IO.Directory.CreateDirectory(outputFolder);
}
}
foreach (string coursePath in Directory.GetDirectories(folderPath, "*",
SearchOption.TopDirectoryOnly))
{
var course = GetCourseFromDb(coursePath);
if (course != null)
{
// Create new course path with the output path
var newCoursePath = Path.Combine(outputFolder, CleanName(course.CourseTitle));
DirectoryInfo courseInfo = Directory.Exists(newCoursePath)
? new DirectoryInfo(newCoursePath)
: Directory.CreateDirectory(newCoursePath);
// Move all folders and its contents to newCoursePath
#region Move file
/*
if (Path.GetPathRoot(newCoursePath) != Path.GetPathRoot(coursePath))
{
Microsoft.VisualBasic.FileIO.FileSystem.MoveDirectory(coursePath, newCoursePath);
}
else
{
Directory.Move(coursePath, newCoursePath);
}
*/
#endregion Move file
// Get list all modules in current course
List<Module> listModules = course.Modules;
if (listModules.Count > 0)
{
// Get each module
foreach (Module module in listModules)
{
// Generate module hash name
string moduleHash = ModuleHash(module.ModuleName, module.AuthorHandle);
// Generate module path
string moduleHashPath = Path.Combine(coursePath, moduleHash);
// Create new module path with decryption name
string newModulePath = Path.Combine(courseInfo.FullName,
module.ModuleIndex + ". " + module.ModuleTitle);
// If length too long, rename it
if (newModulePath.Length > 240)
{
newModulePath = Path.Combine(courseInfo.FullName,
module.ModuleIndex + "");
}
if (Directory.Exists(moduleHashPath))
{
DirectoryInfo moduleInfo = Directory.Exists(newModulePath)
? new DirectoryInfo(newModulePath)
: Directory.CreateDirectory(newModulePath);
// Decrypt all videos in current module folder
await Task.Run(() => DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName));
}
else
{
WriteToConsole(
"Folder " + moduleHash +
" cannot be found in the current course path.",
ConsoleColor.Red);
}
}
}
WriteToConsole("Decryption " + course.CourseTitle + " has been completed!", ConsoleColor.Magenta);
}
}
}
public bool RemoveCourseInDb(string coursePath)
{
string courseName = GetFolderName(coursePath);
var cmd = DatabaseConnection.CreateCommand();
cmd.CommandText = @"DELETE FROM Course
WHERE Name = @courseName";
cmd.Parameters.Add(new SQLiteParameter("@courseName", courseName));
var reader = cmd.ExecuteNonQuery();
return reader > 0;
}
public bool RemoveCourseInDisk(string coursePath)
{
try
{
if (Directory.Exists(coursePath))
{
Directory.Delete(coursePath, true);
}
return true;
}
catch (Exception ex)
{
WriteToConsole(ex.Message, ConsoleColor.White);
return false;
}
}
/// <summary>
/// Check, if the path or file name contains any illegal characters
/// </summary>
/// <param name="path">The path to the file or directory</param>
/// <returns>A path without any illegal characters</returns>
private string EscapeIllegalCharacters(string path)
{
// Windows NTFS (macOS is only ':')
string illegalCharacters = "/?<>:*|\"";
foreach (char c in illegalCharacters)
{
path = path.Replace(c.ToString(), String.Empty);
}
return path;
}
/// <summary>
/// Decrypt all videos in current module folder.
/// </summary>
/// <param name="folderPath">Current module folder</param>
/// <param name="moduleId">Module Id</param>
/// <param name="outputPath">Destination of output video</param>
public void DecryptAllVideos(string folderPath, Module module, string outputPath)
{
// Get all clips of this module from database
List<Clip> listClips = module.Clips;
if (listClips.Count > 0)
{
foreach (Clip clip in listClips)
{
// Get current path of the encrypted video
string currPath = Path.Combine(folderPath, clip.ClipName + ".psv");
if (File.Exists(currPath))
{
// Create new path with output folder
string newPath = Path.Combine(outputPath,
clip.ClipIndex + ". " + clip.ClipTitle + ".mp4");
// If length too long, rename it
if (newPath.Length > 240)
{
newPath = Path.Combine(outputPath,
clip.ClipIndex + ".mp4");
}
// Init video and get it from istream
IStream iStream;
var playingFileStream = new VirtualFileStream(currPath);
playingFileStream.Clone(out iStream);
string fileName = Path.GetFileName(currPath);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine("Start to Decrypt File \"{0}\"", fileName);
Console.ForegroundColor = color_default;
DecryptVideo(iStream, newPath);
if (Options.CreateTranscript)
{
// Generate transcript file if user ask
WriteTranscriptFile(clip, newPath);
}
//Semaphore.Wait();
//TaskList.Add(Task.Run(() =>
//{
// // Write the decrypted video from istream to new file mp4
// DecryptVideo(iStream, newPath);
// if (Options.CreateTranscript)
// {
// // Generate transcript file if user ask
// WriteTranscriptFile(clip, newPath);
// }
// lock (SemaphoreLock)
// {
// Semaphore.Release();
// }
//}));
WriteToConsole($"Decryption File \"{Path.GetFileName(newPath)}\" successfully ", ConsoleColor.Green, true);
playingFileStream.Dispose();
}
else
{
WriteToConsole($"File \"{Path.GetFileName(currPath)}\" cannot be found ", ConsoleColor.Gray, true);
}
}
}
}
/// <summary>
/// Write transcript for the clip if it available.
/// </summary>
/// <param name="clipId">Clip Id</param>
/// <param name="clipPath">Path of current clip</param>
public void WriteTranscriptFile(Clip clip, string clipPath)
{
// Get all transcript to list
List<ClipTranscript> clipTranscripts = clip.Subtitle;
if (clipTranscripts.Count > 0)
{
// Create transcript path with the same name of the clip
string transcriptPath = Path.Combine(Path.GetDirectoryName(clipPath),
Path.GetFileNameWithoutExtension(clipPath) + ".srt");
if (!File.Exists(transcriptPath))
{
// Write it to file with stream writer
StreamWriter writer = new StreamWriter(transcriptPath);
int i = 1;
foreach (var clipTranscript in clipTranscripts)
{
var start = TimeSpan.FromMilliseconds(clipTranscript.StartTime).ToString(@"hh\:mm\:ss\,fff");
var end = TimeSpan.FromMilliseconds(clipTranscript.EndTime).ToString(@"hh\:mm\:ss\,fff");
writer.WriteLine(i++);
writer.WriteLine(start + " --> " + end);
writer.WriteLine(clipTranscript.Text);
writer.WriteLine();
}
writer.Close();
WriteToConsole("Transcript of " + Path.GetFileName(clipPath) + " has been generated successfully.",
ConsoleColor.Red);
}
}
}
public string RenameIfDuplicated(string path)
{
string newFullPath = Empty;
int count = 1;
// If path is file
if (Path.HasExtension(path))
{
string fileName = Path.GetFileNameWithoutExtension(path);
string extension = Path.GetExtension(path);
string currPath = Path.GetDirectoryName(path);
newFullPath = path;
while (File.Exists(newFullPath))
{
string tempFileName = $"{fileName} ({count++})";
newFullPath = Path.Combine(currPath, tempFileName + extension);
}
}
// Else path is directory
else
{
string folderName = GetFolderName(path);
string currPath = Path.GetDirectoryName(path);
newFullPath = path;
while (Directory.Exists(newFullPath))
{
string tempFileName = $"{folderName} ({count++})";
newFullPath = Path.Combine(currPath, tempFileName);
}
}
return newFullPath;
}
public string GetFileNameIfItExisted(string filePath, bool checkExisted = false)
{
if (checkExisted)
{
if (File.Exists(filePath))
{
return filePath.Substring(filePath.LastIndexOf(@"\") + 1);
}
throw new FileNotFoundException();
}
return filePath.Substring(filePath.LastIndexOf(@"\") + 1);
}
/// <summary>
/// Get current folder name in the full path.
/// </summary>
/// <param name="folderPath">The full folder path.</param>
/// <param name="checkExisted">Determine if user need to check folder is existed.</param>
/// <returns>Folder name of full path.</returns>
public string GetFolderName(string folderPath, bool checkExisted = false)
{
if (checkExisted)
{
if (Directory.Exists(folderPath))
{
return folderPath.Substring(folderPath.LastIndexOf(@"\") + 1);
}
throw new DirectoryNotFoundException();
}
return folderPath.Substring(folderPath.LastIndexOf(@"\") + 1);
}
/// <summary>
/// Compare two files
/// </summary>
/// <param name="fileOne">Path of the first file</param>
/// <param name="fileTwo">Path of the second file</param>
/// <returns>Boolean value determine two files is the same.</returns>
public bool CompareTwoFiles(string fileOne, string fileTwo)
{
return File.ReadAllBytes(fileOne).LongLength == File.ReadAllBytes(fileTwo).LongLength;
}
/// <summary>
/// Decrypt video.
/// </summary>
/// <param name="curStream">IStream</param>
/// <param name="newPath">Output path of the clip</param>
public void DecryptVideo(IStream curStream, string newPath)
{
STATSTG stat;
curStream.Stat(out stat, 0);
IntPtr myPtr = (IntPtr)0;
int strmSize = (int)stat.cbSize;
byte[] strmInfo = new byte[strmSize];
curStream.Read(strmInfo, strmSize, myPtr);
File.WriteAllBytes(newPath, strmInfo);
}
#region DB
/// <summary>
/// Get transcript text of specified clip from database.
/// </summary>
/// <param name="clipId">Clip Id</param>
/// <returns>List of transcript text of the current clip.</returns>
public List<ClipTranscript> GetTrasncriptFromDb(int clipId)
{
List<ClipTranscript> list = new List<ClipTranscript>();
var cmd = DatabaseConnection.CreateCommand();
cmd.CommandText = @"SELECT StartTime, EndTime, Text
FROM ClipTranscript
WHERE ClipId = @clipId
ORDER BY Id ASC";
cmd.Parameters.Add(new SQLiteParameter("@clipId", clipId));
var reader = cmd.ExecuteReader();
while (reader.Read())
{
ClipTranscript clipTranscript = new ClipTranscript
{
StartTime = reader.GetInt32(reader.GetOrdinal("StartTime")),
EndTime = reader.GetInt32(reader.GetOrdinal("EndTime")),
Text = reader.GetString(reader.GetOrdinal("Text"))
};
list.Add(clipTranscript);
}
return list;
}
/// <summary>
/// Get all clips information of specified module from database.
/// </summary>
/// <param name="moduleId">Module Id</param>
/// <returns>List of information about clips belong to specifed module.</returns>
public List<Clip> GetClipsFromDb(int moduleId)
{
List<Clip> list = new List<Clip>();
var cmd = DatabaseConnection.CreateCommand();
cmd.CommandText = @"SELECT Id, Name, Title, ClipIndex
FROM Clip
WHERE ModuleId = @moduleId";
cmd.Parameters.Add(new SQLiteParameter("@moduleId", moduleId));
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Clip clip = new Clip
{
ClipId = reader.GetInt32(reader.GetOrdinal("Id")),
ClipName = reader.GetString(reader.GetOrdinal("Name")),
ClipTitle = CleanName(reader.GetString(reader.GetOrdinal("Title"))),
ClipIndex = reader.GetInt32(reader.GetOrdinal("ClipIndex")),
Subtitle = GetTrasncriptFromDb(reader.GetInt32(reader.GetOrdinal("Id")))
};
list.Add(clip);
}
reader.Close();
return list;
}
/// <summary>
/// Get all modules information of specified course from database.
/// </summary>
/// <param name="courseName">Name of course</param>
/// <returns>List of modules information of specified course.</returns>
public List<Module> GetModulesFromDb(string courseName)
{
List<Module> list = new List<Module>();
var cmd = DatabaseConnection.CreateCommand();
cmd.CommandText = @"SELECT Id, Name, Title, AuthorHandle, ModuleIndex
FROM Module
WHERE CourseName = @courseName";
cmd.Parameters.Add(new SQLiteParameter("@courseName", courseName));
var reader = cmd.ExecuteReader();
while (reader.Read())
{
Module module = new Module
{
ModuleId = reader.GetInt32(reader.GetOrdinal("Id")),
AuthorHandle = reader.GetString(reader.GetOrdinal("AuthorHandle")),
ModuleName = reader.GetString(reader.GetOrdinal("Name")),
ModuleTitle = CleanName(reader.GetString(reader.GetOrdinal("Title"))),
ModuleIndex = reader.GetInt32(reader.GetOrdinal("ModuleIndex")),
Clips = GetClipsFromDb(reader.GetInt32(reader.GetOrdinal("Id")))
};
list.Add(module);
}
reader.Close();
return list;
}
/// <summary>
/// Get course information from database.
/// </summary>
/// <param name="folderCoursePath">Folder contains all courses</param>
/// <returns>Course information</returns>
public Course GetCourseFromDb(string folderCoursePath)
{
Course course = null;
string courseName = GetFolderName(folderCoursePath, true).Trim().ToLower();
var cmd = DatabaseConnection.CreateCommand();
cmd.CommandText = @"SELECT Name, Title, HasTranscript
FROM Course
WHERE Name = @courseName";
cmd.Parameters.Add(new SQLiteParameter("@courseName", courseName));
var reader = cmd.ExecuteReader();
if (reader.Read())
{
course = new Course
{
CourseName = reader.GetString(reader.GetOrdinal("Name")),
CourseTitle = CleanName(reader.GetString(reader.GetOrdinal("Title"))),
HasTranscript = reader.GetInt32(reader.GetOrdinal("HasTranscript")),
Modules = GetModulesFromDb(reader.GetString(reader.GetOrdinal("Name")))
};
}
reader.Close();
return course;
}
/// <summary>
/// Init database connection.
/// </summary>
/// <param name="dbPath">Database file path</param>
/// <returns>Boolean value determine the database is open successful or not</returns>
public bool InitDB(string dbPath)
{
if (File.Exists(dbPath))
{
if (Path.GetExtension(dbPath).Equals(".db"))
{
DatabaseConnection = new SQLiteConnection($"Data Source={dbPath}; Version=3;FailIfMissing=True");
DatabaseConnection.Open();
WriteToConsole("The Database Connection has been open completely." + Environment.NewLine,
ConsoleColor.Green);
return true;
}
WriteToConsole("The database file isn't corrected.", ConsoleColor.Red);
return false;
}
WriteToConsole("Cannot find the database path.", ConsoleColor.Red);
return false;
}
#endregion DB
}
}