Skip to content

Commit

Permalink
important bug fix and performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
laolarou726 committed Dec 27, 2021
1 parent eac366f commit ea04c4f
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 21 deletions.
6 changes: 3 additions & 3 deletions ProjBobcat/ProjBobcat/Class/Helper/CryptoHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public static class CryptoHelper
/// <returns></returns>
public static async Task<string> 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);
Expand All @@ -32,8 +32,8 @@ public static async Task<string> ComputeFileHashAsync(string path, HashAlgorithm
/// <returns></returns>
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);
}
Expand Down
18 changes: 5 additions & 13 deletions ProjBobcat/ProjBobcat/Class/Helper/DownloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,7 @@ namespace ProjBobcat.Class.Helper;
/// </summary>
public static class DownloadHelper
{
const int BufferSize = 1024 * 1024 * 5;

/// <summary>
/// 获取或设置用户代理信息。
/// </summary>
public static string Ua { get; set; } = "ProjBobcat";
const int BufferSize = 1024;

/// <summary>
/// 下载线程
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -352,7 +347,6 @@ public static async Task MultiPartDownloadTaskAsync(DownloadFile downloadFile, i
sw.Stop();
await fileToWriteTo.FlushAsync();
fileToWriteTo.Close();
}
Interlocked.Add(ref tasksDone, 1);
Expand Down Expand Up @@ -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);
}

Expand Down
10 changes: 9 additions & 1 deletion ProjBobcat/ProjBobcat/Class/Helper/HttpClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,22 @@ public static class HttpClientHelper
public const string HeadClientName = "HeadClient";
public const string MultiPartClientName = "MultiPartClient";

/// <summary>
/// 获取或设置用户代理信息。
/// </summary>
public static string Ua { get; set; } = "ProjBobcat";

public static IHttpClientFactory HttpClientFactory { get; private set; }

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
Expand Down
4 changes: 1 addition & 3 deletions ProjBobcat/ProjBobcat/Class/Helper/RandomHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,7 @@ public static void Shuffle<T>(this IList<T> 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]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ public static object ResolveMSGraphResult<T>(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}"
Expand Down

0 comments on commit ea04c4f

Please sign in to comment.