From a47b5995cb891c34c0574514603fa7cdf3b327e1 Mon Sep 17 00:00:00 2001 From: Poker Date: Wed, 1 Jan 2025 00:25:26 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=8A=A8=E5=9B=BE=E4=B8=8D?= =?UTF-8?q?=E6=98=BE=E7=A4=BA=20(#586)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Global/Exception/MakoNetworkException.cs | 2 +- src/Pixeval.Utilities/Streams.cs | 25 +++++++++++-------- src/Pixeval/Util/IO/IOHelper.Imaging.cs | 6 ++--- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/Pixeval.CoreApi/Global/Exception/MakoNetworkException.cs b/src/Pixeval.CoreApi/Global/Exception/MakoNetworkException.cs index d611816a..f77ec26f 100644 --- a/src/Pixeval.CoreApi/Global/Exception/MakoNetworkException.cs +++ b/src/Pixeval.CoreApi/Global/Exception/MakoNetworkException.cs @@ -24,7 +24,7 @@ using System.Threading.Tasks; namespace Pixeval.CoreApi.Global.Exception; public class MakoNetworkException(string url, bool domainFronting, string? extraMsg, int statusCode) - : MakoException($"Network error while requesting URL: {url}(Domain fronting: {domainFronting}, Status code: {statusCode}) {extraMsg}") + : MakoException($"Network error while requesting URL: {url} (Domain fronting: {domainFronting}, Status code: {statusCode}) {extraMsg}") { public string Url { get; set; } = url; diff --git a/src/Pixeval.Utilities/Streams.cs b/src/Pixeval.Utilities/Streams.cs index 0b2d2378..f7ad5930 100644 --- a/src/Pixeval.Utilities/Streams.cs +++ b/src/Pixeval.Utilities/Streams.cs @@ -24,6 +24,7 @@ using System.IO.Compression; using System.IO; using System.Threading.Tasks; using Microsoft.IO; +using System.Linq; namespace Pixeval.Utilities; @@ -61,7 +62,7 @@ public static class Streams return s; } - public static async Task ReadZipAsync(Stream zipStream, bool dispose) + public static async Task> ReadZipAsync(Stream zipStream, bool dispose) { if (zipStream is not MemoryStream ms) { @@ -71,19 +72,21 @@ public static class Streams try { - using var archive = new ZipArchive(ms, ZipArchiveMode.Read); - var result = (MemoryStream[])[]; - await Parallel.ForAsync(0, archive.Entries.Count, new ParallelOptions(), async (i, token) => + var list = new List(); + // Dispose ZipArchive会导致ms也被Dispose + var archive = new ZipArchive(ms, ZipArchiveMode.Read); + // ZipArchive不支持多线程 + foreach (var entry in archive.Entries) { - await using var stream = archive.Entries[i].Open(); - var rms = _recyclableMemoryStreamManager.GetStream(); - await stream.CopyToAsync(rms, token); - rms.Position = 0; - result[i] = rms; - }); + await using var stream = entry.Open(); + var s = _recyclableMemoryStreamManager.GetStream(); + await stream.CopyToAsync(s); + s.Position = 0; + list.Add(s); + } if (dispose) await ms.DisposeAsync(); - return result; + return list; } catch (InvalidDataException) { diff --git a/src/Pixeval/Util/IO/IOHelper.Imaging.cs b/src/Pixeval/Util/IO/IOHelper.Imaging.cs index f07f911a..45151377 100644 --- a/src/Pixeval/Util/IO/IOHelper.Imaging.cs +++ b/src/Pixeval/Util/IO/IOHelper.Imaging.cs @@ -128,14 +128,14 @@ public static partial class IoHelper public static async Task UgoiraSaveToImageAsync(this Stream stream, IReadOnlyList delays, IProgress? progress = null, bool dispose = false) { var streams = await Streams.ReadZipAsync(stream, dispose); - var average = 50d / streams.Length; + var average = 50d / streams.Count; var progressValue = 0d; - var images = new Image[streams.Length]; + var images = new Image[streams.Count]; var options = new ParallelOptions(); if (progress is not null) options.TaskScheduler = TaskScheduler.FromCurrentSynchronizationContext(); - await Parallel.ForAsync(0, streams.Length, options, async (i, token) => + await Parallel.ForAsync(0, streams.Count, options, async (i, token) => { var delay = delays.Count > i ? (uint)delays[i] : 10u; streams[i].Position = 0;