From 9fc4bb70553dda53c8b56e1cdeaea39e6f193f32 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 15:09:01 +0900 Subject: [PATCH 1/3] Fix incorrect xmldoc --- osu.Game/IO/Archives/LegacyByteArrayReader.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/IO/Archives/LegacyByteArrayReader.cs b/osu.Game/IO/Archives/LegacyByteArrayReader.cs index 0c3620403f..ea8ff3bbe0 100644 --- a/osu.Game/IO/Archives/LegacyByteArrayReader.cs +++ b/osu.Game/IO/Archives/LegacyByteArrayReader.cs @@ -7,7 +7,7 @@ using System.IO; namespace osu.Game.IO.Archives { /// - /// Allows reading a single file from the provided stream. + /// Allows reading a single file from the provided byte array. /// public class LegacyByteArrayReader : ArchiveReader { From adf81d7fcd024e30a943a59d3f7ed9cc3584c3c7 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 16:39:17 +0900 Subject: [PATCH 2/3] Add pathway to correctly handle stream-based imports which are not zip archives --- osu.Game/Database/ImportTask.cs | 26 +++++++++++++++++++++++--- osu.Game/Utils/ZipUtils.cs | 28 ++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/osu.Game/Database/ImportTask.cs b/osu.Game/Database/ImportTask.cs index 1433a567a9..1fb5a42630 100644 --- a/osu.Game/Database/ImportTask.cs +++ b/osu.Game/Database/ImportTask.cs @@ -47,10 +47,30 @@ namespace osu.Game.Database /// public ArchiveReader GetReader() { - if (Stream != null) - return new ZipArchiveReader(Stream, Path); + return Stream != null + ? getReaderFrom(Stream) + : getReaderFrom(Path); + } - return getReaderFrom(Path); + /// + /// Creates an from a stream. + /// + /// A seekable stream containing the archive content. + /// A reader giving access to the archive's content. + private ArchiveReader getReaderFrom(Stream stream) + { + if (!(stream is MemoryStream memoryStream)) + { + // This isn't used in any current path. May need to reconsider for performance reasons (ie. if we don't expect the incoming stream to be copied out). + byte[] buffer = new byte[stream.Length]; + stream.Read(buffer, 0, (int)stream.Length); + memoryStream = new MemoryStream(buffer); + } + + if (ZipUtils.IsZipArchive(memoryStream)) + return new ZipArchiveReader(memoryStream, Path); + + return new LegacyByteArrayReader(memoryStream.ToArray(), Path); } /// diff --git a/osu.Game/Utils/ZipUtils.cs b/osu.Game/Utils/ZipUtils.cs index cd4d876451..16eb1c7e4e 100644 --- a/osu.Game/Utils/ZipUtils.cs +++ b/osu.Game/Utils/ZipUtils.cs @@ -9,6 +9,34 @@ namespace osu.Game.Utils { public static class ZipUtils { + public static bool IsZipArchive(Stream stream) + { + try + { + stream.Seek(0, SeekOrigin.Begin); + + using (var arc = ZipArchive.Open(stream)) + { + foreach (var entry in arc.Entries) + { + using (entry.OpenEntryStream()) + { + } + } + } + + return true; + } + catch (Exception) + { + return false; + } + finally + { + stream.Seek(0, SeekOrigin.Begin); + } + } + public static bool IsZipArchive(string path) { if (!File.Exists(path)) From f02b57c37121a77523e4c54aa1a2fa5fc14a3cc1 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Fri, 12 Nov 2021 19:16:22 +0900 Subject: [PATCH 3/3] Limit new `IsZipArchive` method to `MemoryStream` for now MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bartłomiej Dach --- osu.Game/Utils/ZipUtils.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/osu.Game/Utils/ZipUtils.cs b/osu.Game/Utils/ZipUtils.cs index 16eb1c7e4e..eb2d2d3b80 100644 --- a/osu.Game/Utils/ZipUtils.cs +++ b/osu.Game/Utils/ZipUtils.cs @@ -9,7 +9,7 @@ namespace osu.Game.Utils { public static class ZipUtils { - public static bool IsZipArchive(Stream stream) + public static bool IsZipArchive(MemoryStream stream) { try {