initial commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
.vs
|
||||
bin
|
||||
obj
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<RootNamespace>Checksum_Test</RootNamespace>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -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
|
||||
@@ -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<ChecksumWorker> _classLogger;
|
||||
|
||||
public ChecksumWorker(IOptions<ChecksumWorkerConfig> options, ILogger<ChecksumWorker> 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<string?> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
+15
@@ -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<ChecksumWorkerConfig>(configuration.GetSection("ChecksumWorker"));
|
||||
services.AddHostedService<ChecksumWorker>();
|
||||
|
||||
var app = host.Build();
|
||||
|
||||
await app.RunAsync();
|
||||
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"profiles": {
|
||||
"Checksum-Test": {
|
||||
"commandName": "Project",
|
||||
"commandLineArgs": "/ChecksumWorker:Directory=C:\\Users\\stefan.atzlesberger\\Downloads /ChecksumWorker:Extension=*.ifc"
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user