From 6f071cf060cfa4827fbb012b772c4f1736a557fa Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Thu, 28 Jul 2022 22:40:59 +0800 Subject: [PATCH 01/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=B8=AA=E4=BA=BA=E8=B5=84=E6=96=99=E6=8E=A5=E5=8F=A3=20?= =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=B8=AA=E4=BA=BA=E8=B5=84=E6=96=99=E5=B1=95?= =?UTF-8?q?=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../HttpModule/Result/UserInfoDto.cs | 80 +++++++++++++++++++ .../Apis/UserInfoApi.cs | 23 +++++- .../Helpers/CommonHelper.cs | 25 ++++++ .../Pages/PersonalCenter.razor | 60 +++++++------- .../Pages/PersonalCenter.razor.cs | 20 ++++- 5 files changed, 175 insertions(+), 33 deletions(-) create mode 100644 src/CloudStoage.Domain/HttpModule/Result/UserInfoDto.cs create mode 100644 src/CloudStorage.Applications/Helpers/CommonHelper.cs diff --git a/src/CloudStoage.Domain/HttpModule/Result/UserInfoDto.cs b/src/CloudStoage.Domain/HttpModule/Result/UserInfoDto.cs new file mode 100644 index 0000000..3ffa7e1 --- /dev/null +++ b/src/CloudStoage.Domain/HttpModule/Result/UserInfoDto.cs @@ -0,0 +1,80 @@ +using CloudStorage.Domain.Shared; + +namespace CloudStoage.Domain.HttpModule.Result; + +public class UserInfoDto +{ + public Guid Id { get; set; } + + /// + /// 账号 + /// + public string? Account { get; set; } + + /// + /// 密码 + /// + public string? Password { get; set; } + + /// + /// 昵称 + /// + public string? Name { get; set; } + + /// + /// 简介 + /// + public string? BriefIntroduction { get; set; } + + /// + /// 微信openid + /// + public string? WeChatOpenId { get; set; } + + /// + /// 头像 + /// + public string? HeadPortraits { get; set; } + + /// + /// 性别 + /// + public SexType Sex { get; set; } + + /// + /// 状态 + /// + public UserStatus Status { get; set; } + + /// + /// 服务器文件 + /// + public string? CloudStorageRoot { get; set; } + + public DateTime CreationTime { get; } + + /// + /// 用户总大小 + /// + public long TotalSize { get; set; } + + /// + /// 已经使用大小 + /// + public long UsedSize { get; set; } = 0; + + /// + /// 使用占比 + /// + public int Percentage + { + get + { + if(UsedSize!=0 && TotalSize != 0) + { + return (int)((UsedSize / TotalSize) * 100); + } + return 0; + } + } +} diff --git a/src/CloudStorage.Applications/Apis/UserInfoApi.cs b/src/CloudStorage.Applications/Apis/UserInfoApi.cs index dab4e99..86009a5 100644 --- a/src/CloudStorage.Applications/Apis/UserInfoApi.cs +++ b/src/CloudStorage.Applications/Apis/UserInfoApi.cs @@ -1,9 +1,14 @@ -using Token.Module.Dependencys; +using CloudStoage.Domain.HttpModule; +using CloudStoage.Domain.HttpModule.Result; +using Newtonsoft.Json; +using Token.Module.Dependencys; namespace CloudStorage.Applications.Apis; -public class UserInfoApi : IScopedDependency +public class UserInfoApi : IScopedDependency { + private const string Name = "api/userinfo"; + private readonly IHttpClientFactory httpClientFactory; public UserInfoApi(IHttpClientFactory httpClientFactory) @@ -11,4 +16,18 @@ public class UserInfoApi : IScopedDependency this.httpClientFactory = httpClientFactory; } + /// + /// 获取用户基本信息 + /// + /// + public async Task GetAsync() + { + var httpclient = httpClientFactory.CreateClient(string.Empty); + + var data =await httpclient.GetStringAsync(Name); + + var result = JsonConvert.DeserializeObject>(data); + + return result.Data; + } } diff --git a/src/CloudStorage.Applications/Helpers/CommonHelper.cs b/src/CloudStorage.Applications/Helpers/CommonHelper.cs new file mode 100644 index 0000000..6b1ef14 --- /dev/null +++ b/src/CloudStorage.Applications/Helpers/CommonHelper.cs @@ -0,0 +1,25 @@ +using Token.Module.Dependencys; + +namespace CloudStorage.Applications.Helpers; + +public class CommonHelper : IScopedDependency +{ + public string GetFileSize(long? size) + { + if (size == null) + return ""; + + var num = 1024.00; //byte + + if (size < num) + return size + "B"; + if (size < Math.Pow(num, 2)) + return ((long)size / num).ToString("f2") + "K"; //kb + if (size < Math.Pow(num, 3)) + return ((long)size / Math.Pow(num, 2)).ToString("f2") + "M"; //M + if (size < Math.Pow(num, 4)) + return ((long)size / Math.Pow(num, 3)).ToString("f2") + "G"; //G + + return ((long)size / Math.Pow(num, 4)).ToString("f2") + "T"; //T + } +} diff --git a/src/CloudStorage.Layou/Pages/PersonalCenter.razor b/src/CloudStorage.Layou/Pages/PersonalCenter.razor index f310e16..101a76f 100644 --- a/src/CloudStorage.Layou/Pages/PersonalCenter.razor +++ b/src/CloudStorage.Layou/Pages/PersonalCenter.razor @@ -6,57 +6,57 @@ + Src="@UserInfo.HeadPortraits">
- 可用空间440MB/100GB -
+ 已使用@(CommonHelper.GetFileSize(UserInfo?.UsedSize))/@(CommonHelper.GetFileSize(UserInfo?.TotalSize)) + -
+
- - - - - + + + + - - 编辑个人资料 - - - + + 编辑个人资料 + + + - + - 设置 + 设置 - + - - - - + + + + - - 退出登录 - - - - + + 退出登录 + + + + +
- diff --git a/src/CloudStorage.Layou/Pages/PersonalCenter.razor.cs b/src/CloudStorage.Layou/Pages/PersonalCenter.razor.cs index 76b5610..0dfb2c6 100644 --- a/src/CloudStorage.Layou/Pages/PersonalCenter.razor.cs +++ b/src/CloudStorage.Layou/Pages/PersonalCenter.razor.cs @@ -1,14 +1,32 @@ -using Microsoft.AspNetCore.Components.Web; +using CloudStoage.Domain.HttpModule.Result; +using CloudStorage.Applications.Helpers; namespace CloudStorage.Layou.Pages; partial class PersonalCenter { + private UserInfoDto? UserInfo { get; set; } =new UserInfoDto(); [Inject] public NavigationManager? Navigation { get; set; } + [Inject] + public CommonHelper CommonHelper { get; set; } + + [Inject] + public UserInfoApi UserInfoApi { get; set; } + private void OnLogoutClick(MouseEventArgs args) { Navigation?.NavigateTo("/login"); } + + protected override async Task OnInitializedAsync() + { + await GetUserInfoAsync(); + } + + private async Task GetUserInfoAsync() + { + UserInfo = await UserInfoApi.GetAsync(); + } } -- Gitee From 352aabbf35bd7d0bf710f01a920c0202aa7cbc9f Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Thu, 28 Jul 2022 23:46:20 +0800 Subject: [PATCH 02/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=BC=B9=E5=87=BA=E6=9C=AA=E5=85=B3=E9=97=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Components/FileFunction.razor.cs | 2 +- .../Components/Storagefile.razor.cs | 33 ++++++++++++++----- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/CloudStorage.Layou/Components/FileFunction.razor.cs b/src/CloudStorage.Layou/Components/FileFunction.razor.cs index 4ef7210..61901cf 100644 --- a/src/CloudStorage.Layou/Components/FileFunction.razor.cs +++ b/src/CloudStorage.Layou/Components/FileFunction.razor.cs @@ -21,6 +21,6 @@ partial class FileFunction { await StorageApi.DeleteStorageAsync(StorageId); - await DistributedEventBus.PublishAsync(nameof(Storagefile.HasFybctuib), false); + await DistributedEventBus.PublishAsync("HasFybctuib", false); } } diff --git a/src/CloudStorage.Layou/Components/Storagefile.razor.cs b/src/CloudStorage.Layou/Components/Storagefile.razor.cs index 796ee3b..140db18 100644 --- a/src/CloudStorage.Layou/Components/Storagefile.razor.cs +++ b/src/CloudStorage.Layou/Components/Storagefile.razor.cs @@ -1,13 +1,24 @@ using CloudStoage.Domain.HttpModule.Result; +using CloudStorage.Layou.Pages; using Token.EventBus; namespace CloudStorage.Layou.Components; partial class Storagefile { + private bool hasFybctuib; + [Parameter] - public bool HasFybctuib { get; set; } + public bool HasFybctuib + { + get { return hasFybctuib; } + set + { + hasFybctuib = value; + ValueChange.InvokeAsync(value); + } + } [Parameter] public EventCallback ValueChange { get; set; } @@ -24,6 +35,9 @@ partial class Storagefile [Inject] public IDistributedEventBus DistributedEventBus { get; set; } + [Inject] + public IDistributedEventBus StringDstributedEventBus { get; set; } + /// /// Ϣ /// @@ -31,14 +45,16 @@ partial class Storagefile public override async Task SetParametersAsync(ParameterView parameters) { - - parameters.TryGetValue(nameof(StorageId), out Guid? storageId); if (storageId == null) return; parameters.TryGetValue(nameof(HasFybctuib), out bool hasFybctuib); + if (hasFybctuib == HasFybctuib) + { + return; + } HasFybctuib = hasFybctuib; parameters.TryGetValue(nameof(ValueChange), out EventCallback valueChange); @@ -47,22 +63,21 @@ partial class Storagefile await GetStorageAsync(storageId); - StateHasChanged(); - } - - protected override async Task OnInitializedAsync() - { await HasFybctuibAsync(); + + StateHasChanged(); } private async Task HasFybctuibAsync() { - await DistributedEventBus.Subscribe(nameof(HasFybctuib), (data) => + await DistributedEventBus.Subscribe("HasFybctuib", async (data) => { var result = data as bool?; if (result != null) { HasFybctuib = (bool)result; + StateHasChanged(); + await StringDstributedEventBus.PublishAsync(nameof(Storages),"ɾļɹ"); } }); } -- Gitee From c851aec470f447778603135926bbfefb87bd0414 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Fri, 29 Jul 2022 00:55:12 +0800 Subject: [PATCH 03/13] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=9B=BF=E6=8D=A2?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CloudStorage.Applications.csproj | 1 + .../CloudStorageApplicationsModule.cs | 2 +- .../Helpers/HtttpClientHelper.cs | 49 +++++++++++++++++++ src/CloudStorage.Domain.Shared/Constant.cs | 6 +++ .../Components/MenuList.razor | 1 + .../Pages/Storages.razor.cs | 21 ++++++-- src/CloudStorage.Layou/Pages/Uploading.razor | 7 +++ 7 files changed, 83 insertions(+), 4 deletions(-) create mode 100644 src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs create mode 100644 src/CloudStorage.Layou/Pages/Uploading.razor diff --git a/src/CloudStorage.Applications/CloudStorage.Applications.csproj b/src/CloudStorage.Applications/CloudStorage.Applications.csproj index c9d7065..6c79903 100644 --- a/src/CloudStorage.Applications/CloudStorage.Applications.csproj +++ b/src/CloudStorage.Applications/CloudStorage.Applications.csproj @@ -19,6 +19,7 @@ + diff --git a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs index d8701d8..b0157dc 100644 --- a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs +++ b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs @@ -14,7 +14,7 @@ public class CloudStorageApplicationsModule : TokenModule .ConfigureHttpClient((services, x) => { var status = services.GetService(); - x.DefaultRequestHeaders.Add("Authorization", $"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJ1c2VyIjoie1wiQWNjb3VudFwiOlwiYWRtaW5cIixcIlBhc3N3b3JkXCI6XCJhZG1pblwiLFwiTmFtZVwiOlwiYWRtaW5cIixcIkJyaWVmSW50cm9kdWN0aW9uXCI6bnVsbCxcIldlQ2hhdE9wZW5JZFwiOm51bGwsXCJIZWFkUG9ydHJhaXRzXCI6bnVsbCxcIlNleFwiOjAsXCJTdGF0dXNcIjowLFwiQ2xvdWRTdG9yYWdlUm9vdFwiOlwiLi93d3dyb290L0Nsb3VkU3RvcmFnZVxcXFxhYzRkZWRmYTFlYmU0YzNhOWJmY2JmZGQ2NWQxZjNkMlwiLFwiSXNEZWxldGVkXCI6ZmFsc2UsXCJDcmVhdGlvblRpbWVcIjpcIjAwMDEtMDEtMDFUMDA6MDA6MDBcIixcIkV4dHJhUHJvcGVydGllc1wiOnt9LFwiQ29uY3VycmVuY3lTdGFtcFwiOlwiZjQwMGJiZThjZDI0NDlkNWEyNTFjZmMxM2FmMzEyOGNcIixcIklkXCI6XCI0OTU4MWRkNC0yYzM5LTQ2NzAtOTE5Yi01MjMyY2UwZTllM2VcIn0iLCJpZCI6IjQ5NTgxZGQ0LTJjMzktNDY3MC05MTliLTUyMzJjZTBlOWUzZSIsImV4cCI6MTc2MjEzNjE0OCwiaXNzIjoidG9rZW5odS50b3AiLCJhdWQiOiJ0b2tlbmh1LnRvcCJ9.D-EpFpkPvg8o4W2OF0ZbiKCv2mqqErfCmJ0bN8KJYD8"); + x.DefaultRequestHeaders.Add(Constant.Authorization, $"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJ1c2VyIjoie1wiQWNjb3VudFwiOlwiYWRtaW5cIixcIlBhc3N3b3JkXCI6XCJhZG1pblwiLFwiTmFtZVwiOlwiYWRtaW5cIixcIkJyaWVmSW50cm9kdWN0aW9uXCI6bnVsbCxcIldlQ2hhdE9wZW5JZFwiOm51bGwsXCJIZWFkUG9ydHJhaXRzXCI6XCJodHRwczovL3RzMS5jbi5tbS5iaW5nLm5ldC90aD9pZD1PSVAtQy5JYmdZY2JDSGZVVXRmd2VHTUtBalR3QUFBQSZ3PTI1MCZoPTI1MCZjPTgmcnM9MSZxbHQ9OTAmbz02JnBpZD0zLjEmcm09MlwiLFwiU2V4XCI6MCxcIlN0YXR1c1wiOjAsXCJDbG91ZFN0b3JhZ2VSb290XCI6XCIuL3d3d3Jvb3QvQ2xvdWRTdG9yYWdlL2FjNGRlZGZhMWViZTRjM2E5YmZjYmZkZDY1ZDFmM2QyXCIsXCJJc0RlbGV0ZWRcIjpmYWxzZSxcIkNyZWF0aW9uVGltZVwiOlwiMDAwMS0wMS0wMVQwMDowMDowMFwiLFwiRXh0cmFQcm9wZXJ0aWVzXCI6e30sXCJDb25jdXJyZW5jeVN0YW1wXCI6XCJmNDAwYmJlOGNkMjQ0OWQ1YTI1MWNmYzEzYWYzMTI4Y1wiLFwiSWRcIjpcIjQ5NTgxZGQ0LTJjMzktNDY3MC05MTliLTUyMzJjZTBlOWUzZVwifSIsImlkIjoiNDk1ODFkZDQtMmMzOS00NjcwLTkxOWItNTIzMmNlMGU5ZTNlIiwiZXhwIjoxNzYyMjI2NDM0LCJpc3MiOiJ0b2tlbmh1LnRvcCIsImF1ZCI6InRva2VuaHUudG9wIn0.Qga-H-eKWrguw6ojk3ps1lW0sdx2XVxfmlZPvtjAWSg"); x.BaseAddress = new Uri(Constant.Api); }) diff --git a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs new file mode 100644 index 0000000..7ba648c --- /dev/null +++ b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs @@ -0,0 +1,49 @@ +using CloudStoage.Domain; +using Microsoft.AspNetCore.Components.Forms; +using System.Net.Http.Handlers; +using Token.Module.Dependencys; + +namespace CloudStorage.Applications.Helpers; + +public class HtttpClientHelper : IScopedDependency +{ + private const string Name = "api/storage"; + + private readonly IHttpClientFactory httpClientFactory; + + public HtttpClientHelper(IHttpClientFactory httpClientFactory) + { + this.httpClientFactory = httpClientFactory; + } + + public async Task UpdateRand(IReadOnlyList files, Guid? storageId = null, EventHandler eventHandler = null) + { + var http = httpClientFactory.CreateClient(string.Empty); + HttpClientHandler handler = new(); + ProgressMessageHandler progressMessageHandler = new(handler); + progressMessageHandler.HttpSendProgress += eventHandler; + + using (HttpClient httpClient = new(progressMessageHandler)) + { + httpClient.BaseAddress = new Uri(Constant.Api); + httpClient.DefaultRequestHeaders + .Add(Constant.Authorization, http.DefaultRequestHeaders.FirstOrDefault(x => x.Key == Constant.Authorization).Value); + using (var multipartFormData = new MultipartFormDataContent()) + { + foreach (var d in files) + { + multipartFormData.Add(new StreamContent(d.OpenReadStream(d.Size)), "files", d.Name); + } + var response = await httpClient.PostAsync(Name + "/upload-file-list?storageId=" + storageId, multipartFormData); + + if (response.IsSuccessStatusCode) + { + return; + } + } + } + + + } + +} diff --git a/src/CloudStorage.Domain.Shared/Constant.cs b/src/CloudStorage.Domain.Shared/Constant.cs index 9451de8..8ba5036 100644 --- a/src/CloudStorage.Domain.Shared/Constant.cs +++ b/src/CloudStorage.Domain.Shared/Constant.cs @@ -4,10 +4,16 @@ public class Constant { public const string Token = "token"; +#if DEBUG + public const string Api = "https://localhost:8081"; +#else public const string Api = "https://tokenhu.top"; +#endif public const string DateTimeStr = "yyyy-MM-dd HH-mm-ss"; + public const string Authorization = "Authorization"; + /// /// 响应状态码 /// diff --git a/src/CloudStorage.Layou/Components/MenuList.razor b/src/CloudStorage.Layou/Components/MenuList.razor index 41d97c2..995cd06 100644 --- a/src/CloudStorage.Layou/Components/MenuList.razor +++ b/src/CloudStorage.Layou/Components/MenuList.razor @@ -4,6 +4,7 @@ + diff --git a/src/CloudStorage.Layou/Pages/Storages.razor.cs b/src/CloudStorage.Layou/Pages/Storages.razor.cs index 752a786..7b38872 100644 --- a/src/CloudStorage.Layou/Pages/Storages.razor.cs +++ b/src/CloudStorage.Layou/Pages/Storages.razor.cs @@ -1,9 +1,11 @@ using CloudStoage.Domain.HttpModule.Input; using CloudStoage.Domain.HttpModule.Result; -using CloudStorage.Layou.Components; +using CloudStorage.Applications.Helpers; using CloudStorage.Layou.Helper; +using Masa.Blazor; using Microsoft.AspNetCore.Components.Forms; -using Microsoft.JSInterop; +using System.Diagnostics; +using System.Net.Http.Handlers; using Token.EventBus; namespace CloudStorage.Layou.Pages; @@ -29,6 +31,9 @@ partial class Storages [Inject] public IDistributedEventBus DistributedEventBus { get; set; } + [Inject] + public IPopupService PopupService { get; set; } + /// /// js /// @@ -38,6 +43,9 @@ partial class Storages [Inject] public StorageApi StorageApi { get; set; } + [Inject] + public HtttpClientHelper HtttpClientHelper { get; set; } + public const string inputFileId = "inputfile"; private async Task ClickInputFileAsync() @@ -110,9 +118,16 @@ partial class Storages var files = eventArgs.GetMultipleFiles(10); if (files.Count > 0) { - await StorageApi.UploadFileListAsync(files, GetStorageListInput.StorageId); + await PopupService.ToastAsync("ϴļ", BlazorComponent.AlertTypes.Info); + await HtttpClientHelper.UpdateRand(files, GetStorageListInput.StorageId, HttpSendProgress); + await PopupService.ToastAsync("ϴ", BlazorComponent.AlertTypes.Success); await GetStorageListAsync(); StateHasChanged(); } } + + private void HttpSendProgress(object sender, HttpProgressEventArgs e) + { + Debug.WriteLine(e.ProgressPercentage + "%"); + } } \ No newline at end of file diff --git a/src/CloudStorage.Layou/Pages/Uploading.razor b/src/CloudStorage.Layou/Pages/Uploading.razor new file mode 100644 index 0000000..190823a --- /dev/null +++ b/src/CloudStorage.Layou/Pages/Uploading.razor @@ -0,0 +1,7 @@ +@page "/Uploading" + +

上传列表

+ +@code { + +} -- Gitee From 9a8610d732321d51472ec6bbe28ad3a169bfff76 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Fri, 29 Jul 2022 00:57:59 +0800 Subject: [PATCH 04/13] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=97=B6=E4=B8=BA=E6=90=BA=E5=B8=A6token?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Helpers/HtttpClientHelper.cs | 32 +++++++++---------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs index 7ba648c..3f06edb 100644 --- a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs +++ b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs @@ -23,24 +23,22 @@ public class HtttpClientHelper : IScopedDependency ProgressMessageHandler progressMessageHandler = new(handler); progressMessageHandler.HttpSendProgress += eventHandler; - using (HttpClient httpClient = new(progressMessageHandler)) + using HttpClient httpClient = new(progressMessageHandler); + + httpClient.BaseAddress = new Uri(Constant.Api); + httpClient.DefaultRequestHeaders + .Add(Constant.Authorization, http.DefaultRequestHeaders.FirstOrDefault(x => x.Key == Constant.Authorization).Value); + + using var multipartFormData = new MultipartFormDataContent(); + foreach (var d in files) + { + multipartFormData.Add(new StreamContent(d.OpenReadStream(d.Size)), "files", d.Name); + } + var response = await httpClient.PostAsync(Name + "/upload-file-list?storageId=" + storageId, multipartFormData); + + if (response.IsSuccessStatusCode) { - httpClient.BaseAddress = new Uri(Constant.Api); - httpClient.DefaultRequestHeaders - .Add(Constant.Authorization, http.DefaultRequestHeaders.FirstOrDefault(x => x.Key == Constant.Authorization).Value); - using (var multipartFormData = new MultipartFormDataContent()) - { - foreach (var d in files) - { - multipartFormData.Add(new StreamContent(d.OpenReadStream(d.Size)), "files", d.Name); - } - var response = await httpClient.PostAsync(Name + "/upload-file-list?storageId=" + storageId, multipartFormData); - - if (response.IsSuccessStatusCode) - { - return; - } - } + return; } -- Gitee From c5354b56a343becc4eb34181f89ddf5143ab2949 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Sun, 31 Jul 2022 23:36:55 +0800 Subject: [PATCH 05/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E6=A8=A1=E5=9D=97=EF=BC=88=E6=9C=AA=E5=AE=8C?= =?UTF-8?q?=E5=96=84=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CloudStoage.Domain/Etos/UploadingEto.cs | 26 ++++++++ src/CloudStoage.Domain/UploadingDto.cs | 45 +++++++++++++ .../CloudStorage.Applications.csproj | 3 +- .../CloudStorageApplicationsModule.cs | 6 +- .../EventHandle/UploadingEventBus.cs | 65 +++++++++++++++++++ .../Helpers/HtttpClientHelper.cs | 20 +++--- .../KeyLoadNames.cs | 6 ++ src/CloudStorage.Domain.Shared/UpdateStats.cs | 24 +++++++ .../CloudStorage.Layou.csproj | 6 +- .../CloudStorageLayouModule.cs | 2 +- .../Components/CreateFolder.razor.cs | 2 +- .../Components/FileFunction.razor.cs | 2 +- .../Components/Storagefile.razor.cs | 4 +- .../Components/Uploads/UploadTheList.razor | 13 ++++ .../Components/Uploads/UploadTheList.razor.cs | 24 +++++++ .../Uploads/UploadTheList.razor.css | 11 ++++ .../Pages/Storages.razor.cs | 23 ++++--- src/CloudStorage.Layou/Pages/Uploading.razor | 12 ++-- 18 files changed, 259 insertions(+), 35 deletions(-) create mode 100644 src/CloudStoage.Domain/Etos/UploadingEto.cs create mode 100644 src/CloudStoage.Domain/UploadingDto.cs create mode 100644 src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs create mode 100644 src/CloudStorage.Domain.Shared/KeyLoadNames.cs create mode 100644 src/CloudStorage.Domain.Shared/UpdateStats.cs create mode 100644 src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor create mode 100644 src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs create mode 100644 src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css diff --git a/src/CloudStoage.Domain/Etos/UploadingEto.cs b/src/CloudStoage.Domain/Etos/UploadingEto.cs new file mode 100644 index 0000000..48d6749 --- /dev/null +++ b/src/CloudStoage.Domain/Etos/UploadingEto.cs @@ -0,0 +1,26 @@ +namespace CloudStoage.Domain.Etos; + +public class UploadingEto +{ + public Guid Id { get; set; } + + /// + /// /文件名称 + /// + public string? FileName { get; set; } + + /// + /// 文件夹Id + /// + public Guid? StorageId { get; set; } + + /// + /// 长度 + /// + public long? Length { get; set; } + + /// + /// Stream + /// + public Stream Stream { get; set; } +} diff --git a/src/CloudStoage.Domain/UploadingDto.cs b/src/CloudStoage.Domain/UploadingDto.cs new file mode 100644 index 0000000..67cc23e --- /dev/null +++ b/src/CloudStoage.Domain/UploadingDto.cs @@ -0,0 +1,45 @@ +using CloudStorage.Domain.Shared; + +namespace CloudStoage.Domain; + +public class UploadingDto +{ + public Guid Id { get; set; } + + /// + /// /文件名称 + /// + public string? FileName { get; set; } + + /// + /// 长度 + /// + public long Length { get; set; } = 0; + + /// + /// 已经上传的大小 + /// + public long UploadingSize { get; set; } = 0; + + + /// + /// 上传状态 + /// + public UpdateStats Stats { get; set; } + + /// + /// 上传进度 + /// + public int Progress + { + get + { + if (UploadingSize == 0 && Length == 0) + { + return 0; + } + + return (int)((UploadingSize / Length) * 100); + } + } +} diff --git a/src/CloudStorage.Applications/CloudStorage.Applications.csproj b/src/CloudStorage.Applications/CloudStorage.Applications.csproj index 6c79903..974e29d 100644 --- a/src/CloudStorage.Applications/CloudStorage.Applications.csproj +++ b/src/CloudStorage.Applications/CloudStorage.Applications.csproj @@ -23,7 +23,8 @@ - + +
diff --git a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs index b0157dc..7d9e696 100644 --- a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs +++ b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs @@ -1,11 +1,13 @@ using CloudStoage.Domain; -using CloudStorage.Applications.Filter; using CloudStorage.Applications.Manage; -using System.Security.Authentication; +using Token.EventBus; using Token.Module; +using Token.Module.Attributes; namespace CloudStorage.Applications; +[DependOn( + typeof(TokenEventBusModule))] public class CloudStorageApplicationsModule : TokenModule { public override async void ConfigureServices(IServiceCollection services) diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs new file mode 100644 index 0000000..1da2b99 --- /dev/null +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -0,0 +1,65 @@ +using CloudStoage.Domain; +using CloudStoage.Domain.Etos; +using CloudStorage.Applications.Helpers; +using CloudStorage.Domain.Shared; +using Microsoft.AspNetCore.Components; +using System.Collections.Concurrent; +using System.Net.Http.Handlers; +using Token.EventBus; +using Token.EventBus.Handlers; +using Token.Module.Dependencys; + +namespace CloudStorage.Applications.EventHandle; + +public class UploadingEventBus : ILocalEventHandler>, ISingletonDependency +{ + public BlockingCollection UploadingList { get; set; } = new BlockingCollection(); + + private readonly IKeyLocalEventBus KeyLocalEventBus; + + private readonly HtttpClientHelper _htttpClientHelper; + + public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus keyLocalEventBus) + { + _htttpClientHelper = htttpClientHelper; + KeyLocalEventBus = keyLocalEventBus; + } + + public async Task HandleEventAsync(List eventData) + { + foreach (var item in eventData) + { + UploadingList.Add(new UploadingDto + { + Id = item.Id, + FileName = item.FileName, + Length = item.Length ?? 0, + Stats = UpdateStats.BeUploading + }); + + await _htttpClientHelper.UpdateRand(item, HttpProgressEvent); + } + + } + + private async void HttpProgressEvent(object data, HttpProgressEventArgs eventArgs) + { + var http = data as HttpRequestMessage; + var value = http.Headers.GetValues("id").FirstOrDefault(); + + if (!string.IsNullOrEmpty(value)) + { + var id = Guid.Parse(value); + + foreach (var d in UploadingList) + { + if (d.Id == id) + { + d.UploadingSize = eventArgs.BytesTransferred; + await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, true); + return; + } + } + } + } +} diff --git a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs index 3f06edb..31609b8 100644 --- a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs +++ b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs @@ -1,4 +1,5 @@ using CloudStoage.Domain; +using CloudStoage.Domain.Etos; using Microsoft.AspNetCore.Components.Forms; using System.Net.Http.Handlers; using Token.Module.Dependencys; @@ -16,7 +17,7 @@ public class HtttpClientHelper : IScopedDependency this.httpClientFactory = httpClientFactory; } - public async Task UpdateRand(IReadOnlyList files, Guid? storageId = null, EventHandler eventHandler = null) + public async Task UpdateRand(UploadingEto files, EventHandler eventHandler = null) { var http = httpClientFactory.CreateClient(string.Empty); HttpClientHandler handler = new(); @@ -28,19 +29,14 @@ public class HtttpClientHelper : IScopedDependency httpClient.BaseAddress = new Uri(Constant.Api); httpClient.DefaultRequestHeaders .Add(Constant.Authorization, http.DefaultRequestHeaders.FirstOrDefault(x => x.Key == Constant.Authorization).Value); - - using var multipartFormData = new MultipartFormDataContent(); - foreach (var d in files) - { - multipartFormData.Add(new StreamContent(d.OpenReadStream(d.Size)), "files", d.Name); - } - var response = await httpClient.PostAsync(Name + "/upload-file-list?storageId=" + storageId, multipartFormData); - - if (response.IsSuccessStatusCode) + httpClient.DefaultRequestHeaders.Add("id", files.Id.ToString()); + + using var multipartFormData = new MultipartFormDataContent { - return; - } + { new StreamContent(files.Stream), "file", files.FileName } + }; + var response = await httpClient.PostAsync(Name + "/upload-file?storageId=" + files.StorageId, multipartFormData); } diff --git a/src/CloudStorage.Domain.Shared/KeyLoadNames.cs b/src/CloudStorage.Domain.Shared/KeyLoadNames.cs new file mode 100644 index 0000000..a91c23f --- /dev/null +++ b/src/CloudStorage.Domain.Shared/KeyLoadNames.cs @@ -0,0 +1,6 @@ +namespace CloudStorage.Domain.Shared; + +public class KeyLoadNames +{ + public const string UploadingListName = "UploadingListName"; +} diff --git a/src/CloudStorage.Domain.Shared/UpdateStats.cs b/src/CloudStorage.Domain.Shared/UpdateStats.cs new file mode 100644 index 0000000..51b68c4 --- /dev/null +++ b/src/CloudStorage.Domain.Shared/UpdateStats.cs @@ -0,0 +1,24 @@ +namespace CloudStorage.Domain.Shared; + +public enum UpdateStats +{ + /// + /// 正在上传 + /// + BeUploading = 0, + + /// + /// 成功 + /// + Succeed, + + /// + /// 失败 + /// + Failure, + + /// + /// 暂停 + /// + Suspend +} diff --git a/src/CloudStorage.Layou/CloudStorage.Layou.csproj b/src/CloudStorage.Layou/CloudStorage.Layou.csproj index 000809d..9b596e9 100644 --- a/src/CloudStorage.Layou/CloudStorage.Layou.csproj +++ b/src/CloudStorage.Layou/CloudStorage.Layou.csproj @@ -12,10 +12,10 @@ - + - - + + diff --git a/src/CloudStorage.Layou/CloudStorageLayouModule.cs b/src/CloudStorage.Layou/CloudStorageLayouModule.cs index 31f3120..c187919 100644 --- a/src/CloudStorage.Layou/CloudStorageLayouModule.cs +++ b/src/CloudStorage.Layou/CloudStorageLayouModule.cs @@ -7,12 +7,12 @@ using Token.Module.Attributes; namespace CloudStorage.Layou; [DependOn( + typeof(TokenEventBusModule), typeof(CloudStorageApplicationsModule))] public class CloudStorageLayouModule : TokenModule { public override void ConfigureServices(IServiceCollection services) { - services.AddEventBus(); services.AddMasaBlazor(); } } diff --git a/src/CloudStorage.Layou/Components/CreateFolder.razor.cs b/src/CloudStorage.Layou/Components/CreateFolder.razor.cs index 7499d08..eae78d4 100644 --- a/src/CloudStorage.Layou/Components/CreateFolder.razor.cs +++ b/src/CloudStorage.Layou/Components/CreateFolder.razor.cs @@ -32,7 +32,7 @@ partial class CreateFolder [Inject] public StorageApi StorageApi { get; set; } [Inject] - public IDistributedEventBus DistributedEventBus { get; set; } + public IKeyLocalEventBus DistributedEventBus { get; set; } /// /// 创建文件夹事件 diff --git a/src/CloudStorage.Layou/Components/FileFunction.razor.cs b/src/CloudStorage.Layou/Components/FileFunction.razor.cs index 61901cf..9410290 100644 --- a/src/CloudStorage.Layou/Components/FileFunction.razor.cs +++ b/src/CloudStorage.Layou/Components/FileFunction.razor.cs @@ -15,7 +15,7 @@ partial class FileFunction public StorageApi StorageApi { get; set; } [Inject] - public IDistributedEventBus DistributedEventBus { get; set; } + public IKeyLocalEventBus DistributedEventBus { get; set; } private async void DeleteFileAsync() { diff --git a/src/CloudStorage.Layou/Components/Storagefile.razor.cs b/src/CloudStorage.Layou/Components/Storagefile.razor.cs index 140db18..b589825 100644 --- a/src/CloudStorage.Layou/Components/Storagefile.razor.cs +++ b/src/CloudStorage.Layou/Components/Storagefile.razor.cs @@ -33,10 +33,10 @@ partial class Storagefile public StorageApi StorageApi { get; set; } [Inject] - public IDistributedEventBus DistributedEventBus { get; set; } + public IKeyLocalEventBus DistributedEventBus { get; set; } [Inject] - public IDistributedEventBus StringDstributedEventBus { get; set; } + public IKeyLocalEventBus StringDstributedEventBus { get; set; } /// /// Ϣ diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor new file mode 100644 index 0000000..ee8e608 --- /dev/null +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -0,0 +1,13 @@ +
+ @foreach (var d in UploadingEventBus.UploadingList) + { + +
@d.FileName
+ + + @($"{context}%") + + +
+ } +
\ No newline at end of file diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs new file mode 100644 index 0000000..e9a26f3 --- /dev/null +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs @@ -0,0 +1,24 @@ +using CloudStorage.Applications.EventHandle; +using CloudStorage.Domain.Shared; +using Token.EventBus; + +namespace CloudStorage.Layou.Components.Uploads +{ + partial class UploadTheList + { + [Inject] + public UploadingEventBus UploadingEventBus { get; set; } + + [Inject] + public IKeyLocalEventBus UploadTheListEventBus { get; set; } + + protected override async void OnInitialized() + { + await UploadTheListEventBus.Subscribe(KeyLoadNames.UploadingListName, a => + { + InvokeAsync(()=> StateHasChanged()); + }); + + } + } +} diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css new file mode 100644 index 0000000..5d38be2 --- /dev/null +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css @@ -0,0 +1,11 @@ +.upload-list { + transition: .3s; + margin: 5px; + padding: 5px; + border-radius: 10px; +} + +.upload-list:hover{ + transition:.3s; + background-color:aqua; +} \ No newline at end of file diff --git a/src/CloudStorage.Layou/Pages/Storages.razor.cs b/src/CloudStorage.Layou/Pages/Storages.razor.cs index 7b38872..6409ebb 100644 --- a/src/CloudStorage.Layou/Pages/Storages.razor.cs +++ b/src/CloudStorage.Layou/Pages/Storages.razor.cs @@ -1,3 +1,4 @@ +using CloudStoage.Domain.Etos; using CloudStoage.Domain.HttpModule.Input; using CloudStoage.Domain.HttpModule.Result; using CloudStorage.Applications.Helpers; @@ -7,6 +8,7 @@ using Microsoft.AspNetCore.Components.Forms; using System.Diagnostics; using System.Net.Http.Handlers; using Token.EventBus; +using Token.EventBus.EventBus; namespace CloudStorage.Layou.Pages; @@ -29,7 +31,10 @@ partial class Storages public PagedResultDto StorageList { get; set; } = new PagedResultDto(); [Inject] - public IDistributedEventBus DistributedEventBus { get; set; } + public IKeyLocalEventBus DistributedEventBus { get; set; } + + [Inject] + public ILocalEventBus LocalEventBus { get; set; } [Inject] public IPopupService PopupService { get; set; } @@ -119,15 +124,17 @@ partial class Storages if (files.Count > 0) { await PopupService.ToastAsync("ϴļ", BlazorComponent.AlertTypes.Info); - await HtttpClientHelper.UpdateRand(files, GetStorageListInput.StorageId, HttpSendProgress); - await PopupService.ToastAsync("ϴ", BlazorComponent.AlertTypes.Success); - await GetStorageListAsync(); + var uploadings = files.Select(x => new UploadingEto + { + Id = Guid.NewGuid(), + FileName = x.Name, + Length = x.Size, + Stream = x.OpenReadStream(x.Size) + }).ToList(); + + await LocalEventBus.PublishAsync(uploadings,false); StateHasChanged(); } } - private void HttpSendProgress(object sender, HttpProgressEventArgs e) - { - Debug.WriteLine(e.ProgressPercentage + "%"); - } } \ No newline at end of file diff --git a/src/CloudStorage.Layou/Pages/Uploading.razor b/src/CloudStorage.Layou/Pages/Uploading.razor index 190823a..a88e457 100644 --- a/src/CloudStorage.Layou/Pages/Uploading.razor +++ b/src/CloudStorage.Layou/Pages/Uploading.razor @@ -1,7 +1,11 @@ @page "/Uploading" +@using CloudStorage.Layou.Components.Uploads -

上传列表

+ + 上传列表 + 下载列表 + 上传记录 + 下载记录 + -@code { - -} + \ No newline at end of file -- Gitee From 123f36f763b08fbd271532df83588a9050f2c371 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Mon, 1 Aug 2022 02:18:05 +0800 Subject: [PATCH 06/13] =?UTF-8?q?=E6=96=B0=E5=A2=9ESignalr=E4=BC=A0?= =?UTF-8?q?=E8=BE=93=E5=A4=A7=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CloudStorage.Applications.csproj | 2 + .../CloudStorageApplicationsModule.cs | 12 +-- .../EventHandle/UploadingEventBus.cs | 84 ++++++++++++++----- .../Manage/TokenStats.cs | 8 ++ .../Components/Uploads/UploadTheList.razor | 2 +- .../Components/Uploads/UploadTheList.razor.cs | 9 +- 6 files changed, 88 insertions(+), 29 deletions(-) create mode 100644 src/CloudStorage.Applications/Manage/TokenStats.cs diff --git a/src/CloudStorage.Applications/CloudStorage.Applications.csproj b/src/CloudStorage.Applications/CloudStorage.Applications.csproj index 974e29d..94b19e6 100644 --- a/src/CloudStorage.Applications/CloudStorage.Applications.csproj +++ b/src/CloudStorage.Applications/CloudStorage.Applications.csproj @@ -21,6 +21,8 @@ + + diff --git a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs index 7d9e696..b8737db 100644 --- a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs +++ b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs @@ -1,5 +1,6 @@ using CloudStoage.Domain; using CloudStorage.Applications.Manage; +using CloudStorage.Domain.Shared; using Token.EventBus; using Token.Module; using Token.Module.Attributes; @@ -16,20 +17,21 @@ public class CloudStorageApplicationsModule : TokenModule .ConfigureHttpClient((services, x) => { var status = services.GetService(); - x.DefaultRequestHeaders.Add(Constant.Authorization, $"Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJ1c2VyIjoie1wiQWNjb3VudFwiOlwiYWRtaW5cIixcIlBhc3N3b3JkXCI6XCJhZG1pblwiLFwiTmFtZVwiOlwiYWRtaW5cIixcIkJyaWVmSW50cm9kdWN0aW9uXCI6bnVsbCxcIldlQ2hhdE9wZW5JZFwiOm51bGwsXCJIZWFkUG9ydHJhaXRzXCI6XCJodHRwczovL3RzMS5jbi5tbS5iaW5nLm5ldC90aD9pZD1PSVAtQy5JYmdZY2JDSGZVVXRmd2VHTUtBalR3QUFBQSZ3PTI1MCZoPTI1MCZjPTgmcnM9MSZxbHQ9OTAmbz02JnBpZD0zLjEmcm09MlwiLFwiU2V4XCI6MCxcIlN0YXR1c1wiOjAsXCJDbG91ZFN0b3JhZ2VSb290XCI6XCIuL3d3d3Jvb3QvQ2xvdWRTdG9yYWdlL2FjNGRlZGZhMWViZTRjM2E5YmZjYmZkZDY1ZDFmM2QyXCIsXCJJc0RlbGV0ZWRcIjpmYWxzZSxcIkNyZWF0aW9uVGltZVwiOlwiMDAwMS0wMS0wMVQwMDowMDowMFwiLFwiRXh0cmFQcm9wZXJ0aWVzXCI6e30sXCJDb25jdXJyZW5jeVN0YW1wXCI6XCJmNDAwYmJlOGNkMjQ0OWQ1YTI1MWNmYzEzYWYzMTI4Y1wiLFwiSWRcIjpcIjQ5NTgxZGQ0LTJjMzktNDY3MC05MTliLTUyMzJjZTBlOWUzZVwifSIsImlkIjoiNDk1ODFkZDQtMmMzOS00NjcwLTkxOWItNTIzMmNlMGU5ZTNlIiwiZXhwIjoxNzYyMjI2NDM0LCJpc3MiOiJ0b2tlbmh1LnRvcCIsImF1ZCI6InRva2VuaHUudG9wIn0.Qga-H-eKWrguw6ojk3ps1lW0sdx2XVxfmlZPvtjAWSg"); + var token = services.GetRequiredService(); + x.DefaultRequestHeaders.Add(Constant.Authorization, $"Bearer " + token.Token); x.BaseAddress = new Uri(Constant.Api); - + }) .ConfigureHttpMessageHandlerBuilder(builder => { builder.PrimaryHandler = new HttpClientHandler { ServerCertificateCustomValidationCallback = (m, c, ch, e) => true - - }; + + }; }); - await services.Initialize(); + await services.Initialize(); } } \ No newline at end of file diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index 1da2b99..b93f1aa 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -2,9 +2,15 @@ using CloudStoage.Domain.Etos; using CloudStorage.Applications.Helpers; using CloudStorage.Domain.Shared; +using MessagePack; using Microsoft.AspNetCore.Components; +using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Maui.Storage; +using Newtonsoft.Json; +using System; using System.Collections.Concurrent; using System.Net.Http.Handlers; +using System.Threading.Channels; using Token.EventBus; using Token.EventBus.Handlers; using Token.Module.Dependencys; @@ -17,49 +23,85 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle private readonly IKeyLocalEventBus KeyLocalEventBus; + private readonly TokenManage token; + private readonly HtttpClientHelper _htttpClientHelper; - public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus keyLocalEventBus) + private HubConnection connection; + + public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus keyLocalEventBus, TokenManage token) { _htttpClientHelper = htttpClientHelper; KeyLocalEventBus = keyLocalEventBus; + this.token = token; } public async Task HandleEventAsync(List eventData) { - foreach (var item in eventData) + if (connection == null || connection.State != HubConnectionState.Connected) + { + connection = new HubConnectionBuilder() + .WithUrl(Constant.Api + "/file-stream", option => + { + option.AccessTokenProvider = () => Task.FromResult(token.Token); + }) + .AddJsonProtocol() + .Build(); + + await connection.StartAsync(); + } + + eventData.ForEach(x => { UploadingList.Add(new UploadingDto { - Id = item.Id, - FileName = item.FileName, - Length = item.Length ?? 0, + Id = x.Id, + FileName = x.FileName, + Length = x.Length ?? 0, Stats = UpdateStats.BeUploading }); + }); - await _htttpClientHelper.UpdateRand(item, HttpProgressEvent); - } + int size = (1024 * 10); + foreach (var item in eventData) + { + int length = (int)(item.Length / size); + var channel = Channel.CreateBounded(length); + await connection.SendAsync("FileStreamSaveAsync", channel.Reader, JsonConvert.SerializeObject(new + { + StorageId = item.StorageId, + FileName = item.FileName, + Length = item.Length + })); + + var bytesTransferred = 0; + for (int k = 0; k < length; k++) + { + var b = new byte[size]; + await item.Stream.ReadAsync(b); + await channel.Writer.WriteAsync(b); + bytesTransferred += b.Length; + if (k != 0) + { + await UploadingSizeEvent(item.Id, bytesTransferred); + } + } + channel.Writer.Complete(); + } } - private async void HttpProgressEvent(object data, HttpProgressEventArgs eventArgs) + private async Task UploadingSizeEvent(Guid id, int BytesTransferred) { - var http = data as HttpRequestMessage; - var value = http.Headers.GetValues("id").FirstOrDefault(); - - if (!string.IsNullOrEmpty(value)) + foreach (var d in UploadingList) { - var id = Guid.Parse(value); - - foreach (var d in UploadingList) + if (d.Id == id) { - if (d.Id == id) - { - d.UploadingSize = eventArgs.BytesTransferred; - await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, true); - return; - } + d.UploadingSize = BytesTransferred; + await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, true); + return; } } } + } diff --git a/src/CloudStorage.Applications/Manage/TokenStats.cs b/src/CloudStorage.Applications/Manage/TokenStats.cs new file mode 100644 index 0000000..bd9c9f9 --- /dev/null +++ b/src/CloudStorage.Applications/Manage/TokenStats.cs @@ -0,0 +1,8 @@ +using Token.Module.Dependencys; + +namespace CloudStorage.Domain.Shared; + +public class TokenManage : IScopedDependency +{ + public string Token { get; set; } = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCIsImN0eSI6IkpXVCJ9.eyJ1c2VyIjoie1wiQWNjb3VudFwiOlwiYWRtaW5cIixcIlBhc3N3b3JkXCI6XCJhZG1pblwiLFwiTmFtZVwiOlwiYWRtaW5cIixcIkJyaWVmSW50cm9kdWN0aW9uXCI6bnVsbCxcIldlQ2hhdE9wZW5JZFwiOm51bGwsXCJIZWFkUG9ydHJhaXRzXCI6XCJodHRwczovL3RzMS5jbi5tbS5iaW5nLm5ldC90aD9pZD1PSVAtQy5JYmdZY2JDSGZVVXRmd2VHTUtBalR3QUFBQSZ3PTI1MCZoPTI1MCZjPTgmcnM9MSZxbHQ9OTAmbz02JnBpZD0zLjEmcm09MlwiLFwiU2V4XCI6MCxcIlN0YXR1c1wiOjAsXCJDbG91ZFN0b3JhZ2VSb290XCI6XCIuL3d3d3Jvb3QvQ2xvdWRTdG9yYWdlL2FjNGRlZGZhMWViZTRjM2E5YmZjYmZkZDY1ZDFmM2QyXCIsXCJJc0RlbGV0ZWRcIjpmYWxzZSxcIkNyZWF0aW9uVGltZVwiOlwiMDAwMS0wMS0wMVQwMDowMDowMFwiLFwiRXh0cmFQcm9wZXJ0aWVzXCI6e30sXCJDb25jdXJyZW5jeVN0YW1wXCI6XCJmNDAwYmJlOGNkMjQ0OWQ1YTI1MWNmYzEzYWYzMTI4Y1wiLFwiSWRcIjpcIjQ5NTgxZGQ0LTJjMzktNDY3MC05MTliLTUyMzJjZTBlOWUzZVwifSIsImlkIjoiNDk1ODFkZDQtMmMzOS00NjcwLTkxOWItNTIzMmNlMGU5ZTNlIiwiZXhwIjoxNzYyMjI2NDM0LCJpc3MiOiJ0b2tlbmh1LnRvcCIsImF1ZCI6InRva2VuaHUudG9wIn0.Qga-H-eKWrguw6ojk3ps1lW0sdx2XVxfmlZPvtjAWSg"; +} diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor index ee8e608..073948f 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -1,5 +1,5 @@ 
- @foreach (var d in UploadingEventBus.UploadingList) + @foreach (var d in UploadingList) {
@d.FileName
diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs index e9a26f3..8d7bc63 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs @@ -1,5 +1,7 @@ -using CloudStorage.Applications.EventHandle; +using CloudStoage.Domain; +using CloudStorage.Applications.EventHandle; using CloudStorage.Domain.Shared; +using System.Collections.Concurrent; using Token.EventBus; namespace CloudStorage.Layou.Components.Uploads @@ -12,11 +14,14 @@ namespace CloudStorage.Layou.Components.Uploads [Inject] public IKeyLocalEventBus UploadTheListEventBus { get; set; } + public BlockingCollection UploadingList { get; set; } = new BlockingCollection(); + protected override async void OnInitialized() { await UploadTheListEventBus.Subscribe(KeyLoadNames.UploadingListName, a => { - InvokeAsync(()=> StateHasChanged()); + UploadingList = UploadingEventBus.UploadingList; + InvokeAsync(() => StateHasChanged()); }); } -- Gitee From 48d7d75beaacfe51eb09bf94e1707e50e70888e8 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Mon, 1 Aug 2022 23:09:58 +0800 Subject: [PATCH 07/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8B=E8=BD=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CloudStoage.Domain/UploadingDto.cs | 2 +- .../EventHandle/UploadingEventBus.cs | 11 +++------- .../Components/Uploads/UploadTheList.razor | 4 ++-- .../Components/Uploads/UploadTheList.razor.cs | 22 +++++++++++++++---- 4 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/CloudStoage.Domain/UploadingDto.cs b/src/CloudStoage.Domain/UploadingDto.cs index 67cc23e..0843d32 100644 --- a/src/CloudStoage.Domain/UploadingDto.cs +++ b/src/CloudStoage.Domain/UploadingDto.cs @@ -34,7 +34,7 @@ public class UploadingDto { get { - if (UploadingSize == 0 && Length == 0) + if (UploadingSize == 0 || Length == 0) { return 0; } diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index b93f1aa..9bcc6a5 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -2,14 +2,9 @@ using CloudStoage.Domain.Etos; using CloudStorage.Applications.Helpers; using CloudStorage.Domain.Shared; -using MessagePack; -using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.SignalR.Client; -using Microsoft.Maui.Storage; using Newtonsoft.Json; -using System; using System.Collections.Concurrent; -using System.Net.Http.Handlers; using System.Threading.Channels; using Token.EventBus; using Token.EventBus.Handlers; @@ -21,7 +16,7 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle { public BlockingCollection UploadingList { get; set; } = new BlockingCollection(); - private readonly IKeyLocalEventBus KeyLocalEventBus; + private readonly IKeyLocalEventBus> KeyLocalEventBus; private readonly TokenManage token; @@ -29,7 +24,7 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle private HubConnection connection; - public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus keyLocalEventBus, TokenManage token) + public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus> keyLocalEventBus, TokenManage token) { _htttpClientHelper = htttpClientHelper; KeyLocalEventBus = keyLocalEventBus; @@ -98,7 +93,7 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle if (d.Id == id) { d.UploadingSize = BytesTransferred; - await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, true); + await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, new Tuple(BytesTransferred, id)); return; } } diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor index 073948f..ed33f9b 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -4,8 +4,8 @@
@d.FileName
- - @($"{context}%") + + @($"{(d.UploadingSize / d.Length*100)}")
diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs index 8d7bc63..52f6504 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs @@ -2,6 +2,7 @@ using CloudStorage.Applications.EventHandle; using CloudStorage.Domain.Shared; using System.Collections.Concurrent; +using System.Drawing; using Token.EventBus; namespace CloudStorage.Layou.Components.Uploads @@ -12,16 +13,29 @@ namespace CloudStorage.Layou.Components.Uploads public UploadingEventBus UploadingEventBus { get; set; } [Inject] - public IKeyLocalEventBus UploadTheListEventBus { get; set; } + public IKeyLocalEventBus> UploadTheListEventBus { get; set; } - public BlockingCollection UploadingList { get; set; } = new BlockingCollection(); + public static List UploadingList { get; set; } protected override async void OnInitialized() { + + UploadingList.AddRange(UploadingEventBus.UploadingList); + + UploadingList = UploadingList.DistinctBy(x => x.Id).ToList(); + await UploadTheListEventBus.Subscribe(KeyLoadNames.UploadingListName, a => { - UploadingList = UploadingEventBus.UploadingList; - InvokeAsync(() => StateHasChanged()); + foreach (var d in UploadingList) + { + if (d.Id == a.Item2) + { + d.UploadingSize = a.Item1; + StateHasChanged(); + return; + } + } + }); } -- Gitee From b8ddee0c9874e1a655b86fee1dd0ff17663f8f4e Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Mon, 1 Aug 2022 23:54:26 +0800 Subject: [PATCH 08/13] =?UTF-8?q?=E5=AE=8C=E5=96=84=E4=B8=8B=E8=BD=BD?= =?UTF-8?q?=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CloudStoage.Domain/UploadingDto.cs | 2 +- .../EventHandle/UploadingEventBus.cs | 21 +++++++++++-------- .../Components/Uploads/UploadTheList.razor | 4 ++-- .../Components/Uploads/UploadTheList.razor.cs | 7 +++---- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/CloudStoage.Domain/UploadingDto.cs b/src/CloudStoage.Domain/UploadingDto.cs index 0843d32..1d5c52c 100644 --- a/src/CloudStoage.Domain/UploadingDto.cs +++ b/src/CloudStoage.Domain/UploadingDto.cs @@ -39,7 +39,7 @@ public class UploadingDto return 0; } - return (int)((UploadingSize / Length) * 100); + return (int)((decimal)UploadingSize / (decimal)Length * 100m); } } } diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index 9bcc6a5..444c372 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -61,8 +61,9 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle foreach (var item in eventData) { int length = (int)(item.Length / size); - var channel = Channel.CreateBounded(length); + var channel = Channel.CreateBounded(length+1); + // 建立传输通道 await connection.SendAsync("FileStreamSaveAsync", channel.Reader, JsonConvert.SerializeObject(new { StorageId = item.StorageId, @@ -71,17 +72,19 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle })); var bytesTransferred = 0; - for (int k = 0; k < length; k++) + + // 定义下载缓存 + var b = new byte[size]; + int len; + while ((len = await item.Stream.ReadAsync(b)) != 0) { - var b = new byte[size]; - await item.Stream.ReadAsync(b); + await channel.Writer.WriteAsync(b); - bytesTransferred += b.Length; - if (k != 0) - { - await UploadingSizeEvent(item.Id, bytesTransferred); - } + bytesTransferred += len; + await UploadingSizeEvent(item.Id, bytesTransferred); } + + // 传输完成结束通道 channel.Writer.Complete(); } } diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor index ed33f9b..be14631 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -4,8 +4,8 @@
@d.FileName
- - @($"{(d.UploadingSize / d.Length*100)}") + + @(d.Progress)%
diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs index 52f6504..9485631 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs @@ -15,15 +15,14 @@ namespace CloudStorage.Layou.Components.Uploads [Inject] public IKeyLocalEventBus> UploadTheListEventBus { get; set; } - public static List UploadingList { get; set; } + public static BlockingCollection UploadingList { get; set; } protected override async void OnInitialized() { - UploadingList.AddRange(UploadingEventBus.UploadingList); + UploadingList = UploadingEventBus.UploadingList; + - UploadingList = UploadingList.DistinctBy(x => x.Id).ToList(); - await UploadTheListEventBus.Subscribe(KeyLoadNames.UploadingListName, a => { foreach (var d in UploadingList) -- Gitee From c34b223a7d9f790c986202facad8ceeda41585d3 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Tue, 2 Aug 2022 00:23:22 +0800 Subject: [PATCH 09/13] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E7=95=8C=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EventHandle/UploadingEventBus.cs | 6 +++++- .../Components/Uploads/UploadTheList.razor | 17 ++++++++++++++++- .../Components/Uploads/UploadTheList.razor.css | 12 +----------- src/CloudStorage.Layou/Pages/Storages.razor.cs | 1 + 4 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index 444c372..48d4b0d 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -2,6 +2,7 @@ using CloudStoage.Domain.Etos; using CloudStorage.Applications.Helpers; using CloudStorage.Domain.Shared; +using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.SignalR.Client; using Newtonsoft.Json; using System.Collections.Concurrent; @@ -17,6 +18,7 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle public BlockingCollection UploadingList { get; set; } = new BlockingCollection(); private readonly IKeyLocalEventBus> KeyLocalEventBus; + private readonly IKeyLocalEventBus DistributedEventBus; private readonly TokenManage token; @@ -24,11 +26,12 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle private HubConnection connection; - public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus> keyLocalEventBus, TokenManage token) + public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus> keyLocalEventBus, TokenManage token, IKeyLocalEventBus distributedEventBus) { _htttpClientHelper = htttpClientHelper; KeyLocalEventBus = keyLocalEventBus; this.token = token; + DistributedEventBus = distributedEventBus; } public async Task HandleEventAsync(List eventData) @@ -86,6 +89,7 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle // 传输完成结束通道 channel.Writer.Complete(); + await DistributedEventBus.PublishAsync("Storages", "上传文件成功"); } } diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor index be14631..03d2009 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -10,4 +10,19 @@
} -
\ No newline at end of file + + + + \ No newline at end of file diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css index 5d38be2..5f28270 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.css @@ -1,11 +1 @@ -.upload-list { - transition: .3s; - margin: 5px; - padding: 5px; - border-radius: 10px; -} - -.upload-list:hover{ - transition:.3s; - background-color:aqua; -} \ No newline at end of file + \ No newline at end of file diff --git a/src/CloudStorage.Layou/Pages/Storages.razor.cs b/src/CloudStorage.Layou/Pages/Storages.razor.cs index 6409ebb..a801c9d 100644 --- a/src/CloudStorage.Layou/Pages/Storages.razor.cs +++ b/src/CloudStorage.Layou/Pages/Storages.razor.cs @@ -129,6 +129,7 @@ partial class Storages Id = Guid.NewGuid(), FileName = x.Name, Length = x.Size, + StorageId= GetStorageListInput.StorageId, Stream = x.OpenReadStream(x.Size) }).ToList(); -- Gitee From 22a894e6922fb9333cfaf5f68e7a030a8f70dc10 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Tue, 2 Aug 2022 19:49:30 +0800 Subject: [PATCH 10/13] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E7=AE=A1=E7=90=86=E5=99=A8=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../EventHandle/UploadingEventBus.cs | 31 +++++++++-------- src/CloudStorage.Domain.Shared/Constant.cs | 1 - .../Components/Uploads/UploadTheList.razor | 33 ++++++++++++++++--- .../Components/Uploads/UploadTheList.razor.cs | 7 ++-- src/CloudStorage.Layou/_Imports.razor | 3 +- 5 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index 48d4b0d..8d3b7a4 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -17,18 +17,15 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle { public BlockingCollection UploadingList { get; set; } = new BlockingCollection(); - private readonly IKeyLocalEventBus> KeyLocalEventBus; + private readonly IKeyLocalEventBus KeyLocalEventBus; private readonly IKeyLocalEventBus DistributedEventBus; private readonly TokenManage token; - private readonly HtttpClientHelper _htttpClientHelper; - private HubConnection connection; - public UploadingEventBus(HtttpClientHelper htttpClientHelper, IKeyLocalEventBus> keyLocalEventBus, TokenManage token, IKeyLocalEventBus distributedEventBus) + public UploadingEventBus(IKeyLocalEventBus keyLocalEventBus, TokenManage token, IKeyLocalEventBus distributedEventBus) { - _htttpClientHelper = htttpClientHelper; KeyLocalEventBus = keyLocalEventBus; this.token = token; DistributedEventBus = distributedEventBus; @@ -64,20 +61,20 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle foreach (var item in eventData) { int length = (int)(item.Length / size); - var channel = Channel.CreateBounded(length+1); + var channel = Channel.CreateBounded(length + 1); // 建立传输通道 await connection.SendAsync("FileStreamSaveAsync", channel.Reader, JsonConvert.SerializeObject(new { - StorageId = item.StorageId, - FileName = item.FileName, - Length = item.Length + item.StorageId, + item.FileName, + item.Length })); var bytesTransferred = 0; // 定义下载缓存 - var b = new byte[size]; + var b = new byte[size > item.Length ? item.Length ?? 0 : size]; int len; while ((len = await item.Stream.ReadAsync(b)) != 0) { @@ -86,21 +83,29 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle bytesTransferred += len; await UploadingSizeEvent(item.Id, bytesTransferred); } - + // 传输完成结束通道 channel.Writer.Complete(); await DistributedEventBus.PublishAsync("Storages", "上传文件成功"); + + await UploadingSizeEvent(item.Id, succeed: true); } } - private async Task UploadingSizeEvent(Guid id, int BytesTransferred) + private async Task UploadingSizeEvent(Guid id, int BytesTransferred = 0, bool succeed = false) { foreach (var d in UploadingList) { if (d.Id == id) { + if (succeed) + { + d.Stats = UpdateStats.Succeed; + await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, d); + return; + } d.UploadingSize = BytesTransferred; - await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, new Tuple(BytesTransferred, id)); + await KeyLocalEventBus.PublishAsync(KeyLoadNames.UploadingListName, d); return; } } diff --git a/src/CloudStorage.Domain.Shared/Constant.cs b/src/CloudStorage.Domain.Shared/Constant.cs index 714fef3..9a6e18a 100644 --- a/src/CloudStorage.Domain.Shared/Constant.cs +++ b/src/CloudStorage.Domain.Shared/Constant.cs @@ -5,7 +5,6 @@ public class Constant public const string Token = "token"; public const string Api = "https://tokenhu.top/cloud-storage"; -#endif public const string DateTimeStr = "yyyy-MM-dd HH-mm-ss"; diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor index 03d2009..c170533 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -3,11 +3,34 @@ {
@d.FileName
- - - @(d.Progress)% - - + @if (d.Stats == UpdateStats.BeUploading) + { + + + + @(d.Progress)% + + + } + else if (d.Stats == UpdateStats.Failure) + { + + + 上传失败 + + } + else if (d.Stats == UpdateStats.Succeed) + { + + 上传完成 + + } + else + { + + 未知状态 + + }
} diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs index 9485631..0d621b6 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs @@ -13,7 +13,7 @@ namespace CloudStorage.Layou.Components.Uploads public UploadingEventBus UploadingEventBus { get; set; } [Inject] - public IKeyLocalEventBus> UploadTheListEventBus { get; set; } + public IKeyLocalEventBus UploadTheListEventBus { get; set; } public static BlockingCollection UploadingList { get; set; } @@ -27,9 +27,10 @@ namespace CloudStorage.Layou.Components.Uploads { foreach (var d in UploadingList) { - if (d.Id == a.Item2) + if (d.Id == a.Id) { - d.UploadingSize = a.Item1; + d.Stats = a.Stats; + d.UploadingSize = a.UploadingSize; StateHasChanged(); return; } diff --git a/src/CloudStorage.Layou/_Imports.razor b/src/CloudStorage.Layou/_Imports.razor index 77e9ffd..8a4522c 100644 --- a/src/CloudStorage.Layou/_Imports.razor +++ b/src/CloudStorage.Layou/_Imports.razor @@ -1,3 +1,4 @@ @using Microsoft.AspNetCore.Components.Web @using Masa.Blazor -@using CloudStorage.Layou.Components; \ No newline at end of file +@using CloudStorage.Layou.Components; +@using Domain.Shared; \ No newline at end of file -- Gitee From 6c18260cf089ee3718d47af8e806e6c7079b5729 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Wed, 3 Aug 2022 00:36:48 +0800 Subject: [PATCH 11/13] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../CloudStorage.Applications.csproj | 2 +- .../CloudStorageApplicationsModule.cs | 5 ++++- .../EventHandle/UploadingEventBus.cs | 8 +++++--- .../CloudStorage.Client.csproj | 4 +++- src/CloudStorage.Client/MauiProgram.cs | 1 + .../appsettings.Development.json | 3 +++ src/CloudStorage.Client/appsettings.json | 3 +++ src/CloudStorage.Client/wwwroot/appsettings.json | 3 --- src/CloudStorage.Layou/CloudStorage.Layou.csproj | 16 ++++++++-------- .../CloudStorageLayouModule.cs | 12 +++++++++++- .../Components/Uploads/UploadTheList.razor | 14 ++++++++++++++ .../Components/Uploads/UploadTheList.razor.cs | 1 - src/CloudStorage.Layou/wwwroot/appsettings.json | 3 --- .../CloudStorageWinfrom.csproj | 2 +- .../appsettings.Development.json | 3 +++ src/CloudStorageWinfrom/appsettings.json | 3 +++ src/CloudStorageWinfrom/wwwroot/appsettings.json | 3 --- 17 files changed, 60 insertions(+), 26 deletions(-) create mode 100644 src/CloudStorage.Client/appsettings.Development.json create mode 100644 src/CloudStorage.Client/appsettings.json delete mode 100644 src/CloudStorage.Client/wwwroot/appsettings.json delete mode 100644 src/CloudStorage.Layou/wwwroot/appsettings.json create mode 100644 src/CloudStorageWinfrom/appsettings.Development.json create mode 100644 src/CloudStorageWinfrom/appsettings.json delete mode 100644 src/CloudStorageWinfrom/wwwroot/appsettings.json diff --git a/src/CloudStorage.Applications/CloudStorage.Applications.csproj b/src/CloudStorage.Applications/CloudStorage.Applications.csproj index 94b19e6..054a334 100644 --- a/src/CloudStorage.Applications/CloudStorage.Applications.csproj +++ b/src/CloudStorage.Applications/CloudStorage.Applications.csproj @@ -18,7 +18,7 @@ - + diff --git a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs index c46d40a..ea4edf2 100644 --- a/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs +++ b/src/CloudStorage.Applications/CloudStorageApplicationsModule.cs @@ -1,6 +1,7 @@ using CloudStoage.Domain; using CloudStorage.Applications.Manage; using CloudStorage.Domain.Shared; +using Microsoft.Extensions.Configuration; using Token.EventBus; using Token.Module; using Token.Module.Attributes; @@ -18,11 +19,13 @@ public class CloudStorageApplicationsModule : TokenModule { var status = services.GetService(); var token = services.GetRequiredService(); + var configuration = services.GetRequiredService(); + x.DefaultRequestHeaders.Add(Constant.Authorization, $"Bearer " + token.Token); // 如果是nginx代理了路由最后要加/不然无法找到路径 - x.BaseAddress = new Uri(Constant.Api.EndsWith("/") ? Constant.Api : Constant.Api + "/"); + x.BaseAddress = new Uri(configuration["HostApi"].EndsWith("/") ? configuration["HostApi"] : configuration["HostApi"] + "/"); }) .ConfigureHttpMessageHandlerBuilder(builder => diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index 8d3b7a4..e7203b8 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -4,6 +4,7 @@ using CloudStorage.Applications.Helpers; using CloudStorage.Domain.Shared; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.SignalR.Client; +using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System.Collections.Concurrent; using System.Threading.Channels; @@ -19,16 +20,17 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle private readonly IKeyLocalEventBus KeyLocalEventBus; private readonly IKeyLocalEventBus DistributedEventBus; - + private readonly IConfiguration _configuration; private readonly TokenManage token; private HubConnection connection; - public UploadingEventBus(IKeyLocalEventBus keyLocalEventBus, TokenManage token, IKeyLocalEventBus distributedEventBus) + public UploadingEventBus(IKeyLocalEventBus keyLocalEventBus, TokenManage token, IKeyLocalEventBus distributedEventBus, IConfiguration configuration) { KeyLocalEventBus = keyLocalEventBus; this.token = token; DistributedEventBus = distributedEventBus; + _configuration = configuration; } public async Task HandleEventAsync(List eventData) @@ -36,7 +38,7 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle if (connection == null || connection.State != HubConnectionState.Connected) { connection = new HubConnectionBuilder() - .WithUrl(Constant.Api + "/file-stream", option => + .WithUrl(_configuration["HostApi"] + "/file-stream", option => { option.AccessTokenProvider = () => Task.FromResult(token.Token); }) diff --git a/src/CloudStorage.Client/CloudStorage.Client.csproj b/src/CloudStorage.Client/CloudStorage.Client.csproj index 6e88995..fe5d8ba 100644 --- a/src/CloudStorage.Client/CloudStorage.Client.csproj +++ b/src/CloudStorage.Client/CloudStorage.Client.csproj @@ -54,8 +54,10 @@ - + Always + true + PreserveNewest diff --git a/src/CloudStorage.Client/MauiProgram.cs b/src/CloudStorage.Client/MauiProgram.cs index ef5c302..39dccd9 100644 --- a/src/CloudStorage.Client/MauiProgram.cs +++ b/src/CloudStorage.Client/MauiProgram.cs @@ -16,6 +16,7 @@ public static class MauiProgram fonts.AddFont("OpenSans-Regular.ttf", "OpenSansRegular"); }); + builder.Services.AddMauiBlazorWebView(); #if DEBUG builder.Services.AddBlazorWebViewDeveloperTools(); diff --git a/src/CloudStorage.Client/appsettings.Development.json b/src/CloudStorage.Client/appsettings.Development.json new file mode 100644 index 0000000..6b5d3ea --- /dev/null +++ b/src/CloudStorage.Client/appsettings.Development.json @@ -0,0 +1,3 @@ +{ + "HostApi": "https://tokenhu.top/cloud-storage" +} \ No newline at end of file diff --git a/src/CloudStorage.Client/appsettings.json b/src/CloudStorage.Client/appsettings.json new file mode 100644 index 0000000..6b5d3ea --- /dev/null +++ b/src/CloudStorage.Client/appsettings.json @@ -0,0 +1,3 @@ +{ + "HostApi": "https://tokenhu.top/cloud-storage" +} \ No newline at end of file diff --git a/src/CloudStorage.Client/wwwroot/appsettings.json b/src/CloudStorage.Client/wwwroot/appsettings.json deleted file mode 100644 index 6705855..0000000 --- a/src/CloudStorage.Client/wwwroot/appsettings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "HostApi": "https://tokenhu.top" -} \ No newline at end of file diff --git a/src/CloudStorage.Layou/CloudStorage.Layou.csproj b/src/CloudStorage.Layou/CloudStorage.Layou.csproj index 2755743..e0c22e4 100644 --- a/src/CloudStorage.Layou/CloudStorage.Layou.csproj +++ b/src/CloudStorage.Layou/CloudStorage.Layou.csproj @@ -6,26 +6,26 @@ enable + + + + - + + + - - - - - - Always - + diff --git a/src/CloudStorage.Layou/CloudStorageLayouModule.cs b/src/CloudStorage.Layou/CloudStorageLayouModule.cs index c187919..bd55c55 100644 --- a/src/CloudStorage.Layou/CloudStorageLayouModule.cs +++ b/src/CloudStorage.Layou/CloudStorageLayouModule.cs @@ -1,4 +1,6 @@ -using CloudStorage.Applications; +using BlazorComponent; +using CloudStorage.Applications; +using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Token.EventBus; using Token.Module; @@ -13,6 +15,14 @@ public class CloudStorageLayouModule : TokenModule { public override void ConfigureServices(IServiceCollection services) { + IConfiguration config = new ConfigurationBuilder() + .AddJsonFile("appsettings.json") + .AddJsonFile("appsettings.Development.json") + .AddEnvironmentVariables() + .Build(); + + services.AddSingleton(config); + services.AddMasaBlazor(); } } diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor index c170533..45cf692 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor @@ -31,6 +31,20 @@ 未知状态 } + @if (d.Stats == UpdateStats.BeUploading || d.Stats == UpdateStats.Suspend) + { +
+ @if (d.Stats == UpdateStats.Suspend) + { + 暂停 + } + else + { + 继续 + } + 取消 +
+ } } diff --git a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs index 0d621b6..b4cd4ac 100644 --- a/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs +++ b/src/CloudStorage.Layou/Components/Uploads/UploadTheList.razor.cs @@ -22,7 +22,6 @@ namespace CloudStorage.Layou.Components.Uploads UploadingList = UploadingEventBus.UploadingList; - await UploadTheListEventBus.Subscribe(KeyLoadNames.UploadingListName, a => { foreach (var d in UploadingList) diff --git a/src/CloudStorage.Layou/wwwroot/appsettings.json b/src/CloudStorage.Layou/wwwroot/appsettings.json deleted file mode 100644 index 6705855..0000000 --- a/src/CloudStorage.Layou/wwwroot/appsettings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "HostApi": "https://tokenhu.top" -} \ No newline at end of file diff --git a/src/CloudStorageWinfrom/CloudStorageWinfrom.csproj b/src/CloudStorageWinfrom/CloudStorageWinfrom.csproj index 80e3b5c..5396315 100644 --- a/src/CloudStorageWinfrom/CloudStorageWinfrom.csproj +++ b/src/CloudStorageWinfrom/CloudStorageWinfrom.csproj @@ -13,7 +13,7 @@ - + Always diff --git a/src/CloudStorageWinfrom/appsettings.Development.json b/src/CloudStorageWinfrom/appsettings.Development.json new file mode 100644 index 0000000..ca8ed50 --- /dev/null +++ b/src/CloudStorageWinfrom/appsettings.Development.json @@ -0,0 +1,3 @@ +{ + "HostApi": "https://localhost:8081" +} \ No newline at end of file diff --git a/src/CloudStorageWinfrom/appsettings.json b/src/CloudStorageWinfrom/appsettings.json new file mode 100644 index 0000000..6b5d3ea --- /dev/null +++ b/src/CloudStorageWinfrom/appsettings.json @@ -0,0 +1,3 @@ +{ + "HostApi": "https://tokenhu.top/cloud-storage" +} \ No newline at end of file diff --git a/src/CloudStorageWinfrom/wwwroot/appsettings.json b/src/CloudStorageWinfrom/wwwroot/appsettings.json deleted file mode 100644 index 6705855..0000000 --- a/src/CloudStorageWinfrom/wwwroot/appsettings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "HostApi": "https://tokenhu.top" -} \ No newline at end of file -- Gitee From 6facd6308ad7ac76f54b7c0b896fffc816139b46 Mon Sep 17 00:00:00 2001 From: "239573049@qq.com" <239573049@qq.com> Date: Wed, 3 Aug 2022 01:31:05 +0800 Subject: [PATCH 12/13] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E6=A8=A1=E5=BC=8F=EF=BC=8C=E4=B8=8A=E4=BC=A0=E9=80=9F=E5=BA=A6?= =?UTF-8?q?=E8=BE=BE=E5=88=B0=E6=9E=81=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CloudStoage.Domain/Etos/UploadingEto.cs | 2 +- .../CloudStorage.Applications.csproj | 4 ++ .../EventHandle/UploadingEventBus.cs | 61 ++++++++++++------- .../Helpers/CommonHelper.cs | 48 ++++++++++++++- .../Helpers/HtttpClientHelper.cs | 43 ------------- src/CloudStorage.Layou/Pages/Storages.razor | 2 - .../Pages/Storages.razor.cs | 39 +----------- 7 files changed, 93 insertions(+), 106 deletions(-) delete mode 100644 src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs diff --git a/src/CloudStoage.Domain/Etos/UploadingEto.cs b/src/CloudStoage.Domain/Etos/UploadingEto.cs index 48d6749..9aa05a3 100644 --- a/src/CloudStoage.Domain/Etos/UploadingEto.cs +++ b/src/CloudStoage.Domain/Etos/UploadingEto.cs @@ -22,5 +22,5 @@ public class UploadingEto /// /// Stream /// - public Stream Stream { get; set; } + public string FilePath { get; set; } } diff --git a/src/CloudStorage.Applications/CloudStorage.Applications.csproj b/src/CloudStorage.Applications/CloudStorage.Applications.csproj index 054a334..33b74c6 100644 --- a/src/CloudStorage.Applications/CloudStorage.Applications.csproj +++ b/src/CloudStorage.Applications/CloudStorage.Applications.csproj @@ -33,4 +33,8 @@ + + + + diff --git a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs index e7203b8..b8551f3 100644 --- a/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs +++ b/src/CloudStorage.Applications/EventHandle/UploadingEventBus.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.SignalR.Client; using Microsoft.Extensions.Configuration; using Newtonsoft.Json; using System.Collections.Concurrent; +using System.Diagnostics; using System.Threading.Channels; using Token.EventBus; using Token.EventBus.Handlers; @@ -62,35 +63,49 @@ public class UploadingEventBus : ILocalEventHandler>, ISingle int size = (1024 * 10); foreach (var item in eventData) { - int length = (int)(item.Length / size); - var channel = Channel.CreateBounded(length + 1); - - // 建立传输通道 - await connection.SendAsync("FileStreamSaveAsync", channel.Reader, JsonConvert.SerializeObject(new + FileStream fileStream = null; + try { - item.StorageId, - item.FileName, - item.Length - })); + fileStream = File.OpenRead(item.FilePath); - var bytesTransferred = 0; + item.Length = fileStream.Length; - // 定义下载缓存 - var b = new byte[size > item.Length ? item.Length ?? 0 : size]; - int len; - while ((len = await item.Stream.ReadAsync(b)) != 0) - { + int length = (int)(item.Length / size); + var channel = Channel.CreateBounded(length + 1); - await channel.Writer.WriteAsync(b); - bytesTransferred += len; - await UploadingSizeEvent(item.Id, bytesTransferred); - } + // 建立传输通道 + await connection.SendAsync("FileStreamSaveAsync", channel.Reader, JsonConvert.SerializeObject(new + { + item.StorageId, + item.FileName, + item.Length + })); + + var bytesTransferred = 0; + + // 定义下载缓存 + var b = new byte[size > item.Length ? item.Length ?? 0 : size]; + int len; + var sw = Stopwatch.StartNew(); + while ((len = await fileStream.ReadAsync(b)) != 0) + { - // 传输完成结束通道 - channel.Writer.Complete(); - await DistributedEventBus.PublishAsync("Storages", "上传文件成功"); + await channel.Writer.WriteAsync(b); + bytesTransferred += len; + await UploadingSizeEvent(item.Id, bytesTransferred); + } + sw.Stop(); + + // 传输完成结束通道 + channel.Writer.Complete(); + await DistributedEventBus.PublishAsync("Storages", "上传文件成功"); - await UploadingSizeEvent(item.Id, succeed: true); + await UploadingSizeEvent(item.Id, succeed: true); + } + finally + { + fileStream?.Close(); + } } } diff --git a/src/CloudStorage.Applications/Helpers/CommonHelper.cs b/src/CloudStorage.Applications/Helpers/CommonHelper.cs index 6b1ef14..ecfea2f 100644 --- a/src/CloudStorage.Applications/Helpers/CommonHelper.cs +++ b/src/CloudStorage.Applications/Helpers/CommonHelper.cs @@ -1,9 +1,24 @@ -using Token.Module.Dependencys; +using CloudStoage.Domain.Etos; +using Token.EventBus.EventBus; +using Token.Module.Dependencys; namespace CloudStorage.Applications.Helpers; public class CommonHelper : IScopedDependency { + + private readonly ILocalEventBus LocalEventBus; + + public CommonHelper(ILocalEventBus localEventBus) + { + LocalEventBus = localEventBus; + } + + /// + /// b转换格式 + /// + /// + /// public string GetFileSize(long? size) { if (size == null) @@ -22,4 +37,35 @@ public class CommonHelper : IScopedDependency return ((long)size / Math.Pow(num, 4)).ToString("f2") + "T"; //T } + + public async Task PickAndShow(Guid? storageId) + { + PickOptions options = new() + { + PickerTitle = "请选择需要上传的文件", + }; + + try + { + var result = await FilePicker.Default.PickMultipleAsync(options); + if (result != null) + { + + var uploadings = result.Select(x => new UploadingEto + { + Id = Guid.NewGuid(), + FileName = x.FileName, + FilePath = x.FullPath, + StorageId = storageId, + }).ToList(); + + _ = LocalEventBus.PublishAsync(uploadings, false); + } + + } + catch (Exception ex) + { + } + + } } diff --git a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs b/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs deleted file mode 100644 index 31609b8..0000000 --- a/src/CloudStorage.Applications/Helpers/HtttpClientHelper.cs +++ /dev/null @@ -1,43 +0,0 @@ -using CloudStoage.Domain; -using CloudStoage.Domain.Etos; -using Microsoft.AspNetCore.Components.Forms; -using System.Net.Http.Handlers; -using Token.Module.Dependencys; - -namespace CloudStorage.Applications.Helpers; - -public class HtttpClientHelper : IScopedDependency -{ - private const string Name = "api/storage"; - - private readonly IHttpClientFactory httpClientFactory; - - public HtttpClientHelper(IHttpClientFactory httpClientFactory) - { - this.httpClientFactory = httpClientFactory; - } - - public async Task UpdateRand(UploadingEto files, EventHandler eventHandler = null) - { - var http = httpClientFactory.CreateClient(string.Empty); - HttpClientHandler handler = new(); - ProgressMessageHandler progressMessageHandler = new(handler); - progressMessageHandler.HttpSendProgress += eventHandler; - - using HttpClient httpClient = new(progressMessageHandler); - - httpClient.BaseAddress = new Uri(Constant.Api); - httpClient.DefaultRequestHeaders - .Add(Constant.Authorization, http.DefaultRequestHeaders.FirstOrDefault(x => x.Key == Constant.Authorization).Value); - httpClient.DefaultRequestHeaders.Add("id", files.Id.ToString()); - - using var multipartFormData = new MultipartFormDataContent - { - { new StreamContent(files.Stream), "file", files.FileName } - }; - - var response = await httpClient.PostAsync(Name + "/upload-file?storageId=" + files.StorageId, multipartFormData); - - } - -} diff --git a/src/CloudStorage.Layou/Pages/Storages.razor b/src/CloudStorage.Layou/Pages/Storages.razor index b7dada0..e8d04c6 100644 --- a/src/CloudStorage.Layou/Pages/Storages.razor +++ b/src/CloudStorage.Layou/Pages/Storages.razor @@ -67,8 +67,6 @@ -