瀏覽代碼

Refactor/rename download management

Ruben 7 月之前
父節點
當前提交
5d6484fe72

+ 3 - 2
src/PicView.Avalonia/Navigation/NavigationManager.cs

@@ -16,6 +16,7 @@ using PicView.Avalonia.WindowBehavior;
 using PicView.Core.ArchiveHandling;
 using PicView.Core.FileHandling;
 using PicView.Core.Gallery;
+using PicView.Core.Http;
 using PicView.Core.ImageDecoding;
 using PicView.Core.Localization;
 using PicView.Core.Navigation;
@@ -527,7 +528,7 @@ public static class NavigationManager
         {
             vm.PlatformService.StopTaskbarProgress();
 
-            var httpDownload = HttpNavigation.GetDownloadClient(url);
+            var httpDownload = HttpManager.GetDownloadClient(url);
             using var client = httpDownload.Client;
             client.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) =>
             {
@@ -536,7 +537,7 @@ public static class NavigationManager
                     return;
                 }
 
-                var displayProgress = HttpNavigation.GetProgressDisplay(totalFileSize, totalBytesDownloaded,
+                var displayProgress = HttpManager.GetProgressDisplay(totalFileSize, totalBytesDownloaded,
                     progressPercentage);
                 vm.Title = displayProgress;
                 vm.TitleTooltip = displayProgress;

+ 3 - 3
src/PicView.Avalonia/Update/UpdateManager.cs

@@ -7,7 +7,7 @@ using Microsoft.Win32;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.WindowBehavior;
-using PicView.Core.FileHandling;
+using PicView.Core.Http;
 #if RELEASE
 using PicView.Core.Config;
 #endif
@@ -212,7 +212,7 @@ public static class UpdateManager
     {
         try
         {
-            using var downloader = new HttpHelper.HttpClientDownloadWithProgress(url, destinationPath);
+            using var downloader = new HttpClientDownloadWithProgress(url, destinationPath);
             await downloader.StartDownloadAsync();
             return true;
         }
@@ -485,7 +485,7 @@ public static class UpdateManager
     {
         vm.PlatformService.StopTaskbarProgress();
 
-        using var downloader = new HttpHelper.HttpClientDownloadWithProgress(downloadUrl, tempPath);
+        using var downloader = new HttpClientDownloadWithProgress(downloadUrl, tempPath);
         try
         {
             downloader.ProgressChanged += (size, downloaded, percentage) =>

+ 0 - 88
src/PicView.Core/FileHandling/HttpHelper.cs

@@ -1,88 +0,0 @@
-namespace PicView.Core.FileHandling;
-
-public static class HttpHelper
-{
-    public sealed class HttpClientDownloadWithProgress(string downloadUrl, string destinationFilePath) : IDisposable
-    {
-        private HttpClient? _httpClient;
-        private bool _disposed;
-
-        public delegate void ProgressChangedHandler(long? totalFileSize, long? totalBytesDownloaded,
-            double? progressPercentage);
-
-        public event ProgressChangedHandler? ProgressChanged;
-
-        public async Task StartDownloadAsync()
-        {
-            _httpClient = new HttpClient { Timeout = TimeSpan.FromHours(6) };
-            using var response = await _httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
-                .ConfigureAwait(false);
-            await DownloadFileFromHttpResponseMessage(response).ConfigureAwait(false);
-        }
-
-        private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response)
-        {
-            response.EnsureSuccessStatusCode();
-            var totalBytes = response.Content.Headers.ContentLength;
-            await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
-            await ProcessContentStream(totalBytes, contentStream).ConfigureAwait(false);
-        }
-
-        private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream)
-        {
-            const int bufferSize = 8192;
-            var buffer = new byte[bufferSize];
-            await using var fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write,
-                FileShare.None, bufferSize, true);
-            var totalBytesRead = 0L;
-            do
-            {
-                var bytesRead = await contentStream.ReadAsync(buffer).ConfigureAwait(false);
-                if (bytesRead == 0)
-                {
-                    break;
-                }
-
-                await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
-                totalBytesRead += bytesRead;
-
-                if (!totalDownloadSize.HasValue) continue;
-                var progressPercentage = (double)totalBytesRead / totalDownloadSize.Value * 100;
-                OnProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage);
-            } while (true);
-        }
-
-        private void OnProgressChanged(long? totalDownloadSize, long totalBytesRead, double progressPercentage)
-        {
-            ProgressChanged?.Invoke(totalDownloadSize, totalBytesRead, progressPercentage);
-        }
-
-        #region IDisposable
-
-        public void Dispose()
-        {
-            Dispose(true);
-            GC.SuppressFinalize(this);
-        }
-
-        private void Dispose(bool disposing)
-        {
-            if (_disposed)
-                return;
-
-            if (disposing)
-            {
-                _httpClient?.Dispose();
-            }
-
-            _disposed = true;
-        }
-
-        ~HttpClientDownloadWithProgress()
-        {
-            Dispose(false);
-        }
-    
-        #endregion
-    }
-}

