2023-06-25 21:12:42 +02:00
|
|
|
using System.Reflection;
|
2023-06-24 01:02:03 +02:00
|
|
|
using Microsoft.Data.Sqlite;
|
2023-06-25 02:45:33 +02:00
|
|
|
namespace Chksum.Utils;
|
|
|
|
public class ChksumUtils {
|
|
|
|
|
2023-05-16 22:50:29 +02:00
|
|
|
// int getDirectoryCount() {
|
|
|
|
// int folderCount = Directory.GetDirectories(Directory.GetCurrentDirectory()).Length; // Get folder count in current directory
|
|
|
|
// return folderCount;
|
|
|
|
// }
|
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
private int getFileCount() {
|
2023-05-16 22:50:29 +02:00
|
|
|
int fileCount = Directory.GetFiles(Directory.GetCurrentDirectory()).Length; // Get file count in current directory
|
|
|
|
return fileCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
// string getParentFolder() {
|
|
|
|
// string parentFolder = Directory.GetParent(Directory.GetCurrentDirectory()).ToString(); // Get parent folder of current directory
|
|
|
|
// return parentFolder;
|
|
|
|
// }
|
|
|
|
|
2023-06-25 21:12:42 +02:00
|
|
|
public string DatabaseRoot { get; set; } = string.Empty;
|
2023-06-25 02:45:33 +02:00
|
|
|
public void getBaseDir() {
|
2023-06-24 01:02:03 +02:00
|
|
|
DatabaseRoot = AppDomain.CurrentDomain.BaseDirectory;
|
|
|
|
}
|
|
|
|
|
2023-06-25 21:12:42 +02:00
|
|
|
public string libraryPath { get; set; } = string.Empty;
|
|
|
|
public void ExtractEmbeddedLibrary() {
|
|
|
|
libraryPath = Path.Combine(DatabaseRoot, "libe_sqlite3.so");
|
|
|
|
|
|
|
|
using (Stream? resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Chksum.Libraries.libe_sqlite3.so")) {
|
|
|
|
if (resourceStream != null) {
|
|
|
|
byte[] buffer = new byte[resourceStream.Length];
|
|
|
|
resourceStream.Read(buffer, 0, buffer.Length);
|
|
|
|
File.WriteAllBytes(libraryPath, buffer);
|
|
|
|
} else {
|
|
|
|
throw new Exception(libraryPath + " could not be loaded");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanup() {
|
|
|
|
File.Delete(libraryPath);
|
|
|
|
}
|
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
public void initializeDB() {
|
|
|
|
if (!File.Exists("chksum.db")) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void cleanDB() {
|
|
|
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db")) {
|
2023-06-24 01:02:03 +02:00
|
|
|
var command = connection.CreateCommand();
|
|
|
|
command.CommandText =
|
|
|
|
@"
|
2023-06-25 02:45:33 +02:00
|
|
|
vacuum;
|
2023-06-24 01:02:03 +02:00
|
|
|
";
|
|
|
|
command.ExecuteNonQuery();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
private string CalculateMD5(string filename) {
|
2023-06-04 23:44:03 +02:00
|
|
|
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
|
|
|
using (var stream = File.OpenRead(filename)) {
|
2023-05-16 22:50:29 +02:00
|
|
|
var hash = md5.ComputeHash(stream);
|
|
|
|
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
public void doTheThing() {
|
|
|
|
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
2023-06-25 03:32:43 +02:00
|
|
|
Directory.SetCurrentDirectory(directory); // Set new root
|
|
|
|
if (getFileCount() >= 1) {
|
|
|
|
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
|
|
|
FileInfo[] files = dir.GetFiles();
|
|
|
|
foreach (FileInfo file in files) {
|
|
|
|
string fileName = file.Name;
|
|
|
|
string absolutePathToFile = Path.GetFullPath(fileName);
|
|
|
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
|
|
|
string fileHash = CalculateMD5(fileName);
|
2023-06-24 01:02:03 +02:00
|
|
|
|
2023-06-25 03:32:43 +02:00
|
|
|
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExists(fileHash, fileName) == false) {
|
|
|
|
connection.Open();
|
2023-06-24 01:02:03 +02:00
|
|
|
|
2023-06-25 03:32:43 +02:00
|
|
|
var command = connection.CreateCommand();
|
|
|
|
command.CommandText =
|
|
|
|
@"
|
|
|
|
INSERT INTO file (filehash, filename, pathtofile)
|
|
|
|
VALUES ($filehash, $filename, $pathtofile)
|
2023-06-25 03:28:34 +02:00
|
|
|
";
|
2023-06-25 03:32:43 +02:00
|
|
|
command.Parameters.AddWithValue("$filehash", fileHash);
|
|
|
|
command.Parameters.AddWithValue("$filename", fileName);
|
|
|
|
command.Parameters.AddWithValue("$pathtofile", pathToFile);
|
|
|
|
command.ExecuteNonQuery();
|
2023-06-24 01:02:03 +02:00
|
|
|
}
|
2023-05-16 22:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
2023-06-25 03:32:43 +02:00
|
|
|
doTheThing();
|
|
|
|
}
|
2023-06-25 02:45:33 +02:00
|
|
|
}
|
|
|
|
|
2023-06-25 03:28:34 +02:00
|
|
|
private bool checkIfFileAlreadyExists(string fileHash, string pathToFile) {
|
2023-06-25 02:45:33 +02:00
|
|
|
string filehash = string.Empty;
|
2023-06-25 03:28:34 +02:00
|
|
|
string pathtofile = string.Empty;
|
2023-06-25 02:45:33 +02:00
|
|
|
|
|
|
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
var command = connection.CreateCommand();
|
|
|
|
command.CommandText =
|
|
|
|
@"
|
2023-06-25 03:28:34 +02:00
|
|
|
SELECT filehash, pathtofile FROM file WHERE filehash = $filehash
|
2023-06-25 02:45:33 +02:00
|
|
|
";
|
|
|
|
command.Parameters.AddWithValue("$filehash", fileHash);
|
|
|
|
|
|
|
|
using (var reader = command.ExecuteReader()) {
|
|
|
|
while (reader.Read()) {
|
|
|
|
filehash = reader.GetString(0);
|
2023-06-25 03:28:34 +02:00
|
|
|
pathtofile = reader.GetString(1);
|
2023-06-25 02:45:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fileHash == filehash) {
|
2023-06-25 03:28:34 +02:00
|
|
|
Console.WriteLine($"Duplicate files found: {pathToFile} with the hash {fileHash} is identical to {pathtofile} with the hash {filehash}");
|
2023-06-25 02:45:33 +02:00
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
2023-05-16 22:50:29 +02:00
|
|
|
}
|
|
|
|
}
|
2023-05-16 23:21:43 +02:00
|
|
|
|
2023-06-25 03:28:34 +02:00
|
|
|
private bool checkIfFileMovedAndUpdatePathToFile(string fileHash, string fileName, string pathToFile) {
|
|
|
|
string pathtofile = string.Empty;
|
|
|
|
|
|
|
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
|
|
|
connection.Open();
|
|
|
|
|
|
|
|
var command = connection.CreateCommand();
|
|
|
|
command.CommandText =
|
|
|
|
@"
|
|
|
|
SELECT pathtofile FROM file WHERE filehash = $filehash
|
|
|
|
";
|
|
|
|
command.Parameters.AddWithValue("$filehash", fileHash);
|
|
|
|
|
|
|
|
using (var reader = command.ExecuteReader()) {
|
|
|
|
while (reader.Read()) {
|
|
|
|
pathtofile = reader.GetString(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pathToFile != pathtofile && pathtofile != "") {
|
|
|
|
var command2 = connection.CreateCommand();
|
|
|
|
command2.CommandText =
|
|
|
|
@"
|
|
|
|
UPDATE file
|
|
|
|
SET pathtofile = $newpathtofile
|
|
|
|
WHERE filehash = $filehash
|
|
|
|
";
|
|
|
|
command2.Parameters.AddWithValue("$newpathtofile", pathToFile);
|
|
|
|
command2.Parameters.AddWithValue("$filehash", fileHash);
|
|
|
|
command2.ExecuteNonQuery();
|
|
|
|
|
|
|
|
Console.WriteLine($"File moved: {fileName} was previously at {pathtofile} but is now at {pathToFile}");
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
private int getTotalFileCount() {
|
2023-05-16 23:21:43 +02:00
|
|
|
int totalFileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.AllDirectories).Length;
|
|
|
|
return totalFileCount - 1; // Remove the program from the totalFileCount
|
|
|
|
}
|
2023-05-17 12:18:20 +02:00
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
public void countAllMd5Checksums() {
|
2023-05-17 12:18:20 +02:00
|
|
|
int totalMD5FileCount = Directory.GetFiles(Directory.GetCurrentDirectory(), "*.md5", SearchOption.AllDirectories).Length;
|
|
|
|
Console.WriteLine("There are " + totalMD5FileCount + " md5 checksums");
|
|
|
|
}
|
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
public void deleteAllMd5Checksums() {
|
2023-05-17 12:18:20 +02:00
|
|
|
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
|
|
|
Directory.SetCurrentDirectory(directory); // Set new root
|
|
|
|
if (getFileCount() >= 1) {
|
|
|
|
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
|
|
|
FileInfo[] files = dir.GetFiles();
|
|
|
|
foreach (FileInfo file in files) {
|
|
|
|
string fileName = file.Name;
|
|
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
|
|
|
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5";
|
|
|
|
File.Delete(checksumFile);
|
|
|
|
Console.WriteLine("Deleted " + checksumFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
deleteAllMd5Checksums();
|
|
|
|
}
|
|
|
|
}
|
2023-05-17 14:52:30 +02:00
|
|
|
|
2023-06-25 02:45:33 +02:00
|
|
|
public void compareChecksums() {
|
2023-05-17 14:52:30 +02:00
|
|
|
foreach (var directory in Directory.GetDirectories(Directory.GetCurrentDirectory())) {
|
|
|
|
Directory.SetCurrentDirectory(directory); // Set new root
|
|
|
|
if (getFileCount() >= 1) {
|
|
|
|
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
|
|
|
FileInfo[] files = dir.GetFiles();
|
|
|
|
// files.ToList().ForEach(i => Console.WriteLine(i.ToString())); // Print all files in files array
|
|
|
|
foreach (FileInfo file in files) {
|
|
|
|
string fileName = file.Name;
|
|
|
|
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName);
|
|
|
|
string checksumFile = Directory.GetCurrentDirectory() + "/" + fileNameWithoutExtension + ".md5";
|
|
|
|
string fileMd5Checksum = fileNameWithoutExtension + ".md5";
|
|
|
|
if (File.Exists(fileMd5Checksum)) {
|
|
|
|
string newFileChecksum = CalculateMD5(fileName) + " " + fileName;
|
|
|
|
string existingFileChecksum = File.ReadAllText(fileMd5Checksum);
|
|
|
|
string newFileName = newFileChecksum.Substring(34);
|
|
|
|
string existingFileName = existingFileChecksum.Substring(34);
|
|
|
|
if (newFileChecksum.Equals(existingFileChecksum)) {
|
|
|
|
Console.WriteLine(newFileName + " and " + existingFileName + " are the same.");
|
|
|
|
} else {
|
|
|
|
Console.WriteLine(newFileName + " and " + existingFileName + " are not the same.");
|
|
|
|
Console.WriteLine("The checksum of " + newFileName + " is " + newFileChecksum);
|
|
|
|
Console.WriteLine("The checksum of the already exting file " + existingFileName + " is " + existingFileChecksum);
|
|
|
|
// TODO Tell the user to check which file is the correct one
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
File.AppendAllText(checksumFile, CalculateMD5(fileName) + " " + fileName);
|
|
|
|
Console.WriteLine("Calculated checksum for: " + checksumFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
compareChecksums();
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 22:50:29 +02:00
|
|
|
}
|