From f96ee665b5c8f093b4cd29790a4d9a19024574cf Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Wed, 3 Mar 2021 17:01:32 +0800 Subject: [PATCH] http stream support --- .../WindowsDesktopPlatformServiceImpl.cs | 2 +- .../Implementation/HttpServiceImpl.cs | 27 +++++++---- .../Models/OpenSourceLibrary.Values.cs | 3 +- ...n.SteamTools.Tools.Win7Troubleshoot.csproj | 8 ++++ .../Application/Models/JWTEntity.cs | 45 +++++++++++++++++++ 5 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 System.Common.CoreLib/Application/Models/JWTEntity.cs diff --git a/System.Application.SteamTools.Client.Desktop.Windows/Application/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs b/System.Application.SteamTools.Client.Desktop.Windows/Application/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs index d4fc00a6c..b3cfbad9b 100644 --- a/System.Application.SteamTools.Client.Desktop.Windows/Application/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs +++ b/System.Application.SteamTools.Client.Desktop.Windows/Application/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs @@ -127,7 +127,7 @@ namespace System.Application.Services.Implementation static string? GetFullPath(string s) { - if (!string.IsNullOrWhiteSpace(s) && File.Exists(s)) + if (!string.IsNullOrWhiteSpace(s)) { return Path.GetFullPath(s); } diff --git a/System.Application.SteamTools.Client.Desktop/Services/Implementation/HttpServiceImpl.cs b/System.Application.SteamTools.Client.Desktop/Services/Implementation/HttpServiceImpl.cs index db6dea875..8c919d5f1 100644 --- a/System.Application.SteamTools.Client.Desktop/Services/Implementation/HttpServiceImpl.cs +++ b/System.Application.SteamTools.Client.Desktop/Services/Implementation/HttpServiceImpl.cs @@ -15,22 +15,25 @@ namespace System.Application.Services.Implementation const string Accept = MediaTypeNames.JSON; readonly JsonSerializer jsonSerializer = new JsonSerializer(); - public HttpServiceImpl(ILogger logger, IHttpPlatformHelper http_helper, IHttpClientFactory clientFactory) : base(logger, http_helper, clientFactory) + public HttpServiceImpl(ILoggerFactory loggerFactory, IHttpPlatformHelper http_helper, IHttpClientFactory clientFactory) : base(loggerFactory.CreateLogger(TAG), http_helper, clientFactory) { } public async Task GetAsync(string requestUri, CancellationToken cancellationToken) where T : notnull { + HttpRequestMessage? request = null; + HttpResponseMessage? response = null; + bool notDispose = false; try { - using var request = new HttpRequestMessage(HttpMethod.Get, requestUri); + request = new HttpRequestMessage(HttpMethod.Get, requestUri); request.Headers.Accept.ParseAdd(Accept); request.Headers.UserAgent.ParseAdd(http_helper.UserAgent); var client = CreateClient(); - using var response = await client.SendAsync(request, - HttpCompletionOption.ResponseHeadersRead, - cancellationToken) - .ConfigureAwait(false); + response = await client.SendAsync(request, + HttpCompletionOption.ResponseHeadersRead, + cancellationToken) + .ConfigureAwait(false); if (response.IsSuccessStatusCode) { if (response.Content != null) @@ -46,8 +49,8 @@ namespace System.Application.Services.Implementation } else if (rspContentClrType == typeof(Stream)) { - // 因为上面用了 using 释放,所以不支持流的形式 - throw new NotSupportedException(); + notDispose = true; + return (T)(object)await response.Content.ReadAsStreamAsync(); } var mime = response.Content.Headers.ContentType?.MediaType ?? Accept; switch (mime) @@ -83,6 +86,14 @@ namespace System.Application.Services.Implementation { Log.Error(TAG, e, "GetAsync Fail."); } + finally + { + if (!notDispose) + { + request?.Dispose(); + response?.Dispose(); + } + } return default; } } diff --git a/System.Application.SteamTools.Client/Models/OpenSourceLibrary.Values.cs b/System.Application.SteamTools.Client/Models/OpenSourceLibrary.Values.cs index c10761bc4..f22c41de1 100644 --- a/System.Application.SteamTools.Client/Models/OpenSourceLibrary.Values.cs +++ b/System.Application.SteamTools.Client/Models/OpenSourceLibrary.Values.cs @@ -9,7 +9,8 @@ namespace System.Application.Models { try { - return Serializable.DMP>(SR.OpenSourceLibraryList) ?? new List(); + var list = Serializable.DMP>(SR.OpenSourceLibraryList) ?? new List(); + return list; } catch { diff --git a/System.Application.SteamTools.Tools.Win7Troubleshoot/System.Application.SteamTools.Tools.Win7Troubleshoot.csproj b/System.Application.SteamTools.Tools.Win7Troubleshoot/System.Application.SteamTools.Tools.Win7Troubleshoot.csproj index 57e08c636..55a4c6de4 100644 --- a/System.Application.SteamTools.Tools.Win7Troubleshoot/System.Application.SteamTools.Tools.Win7Troubleshoot.csproj +++ b/System.Application.SteamTools.Tools.Win7Troubleshoot/System.Application.SteamTools.Tools.Win7Troubleshoot.csproj @@ -43,4 +43,12 @@ SR.Designer.cs + + + + + steam++.win7.visualelementsmanifest.xml + PreserveNewest + + \ No newline at end of file diff --git a/System.Common.CoreLib/Application/Models/JWTEntity.cs b/System.Common.CoreLib/Application/Models/JWTEntity.cs new file mode 100644 index 000000000..143c9a11c --- /dev/null +++ b/System.Common.CoreLib/Application/Models/JWTEntity.cs @@ -0,0 +1,45 @@ +using MPKey = MessagePack.KeyAttribute; +using MPObj = MessagePack.MessagePackObjectAttribute; +using N_JsonProperty = Newtonsoft.Json.JsonPropertyAttribute; +using S_JsonProperty = System.Text.Json.Serialization.JsonPropertyNameAttribute; + +namespace System.Application.Models +{ + /// + /// JWT 值 + /// + [MPObj] + [Serializable] + public sealed class JWTEntity : IExplicitHasValue + { + /// + /// 凭证有效期 + /// + [MPKey(0)] + [N_JsonProperty("0")] + [S_JsonProperty("0")] + public DateTimeOffset ExpiresIn { get; set; } + + /// + /// 当前凭证 + /// + [MPKey(1)] + [N_JsonProperty("1")] + [S_JsonProperty("1")] + public string? AccessToken { get; set; } + + /// + /// 刷新凭证 + /// + [MPKey(2)] + [N_JsonProperty("2")] + [S_JsonProperty("2")] + public string? RefreshToken { get; set; } + + bool IExplicitHasValue.ExplicitHasValue() + { + return !string.IsNullOrEmpty(AccessToken) && + !string.IsNullOrEmpty(RefreshToken); + } + } +} \ No newline at end of file