+ 91 - 0
src/PicView.Core/Http/HttpClientDownloadWithProgress.cs

@@ -0,0 +1,91 @@
+namespace PicView.Core.Http;
+
+public sealed class HttpClientDownloadWithProgress(string downloadUrl, string destinationFilePath) : IDisposable
+{
+    public delegate void ProgressChangedHandler(long? totalFileSize, long? totalBytesDownloaded,
+        double? progressPercentage);
+
+    private bool _disposed;
+    private HttpClient? _httpClient;
+
+    public event ProgressChangedHandler? ProgressChanged;
+
+    public async Task StartDownloadAsync()
+    {
+        _httpClient = new HttpClient { Timeout = TimeSpan.FromHours(6) };
+        using var response = await _httpClient.GetAsync(downloadUrl, HttpCompletionOption.ResponseHeadersRead)
+            .ConfigureAwait(false);
+        await DownloadFileFromHttpResponseMessage(response).ConfigureAwait(false);
+    }
+
+    private async Task DownloadFileFromHttpResponseMessage(HttpResponseMessage response)
+    {
+        response.EnsureSuccessStatusCode();
+        var totalBytes = response.Content.Headers.ContentLength;
+        await using var contentStream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
+        await ProcessContentStream(totalBytes, contentStream).ConfigureAwait(false);
+    }
+
+    private async Task ProcessContentStream(long? totalDownloadSize, Stream contentStream)
+    {
+        const int bufferSize = 8192;
+        var buffer = new byte[bufferSize];
+        await using var fileStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write,
+            FileShare.None, bufferSize, true);
+        var totalBytesRead = 0L;
+        do
+        {
+            var bytesRead = await contentStream.ReadAsync(buffer).ConfigureAwait(false);
+            if (bytesRead == 0)
+            {
+                break;
+            }
+
+            await fileStream.WriteAsync(buffer.AsMemory(0, bytesRead)).ConfigureAwait(false);
+            totalBytesRead += bytesRead;
+
+            if (!totalDownloadSize.HasValue)
+            {
+                continue;
+            }
+
+            var progressPercentage = (double)totalBytesRead / totalDownloadSize.Value * 100;
+            OnProgressChanged(totalDownloadSize, totalBytesRead, progressPercentage);
+        } while (true);
+    }
+
+    private void OnProgressChanged(long? totalDownloadSize, long totalBytesRead, double progressPercentage)
+    {
+        ProgressChanged?.Invoke(totalDownloadSize, totalBytesRead, progressPercentage);
+    }
+
+    #region IDisposable
+
+    public void Dispose()
+    {
+        Dispose(true);
+        GC.SuppressFinalize(this);
+    }
+
+    private void Dispose(bool disposing)
+    {
+        if (_disposed)
+        {
+            return;
+        }
+
+        if (disposing)
+        {
+            _httpClient?.Dispose();
+        }
+
+        _disposed = true;
+    }
+
+    ~HttpClientDownloadWithProgress()
+    {
+        Dispose(false);
+    }
+
+    #endregion
+}

+ 4 - 4
src/PicView.Core/Navigation/HttpNavigation.cs → src/PicView.Core/Http/HttpManager.cs

@@ -1,14 +1,14 @@
 using PicView.Core.FileHandling;
 using PicView.Core.Localization;
 
-namespace PicView.Core.Navigation;
+namespace PicView.Core.Http;
 
-public static class HttpNavigation
+public static class HttpManager
 {
     public struct HttpDownload
     {
         public string DownloadPath { get; init; }
-        public HttpHelper.HttpClientDownloadWithProgress? Client { get; init; }
+        public HttpClientDownloadWithProgress? Client { get; init; }
     }
     
     public static HttpDownload GetDownloadClient(string url)
@@ -33,7 +33,7 @@ public static class HttpNavigation
         tempPath = Path.Combine(tempPath, fileName);
         TempFileHelper.TempFilePath = string.Empty; // Reset it, since not browsing archive
 
-        var client = new HttpHelper.HttpClientDownloadWithProgress(url, tempPath);
+        var client = new HttpClientDownloadWithProgress(url, tempPath);
 
         return new HttpDownload
         {