diff --git a/DownKyi.Core/Storage/StorageCover.cs b/DownKyi.Core/Storage/StorageCover.cs index b48106e..d52e010 100644 --- a/DownKyi.Core/Storage/StorageCover.cs +++ b/DownKyi.Core/Storage/StorageCover.cs @@ -3,6 +3,7 @@ using DownKyi.Core.Logging; using DownKyi.Core.Storage.Database; using DownKyi.Core.Utils.Encryptor; +using System.Threading; using Console = DownKyi.Core.Utils.Debugging.Console; namespace DownKyi.Core.Storage; @@ -32,15 +33,6 @@ public Bitmap GetCoverThumbnail(long avid, string bvid, long cid, string url, in return GetCoverThumbnail(header, width, height); } - public Task GetCoverThumbnailAsync(long avid, string bvid, long cid, string url, int width, int height) - { - return Task.Run(() => - { - var header = GetCover(avid, bvid, cid, url); - return GetCoverThumbnail(header, width, height); - }); - } - /// /// 获取封面缩略图 /// @@ -184,6 +176,37 @@ public string GetCover(long avid, string bvid, long cid, string url) } } + public async Task GetCoverAsync(string url,TimeSpan? timeOut = null) + { + timeOut = timeOut ?? TimeSpan.FromSeconds(1.5); + using var stream = await GetImageBytesAsync(url, timeOut); + return stream == null ? null : new Bitmap(stream); + } + + + public async Task GetImageBytesAsync(string imageUrl,TimeSpan? timeOut = null) + { + try + { + using (var httpClient = new HttpClient()) + { + if (timeOut is not null) + { + httpClient.Timeout = timeOut.Value; + } + var response = await httpClient.GetAsync(imageUrl); + response.EnsureSuccessStatusCode(); + + var stream = await response.Content.ReadAsStreamAsync(); + return stream; + } + } + catch (Exception e) + { + return null; + } + } + /// /// 下载图片 /// diff --git a/DownKyi/ViewModels/ViewPublicationViewModel.cs b/DownKyi/ViewModels/ViewPublicationViewModel.cs index 28d3470..f04c953 100644 --- a/DownKyi/ViewModels/ViewPublicationViewModel.cs +++ b/DownKyi/ViewModels/ViewPublicationViewModel.cs @@ -20,6 +20,7 @@ using DownKyi.Utils; using DownKyi.ViewModels.PageViewModels; using DownKyi.ViewModels.UserSpace; +using DryIoc; using Prism.Commands; using Prism.Events; using Prism.Regions; @@ -389,7 +390,7 @@ private bool OnCurrentChanged_Pager(int old, int current) return true; } - + private static string StringToUnicode(string s) { var charbuffers = s.ToCharArray(); @@ -402,7 +403,7 @@ private static string StringToUnicode(string s) } return sb.ToString(); } - + private readonly Bitmap defaultPic = ImageHelper.LoadFromResource(new Uri("avares://DownKyi/Resources/video-placeholder.png")); private async Task UpdatePublication(int current) { _tokenSource?.Cancel(); @@ -411,9 +412,9 @@ private async Task UpdatePublication(int current) IsEnabled = false; _tokenSource = new CancellationTokenSource(); var cancellationToken = _tokenSource.Token; - var defaultPic = ImageHelper.LoadFromResource(new Uri("avares://DownKyi/Resources/video-placeholder.png")); - var tab = TabHeaders[SelectTabId]; + var tab = TabHeaders[SelectTabId]; + var storageCover = new StorageCover(); await Task.Run(async () => { var publications = Core.BiliApi.Users.UserSpace.GetPublication(_mid, current, _videoNumberInPage, tab.Id); @@ -438,7 +439,7 @@ await Task.Run(async () => { // 查询、保存封面 var coverUrl = video.Pic; - + // 播放数 var play = string.Empty; if (video.Play > 0) @@ -453,7 +454,6 @@ await Task.Run(async () => var startTime = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1)); // 当地时区 var dateCTime = startTime.AddSeconds(video.Created); var ctime = dateCTime.ToString("yyyy-MM-dd"); - App.PropertyChangeAsync(() => { var media = new PublicationMedia(EventAggregator) @@ -482,19 +482,21 @@ await Task.Run(async () => await UpdateMediaCovers(cancellationToken); }, cancellationToken).ContinueWith(t => { }); } - + private readonly StorageCover storageCover = new StorageCover(); private async Task UpdateMediaCovers(CancellationToken cancellationToken) { - var storageCover = new StorageCover(); var currentMedias = _medias.ToList(); - foreach (var media in currentMedias) + var tasks = currentMedias.Select(async media => { if (cancellationToken.IsCancellationRequested) { return; } - media.Cover = await storageCover.GetCoverThumbnailAsync(media.Avid, media.Bvid, -1, media.CoverUrl, 200, 125); - } + var coverUrl = $"{media.CoverUrl}@{200}w_{125}h_1c_!web-space-index-myvideo.webp"; + var bitmap = await storageCover.GetCoverAsync(coverUrl) ?? defaultPic; + media.Cover = bitmap; + }); + await Task.WhenAll(tasks); } /// @@ -529,7 +531,6 @@ public override void OnNavigatedTo(NavigationContext navigationContext) { return; } - InitView(); _mid = (long)parameter["mid"];