Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
refactor(engines): share the download client and the status rules
Yavuz Mete Afşar1a09d4803fdf
diff --git a/src/Filee.App/Services/EngineDownloadService.cs b/src/Filee.App/Services/EngineDownloadService.csindex d8572332..66a522c2 100644--- a/src/Filee.App/Services/EngineDownloadService.cs+++ b/src/Filee.App/Services/EngineDownloadService.cs@@ -188,10 +188,7 @@ public sealed class EngineDownloadService_catalog = catalog;_logger = logger;_loc = loc;- // Redirects are followed by the installer (mirrors may use plain HTTP; files are verified by SHA-256).- var http = new HttpClient(new SocketsHttpHandler { AllowAutoRedirect = false }) { Timeout = Timeout.InfiniteTimeSpan };- http.DefaultRequestHeaders.UserAgent.ParseAdd($"Filee/{typeof(EngineDownloadService).Assembly.GetName().Version}");- _installer = new EngineInstaller(http, logger: logger);+ _installer = new EngineInstaller(EngineInstaller.CreateHttpClient(Timeout.InfiniteTimeSpan), logger: logger);InstallPackage = _installer.InstallAsync;Packages = EngineDownloads.Packages.Select(p => new EnginePackageState(p, loc)).ToList();if (loc is LocalizationService localization)@@ -212,23 +209,24 @@ public sealed class EngineDownloadService}/// <summary>- /// In the order the engines look: the user's own copy, Filee's download, a copy on the PC. A copy Filee didn't- /// download gets its version checked in the background (<see cref="EnginePackageState.UpdateNote"/>).+ /// The copy the engines use (<see cref="EngineInstaller.StatusOf"/>). A copy Filee didn't download gets its+ /// version checked in the background (<see cref="EnginePackageState.UpdateNote"/>)./// </summary>private EnginePackageStatus StatusOf(EnginePackageState state){- var id = state.Package.Id;- var own = EngineEnvironment.OwnCopyFolder(id);- var system = own is null && !_installer.IsInstalled(state.Package) ? EngineEnvironment.SystemCopyFolder(id) : null;- state.OwnCopyFolder = own ?? system;- state.FromSystem = system is not null;+ var copy = _installer.StatusOf(state.Package);+ state.OwnCopyFolder = copy.Folder;+ state.FromSystem = copy.FromSystem;state.UpdateNote = null;- state.IsOutdated = state.OwnCopyFolder is null && _installer.IsInstalled(state.Package) && !_installer.IsUpToDate(state.Package);- if (state.OwnCopyFolder is { } folder)+ state.IsOutdated = copy.Outdated;+ if (copy.Folder is { } folder)_ = CheckVersionAsync(state, folder);- return state.OwnCopyFolder is not null ? EnginePackageStatus.OwnCopy- : _installer.IsInstalled(state.Package) ? EnginePackageStatus.Installed- : EnginePackageStatus.NotInstalled;+ return copy.Status switch+ {+ EngineCopyStatus.OwnCopy => EnginePackageStatus.OwnCopy,+ EngineCopyStatus.Installed => EnginePackageStatus.Installed,+ _ => EnginePackageStatus.NotInstalled,+ };}/// <summary>Runs the copy's version check off the UI thread (it starts the program once) and notes an old one.</summary>diff --git a/src/Filee.Engines/Infrastructure/EngineInstaller.cs b/src/Filee.Engines/Infrastructure/EngineInstaller.csindex 024a8434..06c30281 100644--- a/src/Filee.Engines/Infrastructure/EngineInstaller.cs+++ b/src/Filee.Engines/Infrastructure/EngineInstaller.cs@@ -33,6 +33,23 @@ public enum EngineInstallStage/// <param name="BytesTotal">Download size of the components being installed.</param>public readonly record struct EngineInstallProgress(EngineInstallStage Stage, string ComponentId, double Fraction, long BytesDone = 0, long BytesTotal = 0);+/// <summary>Which copy of a package the engines use, see <see cref="EngineInstaller.StatusOf"/>.</summary>+public enum EngineCopyStatus+{+ NotInstalled,+ /// <summary>Filee's download.</summary>+ Installed,+ /// <summary>The user's own copy (Settings → Engines) or one found on the PC; nothing of Filee's is used.</summary>+ OwnCopy,+}++/// <summary>The copy of a package the engines use, as Settings → Engines and 'filee engines' show it.</summary>+/// <param name="Folder">Folder of the copy in use while the status is <see cref="EngineCopyStatus.OwnCopy"/>.</param>+/// <param name="FromSystem">That copy was found on the PC (PATH, Program Files), not chosen by the user.</param>+/// <param name="Outdated">Filee's download is older than the version engines.json pins.</param>+public sealed record EngineCopy(EngineCopyStatus Status, string? Folder, bool FromSystem, bool Outdated);++/// <summary>Installs and removes <see cref="EnginePackage"/>s.</summary>public sealed class EngineInstaller{@@ -54,6 +71,39 @@ public sealed class EngineInstaller_logger = logger ?? NullLogger.Instance;}+ /// <summary>+ /// The client to create an installer with: automatic redirects off, because the installer follows them itself+ /// (mirrors may use plain HTTP; every file is checked against its SHA-256), and Filee's user agent.+ /// </summary>+ public static HttpClient CreateHttpClient(TimeSpan timeout)+ {+ var http = new HttpClient(new SocketsHttpHandler { AllowAutoRedirect = false }) { Timeout = timeout };+ http.DefaultRequestHeaders.UserAgent.ParseAdd($"Filee/{typeof(EngineInstaller).Assembly.GetName().Version}");+ return http;+ }++ /// <summary>Folder that receives one sub folder per engine.</summary>+ public string Root => _root;++ /// <summary>The downloads this installer knows: engines.json for this computer unless a test passed its own.</summary>+ public IReadOnlyDictionary<string, EngineComponent> Components => _components;++ /// <summary>+ /// The copy the engines use, in the order they look: the user's own copy, Filee's download, a copy on the PC+ /// (only looked for when neither of the others is there).+ /// </summary>+ public EngineCopy StatusOf(EnginePackage package)+ {+ var installed = IsInstalled(package);+ var own = EngineEnvironment.OwnCopyFolder(package.Id);+ var system = own is null && !installed ? EngineEnvironment.SystemCopyFolder(package.Id) : null;+ var folder = own ?? system;+ var status = folder is not null ? EngineCopyStatus.OwnCopy+ : installed ? EngineCopyStatus.Installed+ : EngineCopyStatus.NotInstalled;+ return new EngineCopy(status, folder, system is not null, folder is null && installed && !IsUpToDate(package));+ }+/// <summary>True when every component is installed (any version).</summary>public bool IsInstalled(EnginePackage package) => package.Components.All(c => _components.ContainsKey(c) && Marker(c) is not null);diff --git a/src/Filee.Engines/Ocr/OcrLanguages.cs b/src/Filee.Engines/Ocr/OcrLanguages.csindex 3c3991c4..297ac8f3 100644--- a/src/Filee.Engines/Ocr/OcrLanguages.cs+++ b/src/Filee.Engines/Ocr/OcrLanguages.cs@@ -80,13 +80,7 @@ public static class OcrLanguagesprivate static readonly SemaphoreSlim OneAtATime = new(1, 1);- /// <summary>Redirects are followed by the installer, which checks every file against its SHA-256.</summary>- private static readonly Lazy<HttpClient> Http = new(() =>- {- var http = new HttpClient(new SocketsHttpHandler { AllowAutoRedirect = false }) { Timeout = TimeSpan.FromMinutes(10) };- http.DefaultRequestHeaders.UserAgent.ParseAdd($"Filee/{typeof(OcrLanguages).Assembly.GetName().Version}");- return http;- });+ private static readonly Lazy<HttpClient> Http = new(() => EngineInstaller.CreateHttpClient(TimeSpan.FromMinutes(10)));private static EnginePackage PackageOf(string code) => new("ocr-" + code, [code], Components[code].Size, ["tesseract"]);diff --git a/tests/Filee.Engines.Tests/EngineInstallerTests.cs b/tests/Filee.Engines.Tests/EngineInstallerTests.csindex a81f6bca..cce5d7f2 100644--- a/tests/Filee.Engines.Tests/EngineInstallerTests.cs+++ b/tests/Filee.Engines.Tests/EngineInstallerTests.cs@@ -133,6 +133,25 @@ public sealed class EngineInstallerTests : IDisposableAssert.False(Directory.Exists(Path.Combine(_root, "tool")));}+ [Fact]+ public async Task The_copy_in_use_is_Filees_download_until_it_is_removed()+ {+ var archive = Zip(("tool.exe", "binary"));+ var component = Component("tool", "zip", archive);+ var installer = Installer(new FakeServer(new() { [component.Url] = archive }), component);+ var package = new EnginePackage("tool", ["tool"], 1000, ["tool"]);+ Assert.Equal(new EngineCopy(EngineCopyStatus.NotInstalled, null, false, false), installer.StatusOf(package));++ await installer.InstallAsync(package, null, TestContext.Current.CancellationToken);+ Assert.Equal(new EngineCopy(EngineCopyStatus.Installed, null, false, false), installer.StatusOf(package));++ var newer = Installer(new FakeServer(new()), component with { Version = "2.0", Sha256 = new string('1', 64) });+ Assert.Equal(new EngineCopy(EngineCopyStatus.Installed, null, false, true), newer.StatusOf(package));++ installer.Uninstall(package);+ Assert.Equal(EngineCopyStatus.NotInstalled, installer.StatusOf(package).Status);+ }+[Fact]public void Transfer_rate_averages_over_the_last_seconds_and_estimates_the_time_left(){