commit de60d502028b6206b9284d23981e9f1893ab0f20 Author: Stefan ATZLESBERGER Date: Mon Jan 15 13:21:21 2024 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5003052 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vs +bin +obj \ No newline at end of file diff --git a/Checksum-Test.csproj b/Checksum-Test.csproj new file mode 100644 index 0000000..9571219 --- /dev/null +++ b/Checksum-Test.csproj @@ -0,0 +1,15 @@ + + + + Exe + net8.0 + Checksum_Test + enable + enable + + + + + + + diff --git a/Checksum-Test.sln b/Checksum-Test.sln new file mode 100644 index 0000000..3661fc5 --- /dev/null +++ b/Checksum-Test.sln @@ -0,0 +1,25 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.8.34330.188 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Checksum-Test", "Checksum-Test.csproj", "{C2F5F38A-AD77-4A0E-8910-FF27863EE1D7}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {C2F5F38A-AD77-4A0E-8910-FF27863EE1D7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C2F5F38A-AD77-4A0E-8910-FF27863EE1D7}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C2F5F38A-AD77-4A0E-8910-FF27863EE1D7}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C2F5F38A-AD77-4A0E-8910-FF27863EE1D7}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {2EC0A183-6281-44F1-A6DF-0365CB5EDD2C} + EndGlobalSection +EndGlobal diff --git a/ChecksumWorker.cs b/ChecksumWorker.cs new file mode 100644 index 0000000..224648d --- /dev/null +++ b/ChecksumWorker.cs @@ -0,0 +1,83 @@ +// See https://aka.ms/new-console-template for more information +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; +using System.Diagnostics.CodeAnalysis; +using System.Security.Cryptography; + +internal class ChecksumWorkerConfig +{ + public string Directory { + get; set; + } = "."; + + public string Extension { + get; set; + } = "*"; +} + +internal class ChecksumWorker : BackgroundService +{ + private readonly SHA512 SHA = SHA512.Create(); + private readonly ChecksumWorkerConfig _config; + private readonly ILogger _classLogger; + + public ChecksumWorker(IOptions options, ILogger classLogger) + { + _config = options.Value; + _classLogger = classLogger; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + var dateTime = DateTime.Now; + var directory = _config.Directory; + var extension = _config.Extension; + if (Directory.Exists(directory) == false) { + _classLogger.LogError($"Directory {directory} does not exist"); + return; + } + + _classLogger.LogInformation($"Watch in: {directory}"); + _classLogger.LogInformation($"Watch for: {extension}"); + + while (stoppingToken.IsCancellationRequested == false) { + var items = Directory.GetFiles(directory, extension) + .Select(file => new { + time = File.GetLastWriteTime(file), + file = file, + }) + .Where(x => x.time > dateTime) + .OrderBy(x => x.time); + foreach (var item in items) { + var fileTime = item.time; + if (fileTime > dateTime) { + if (await TryCalculateChecksumAsync(item.file) is { } checksum) { + _classLogger.LogInformation($"File {item.file} has changed {checksum}", checksum); + } + else { + break; // try again later + } + } + + dateTime = fileTime; + } + + await Task.Delay(1000, stoppingToken); + } + } + + private async Task TryCalculateChecksumAsync(string file) + { + try { + using (var fs = File.Open(file, FileMode.Open, FileAccess.Read, FileShare.Read)) { + var checksum = await SHA.ComputeHashAsync(fs); + + return Convert.ToBase64String(checksum); + } + } + catch { + return default; + } + } +} \ No newline at end of file diff --git a/Program.cs b/Program.cs new file mode 100644 index 0000000..646c6b8 --- /dev/null +++ b/Program.cs @@ -0,0 +1,15 @@ +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Hosting; + + + +var host = Host.CreateApplicationBuilder(args); +var services = host.Services; +var configuration = host.Configuration; + +services.Configure(configuration.GetSection("ChecksumWorker")); +services.AddHostedService(); + +var app = host.Build(); + +await app.RunAsync(); \ No newline at end of file diff --git a/Properties/launchSettings.json b/Properties/launchSettings.json new file mode 100644 index 0000000..511b9a3 --- /dev/null +++ b/Properties/launchSettings.json @@ -0,0 +1,8 @@ +{ + "profiles": { + "Checksum-Test": { + "commandName": "Project", + "commandLineArgs": "/ChecksumWorker:Directory=C:\\Users\\stefan.atzlesberger\\Downloads /ChecksumWorker:Extension=*.ifc" + } + } +} \ No newline at end of file