From 69db90e739e7dfdc14a9a4cf7a810b91f0001a50 Mon Sep 17 00:00:00 2001 From: mormegil Date: Fri, 30 Aug 2024 16:09:18 +0200 Subject: [PATCH] Cleanup, update disabling --- .../Engine/Djr/DataDownloader.cs | 36 +++++++++---------- KdyPojedeVlak.Web/Startup.cs | 3 +- .../appsettings.Development.json | 1 + 3 files changed, 21 insertions(+), 19 deletions(-) diff --git a/KdyPojedeVlak.Web/Engine/Djr/DataDownloader.cs b/KdyPojedeVlak.Web/Engine/Djr/DataDownloader.cs index b9eb37e..c39d7bf 100644 --- a/KdyPojedeVlak.Web/Engine/Djr/DataDownloader.cs +++ b/KdyPojedeVlak.Web/Engine/Djr/DataDownloader.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Globalization; using System.IO; using System.Linq; using System.Security.Cryptography; @@ -10,33 +9,34 @@ namespace KdyPojedeVlak.Web.Engine.Djr { - public class DataDownloader + public partial class DataDownloader { - private const string clientName = "KdyPojedeVlak/CoreFTP"; - private static readonly Uri serverBaseUri = new Uri(@"ftp://ftp.cisjr.cz/draha/celostatni/szdc/"); + private const string ClientName = "KdyPojedeVlak/CoreFTP"; + private static readonly Uri serverBaseUri = new(@"ftp://ftp.cisjr.cz/draha/celostatni/szdc/"); + private const int BuffSize = 10240; - private static readonly Regex reFilename = new Regex(@"^([^.]+)\.(XML\.)?ZIP$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline); + private static readonly Regex reFilename = RegexFilename(); + private static readonly Regex reDirectory = RegexDirectory(); + private static readonly Regex reSubdirectory = RegexSubdirectory(); - private static readonly Regex reDirectory = new Regex(@"^2[0-9]{3}$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline); + [GeneratedRegex(@"^([^.]+)\.(XML\.)?ZIP$", RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.CultureInvariant)] + private static partial Regex RegexFilename(); - private static readonly Regex reSubdirectory = new Regex(@"^2[0-9]{3}-[0-9]{2}$", - RegexOptions.CultureInvariant | RegexOptions.IgnoreCase | RegexOptions.Singleline); + [GeneratedRegex(@"^2[0-9]{3}$", RegexOptions.Singleline | RegexOptions.CultureInvariant)] + private static partial Regex RegexDirectory(); - private const int BUFF_SIZE = 10240; + [GeneratedRegex(@"^2[0-9]{3}-[0-9]{2}$", RegexOptions.Singleline | RegexOptions.CultureInvariant)] + private static partial Regex RegexSubdirectory(); private FtpClient ftp; - public bool ShouldExtractZip => false; - public async Task Connect() { if (ftp != null) throw new InvalidOperationException("Already connected"); ftp = new FtpClient(new FtpClientConfiguration { Host = serverBaseUri.GetLeftPart(UriPartial.Authority) }); await ftp.LoginAsync(); - await ftp.SetClientName(clientName); + await ftp.SetClientName(ClientName); await ftp.ChangeWorkingDirectoryAsync(serverBaseUri.AbsolutePath); } @@ -68,7 +68,7 @@ async Task AddListOfFilesAvailableInDir(Dictionary dictionary, str .Where(f => f.Match.Success) .Select(f => f.Directory.Name) .ToList(); - + var results = new Dictionary(); foreach (var dir in directories) { @@ -86,7 +86,7 @@ async Task AddListOfFilesAvailableInDir(Dictionary dictionary, str await AddListOfFilesAvailableInDir(results, dir + "/" + subdir); await ftp.ChangeWorkingDirectoryAsync(".."); } - + await AddListOfFilesAvailableInDir(results, dir); await ftp.ChangeWorkingDirectoryAsync(".."); @@ -135,9 +135,9 @@ public async Task GetLatestVersionAvailable() using (var receiveStream = await ftp.OpenFileReadStreamAsync(fileName)) { using (var storeStream = new FileStream(destinationFilename, FileMode.Create, FileAccess.Write, - FileShare.Read)) + FileShare.Read)) { - var buffer = new byte[BUFF_SIZE]; + var buffer = new byte[BuffSize]; while (true) { var read = await receiveStream.ReadAsync(buffer, 0, buffer.Length); diff --git a/KdyPojedeVlak.Web/Startup.cs b/KdyPojedeVlak.Web/Startup.cs index 28b1134..461ee36 100644 --- a/KdyPojedeVlak.Web/Startup.cs +++ b/KdyPojedeVlak.Web/Startup.cs @@ -1,5 +1,6 @@ using System; using System.Diagnostics; +using System.IO; using KdyPojedeVlak.Web.Engine; using KdyPojedeVlak.Web.Engine.DbStorage; using KdyPojedeVlak.Web.Engine.Djr; @@ -148,7 +149,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env) throw; } - if (EnableUpdates) UpdateManager.Initialize(@"App_Data\cisjrdata", serviceScopeFactory); + if (EnableUpdates && Configuration["DisableUpdates"] == null) UpdateManager.Initialize(Path.Combine("App_Data", "cisjrdata"), serviceScopeFactory); } } } \ No newline at end of file diff --git a/KdyPojedeVlak.Web/appsettings.Development.json b/KdyPojedeVlak.Web/appsettings.Development.json index b9e7512..1c06a4f 100644 --- a/KdyPojedeVlak.Web/appsettings.Development.json +++ b/KdyPojedeVlak.Web/appsettings.Development.json @@ -1,5 +1,6 @@ { "AdminPassword": "pass", + "DisableUpdates": "disable", "Logging": { "LogLevel": { "Microsoft.EntityFrameworkCore.Database.Command": "Information"