feature/SQlite #1

Merged
ProfessionalUwU merged 14 commits from feature/SQlite into main 2023-06-26 18:22:28 +02:00
2 changed files with 52 additions and 5 deletions
Showing only changes of commit 846b983caa - Show all commits

View File

@ -1,4 +1,4 @@
public class Program { public class Program {
static void Main(string[] args) { static void Main(string[] args) {
Console.ForegroundColor = ConsoleColor.Red; Console.ForegroundColor = ConsoleColor.Red;
@ -11,6 +11,8 @@
return; return;
} }
Chksum.getBaseDir();
Console.ForegroundColor = ConsoleColor.Green; Console.ForegroundColor = ConsoleColor.Green;
switch (args[0]) { switch (args[0]) {
case "checksum": case "checksum":
@ -46,6 +48,9 @@
Chksum.compareChecksums(); Chksum.compareChecksums();
break; break;
case "createDB":
Chksum.initializeDB();
break;
case "help": case "help":
PrintAvailableOptions(); PrintAvailableOptions();
break; break;
@ -63,6 +68,7 @@
"countmd5", "countmd5",
"deletemd5", "deletemd5",
"compareChecksums", "compareChecksums",
"createDB",
"help" "help"
}; };

View File

@ -1,7 +1,10 @@
using Microsoft.Data.Sqlite;
// Go into folder // Go into folder
// Check if any file is in there // Check if any file is in there
// If there is a file. Calculate md5sum > filename.md5 // If there is a file. Calculate md5sum > filename.md5
// If there is no file. Repeat // If there is no file. Repeat
public class Chksum { public class Chksum {
// int getDirectoryCount() { // int getDirectoryCount() {
@ -19,6 +22,30 @@ public class Chksum {
// return parentFolder; // return parentFolder;
// } // }
public static string DatabaseRoot { get; set; }
public static void getBaseDir() {
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
}
public static void initializeDB() {
using (var connection = new SqliteConnection("Data Source=chksum.db")) {
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
CREATE TABLE file (
filehash TEXT NOT NULL PRIMARY KEY,
filename TEXT NOT NULL,
pathtofile TEXT NOT NULL,
artist TEXT,
playbacklength INTEGER
);
";
command.ExecuteNonQuery();
}
}
private static string CalculateMD5(string filename) { private static string CalculateMD5(string filename) {
using (var md5 = System.Security.Cryptography.MD5.Create()) { using (var md5 = System.Security.Cryptography.MD5.Create()) {
using (var stream = File.OpenRead(filename)) { using (var stream = File.OpenRead(filename)) {
@ -36,10 +63,24 @@ public class Chksum {
FileInfo[] files = dir.GetFiles(); FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files) { foreach (FileInfo file in files) {
string fileName = file.Name; string fileName = file.Name;
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName); string absolutePathToFile = Path.GetFullPath(fileName);
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5"; string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName); string fileHash = CalculateMD5(fileName);
Console.WriteLine(checksumFile);
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
connection.Open();
var command = connection.CreateCommand();
command.CommandText =
@"
INSERT INTO file (filehash, filename, pathtofile)
VALUES ($filehash, $filename, $pathtofile)
";
command.Parameters.AddWithValue("$filehash", fileHash);
command.Parameters.AddWithValue("$filename", fileName);
command.Parameters.AddWithValue("$pathtofile", pathToFile);
command.ExecuteNonQuery();
}
} }
} }
doTheThing(); doTheThing();