From ea04c4fcec8065068eb70112fc533abf5531001b Mon Sep 17 00:00:00 2001 From: laolarou Date: Mon, 27 Dec 2021 12:29:21 -0800 Subject: [PATCH] important bug fix and performance improvement --- .../ProjBobcat/Class/Helper/CryptoHelper.cs | 6 +++--- .../ProjBobcat/Class/Helper/DownloadHelper.cs | 18 +++++------------- .../Class/Helper/HttpClientHelper.cs | 10 +++++++++- .../ProjBobcat/Class/Helper/RandomHelper.cs | 4 +--- .../Authenticator/MicrosoftAuthenticator.cs | 2 +- 5 files changed, 19 insertions(+), 21 deletions(-) diff --git a/ProjBobcat/ProjBobcat/Class/Helper/CryptoHelper.cs b/ProjBobcat/ProjBobcat/Class/Helper/CryptoHelper.cs index b306f0cd..37c94eb6 100644 --- a/ProjBobcat/ProjBobcat/Class/Helper/CryptoHelper.cs +++ b/ProjBobcat/ProjBobcat/Class/Helper/CryptoHelper.cs @@ -18,7 +18,7 @@ public static class CryptoHelper /// public static async Task ComputeFileHashAsync(string path, HashAlgorithm hashAlgorithm) { - await using var fs = File.OpenRead(path); + await using var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); var retVal = await hashAlgorithm.ComputeHashAsync(fs); return BitConverter.ToString(retVal).Replace("-", string.Empty); @@ -32,8 +32,8 @@ public static async Task ComputeFileHashAsync(string path, HashAlgorithm /// public static string ComputeFileHash(string path, HashAlgorithm hashAlgorithm) { - var bytes = File.ReadAllBytes(path); - var retVal = hashAlgorithm.ComputeHash(bytes); + using var fs = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + var retVal = hashAlgorithm.ComputeHash(fs); return BitConverter.ToString(retVal).Replace("-", string.Empty); } diff --git a/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs b/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs index 18c28ad5..20e5e423 100644 --- a/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs +++ b/ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs @@ -19,12 +19,7 @@ namespace ProjBobcat.Class.Helper; /// public static class DownloadHelper { - const int BufferSize = 1024 * 1024 * 5; - - /// - /// 获取或设置用户代理信息。 - /// - public static string Ua { get; set; } = "ProjBobcat"; + const int BufferSize = 1024; /// /// 下载线程 @@ -108,7 +103,7 @@ public static async Task DownloadData(DownloadFile downloadProperty, Cancellatio using var res = await DataClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead, ct); await using var stream = await res.Content.ReadAsStreamAsync(ct); - await using var fileToWriteTo = File.Create(filePath); + await using var fileToWriteTo = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); var responseLength = res.Content.Headers.ContentLength ?? 0; var downloadedBytesCount = 0L; @@ -319,7 +314,7 @@ public static async Task MultiPartDownloadTaskAsync(DownloadFile downloadFile, i await using (var stream = await res.Content.ReadAsStreamAsync(cts.Token)) { - await using var fileToWriteTo = File.OpenWrite(t.Item2.TempFileName); + await using var fileToWriteTo = File.Open(t.Item2.TempFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite); var buffer = new byte[BufferSize]; var sw = new Stopwatch(); @@ -352,7 +347,6 @@ public static async Task MultiPartDownloadTaskAsync(DownloadFile downloadFile, i sw.Stop(); await fileToWriteTo.FlushAsync(); - fileToWriteTo.Close(); } Interlocked.Add(ref tasksDone, 1); @@ -392,16 +386,14 @@ public static async Task MultiPartDownloadTaskAsync(DownloadFile downloadFile, i return; } - await using (var outputStream = File.Create(filePath)) + await using (var outputStream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { foreach (var inputFilePath in readRanges) { - await using var inputStream = File.OpenRead(inputFilePath.TempFileName); + await using var inputStream = File.Open(inputFilePath.TempFileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); outputStream.Seek(inputFilePath.Start, SeekOrigin.Begin); await inputStream.CopyToAsync(outputStream, cts.Token); - inputStream.Close(); - File.Delete(inputFilePath.TempFileName); } diff --git a/ProjBobcat/ProjBobcat/Class/Helper/HttpClientHelper.cs b/ProjBobcat/ProjBobcat/Class/Helper/HttpClientHelper.cs index 30ea12d3..1c5ae10e 100644 --- a/ProjBobcat/ProjBobcat/Class/Helper/HttpClientHelper.cs +++ b/ProjBobcat/ProjBobcat/Class/Helper/HttpClientHelper.cs @@ -14,6 +14,11 @@ public static class HttpClientHelper public const string HeadClientName = "HeadClient"; public const string MultiPartClientName = "MultiPartClient"; + /// + /// 获取或设置用户代理信息。 + /// + public static string Ua { get; set; } = "ProjBobcat"; + public static IHttpClientFactory HttpClientFactory { get; private set; } public static void Init() @@ -21,7 +26,10 @@ public static void Init() var arr = new[] {DefaultClientName, DataClientName, HeadClientName, MultiPartClientName}; foreach (var name in arr) ServiceHelper.ServiceCollection - .AddHttpClient(name) + .AddHttpClient(name, client => + { + client.DefaultRequestHeaders.UserAgent.ParseAdd(Ua); + }) .ConfigurePrimaryHttpMessageHandler(_ => new HttpClientHandler { AllowAutoRedirect = false diff --git a/ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs b/ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs index 0b78eb63..d74f8d7c 100644 --- a/ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs +++ b/ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs @@ -73,9 +73,7 @@ public static void Shuffle(this IList list) { n--; var k = RandomInteger(0, n); - var value = list[k]; - list[k] = list[n]; - list[n] = value; + (list[k], list[n]) = (list[n], list[k]); } } } \ No newline at end of file diff --git a/ProjBobcat/ProjBobcat/DefaultComponent/Authenticator/MicrosoftAuthenticator.cs b/ProjBobcat/ProjBobcat/DefaultComponent/Authenticator/MicrosoftAuthenticator.cs index 5dfb45c9..c0fddb8f 100644 --- a/ProjBobcat/ProjBobcat/DefaultComponent/Authenticator/MicrosoftAuthenticator.cs +++ b/ProjBobcat/ProjBobcat/DefaultComponent/Authenticator/MicrosoftAuthenticator.cs @@ -398,7 +398,7 @@ public static object ResolveMSGraphResult(string content) public static string GetLoginUri(string clientId, string redirectUri) { - return Uri.EscapeUriString("https://login.live.com/oauth20_authorize.srf" + return Uri.EscapeDataString("https://login.live.com/oauth20_authorize.srf" + $"?client_id={clientId}" + "&response_type=code" + $"&scope={MSAuthScope}"