feature/multi-threading #2
@ -1,4 +1,6 @@
|
|||||||
using System.Reflection;
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Data.Sqlite;
|
using Microsoft.Data.Sqlite;
|
||||||
namespace Chksum.Utils;
|
namespace Chksum.Utils;
|
||||||
public class ChksumUtils {
|
public class ChksumUtils {
|
||||||
@ -62,13 +64,19 @@ public class ChksumUtils {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private string CalculateMD5(string filename) {
|
private string[] CalculateMD5(string[] filenames) {
|
||||||
using (var md5 = System.Security.Cryptography.MD5.Create()) {
|
string[] checksums = new string[filenames.Length];
|
||||||
using (var stream = File.OpenRead(filename)) {
|
|
||||||
var hash = md5.ComputeHash(stream);
|
Parallel.ForEach(filenames, (filename, state, index) => {
|
||||||
return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
using (var md5 = MD5.Create()) {
|
||||||
|
using (var stream = File.OpenRead(filename)) {
|
||||||
|
var hash = md5.ComputeHash(stream);
|
||||||
|
checksums[index] = BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
});
|
||||||
|
|
||||||
|
return checksums;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void doTheThing() {
|
public void doTheThing() {
|
||||||
@ -76,14 +84,22 @@ public class ChksumUtils {
|
|||||||
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
using (var connection = new SqliteConnection("Data Source=" + DatabaseRoot + "chksum.db;Mode=ReadWrite")) {
|
||||||
Directory.SetCurrentDirectory(directory); // Set new root
|
Directory.SetCurrentDirectory(directory); // Set new root
|
||||||
if (getFileCount() >= 1) {
|
if (getFileCount() >= 1) {
|
||||||
DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
string[] filenames = Directory.GetFiles(directory);
|
||||||
FileInfo[] files = dir.GetFiles();
|
string[] fileHashes = CalculateMD5(filenames);
|
||||||
foreach (FileInfo file in files) {
|
//DirectoryInfo dir = new DirectoryInfo(Directory.GetCurrentDirectory());
|
||||||
string fileName = file.Name;
|
//FileInfo[] files = dir.GetFiles();
|
||||||
string absolutePathToFile = Path.GetFullPath(fileName);
|
int index = 0;
|
||||||
|
foreach (string file in filenames) {
|
||||||
|
index++;
|
||||||
|
string absolutePathToFile = file;
|
||||||
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
string pathToFile = Path.GetRelativePath(DatabaseRoot, absolutePathToFile);
|
||||||
string fileHash = CalculateMD5(fileName);
|
string fileHash = "";
|
||||||
|
if (index < fileHashes.Length) {
|
||||||
|
//Console.WriteLine("Index at: " + index);
|
||||||
|
fileHash = fileHashes.GetValue(index).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
string fileName = Path.GetFileName(absolutePathToFile);
|
||||||
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
|
if (checkIfFileMovedAndUpdatePathToFile(fileHash, fileName, pathToFile) == false && checkIfFileAlreadyExistsInDatabase(fileHash, fileName) == false) {
|
||||||
connection.Open();
|
connection.Open();
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user