From b747655d00aa3d5d90f269aed32bc0aa289e48b4 Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Sat, 27 Sep 2025 00:34:48 +0100 Subject: [PATCH] Refactor MediaInformation to use a sorted set for preventing crashes on duplicate media types. --- .../Components/Pages/Report/View.razor | 8 ++--- .../Components/Pages/Report/View.razor.cs | 29 +++++++++++-------- Aaru.Server/Core/SscTestedMedia.cs | 23 ++++++++++----- Aaru.Server/Core/TestedMedia.cs | 19 +++++++++--- 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/Aaru.Server/Components/Pages/Report/View.razor b/Aaru.Server/Components/Pages/Report/View.razor index 7c657ca5..15a109ef 100644 --- a/Aaru.Server/Components/Pages/Report/View.razor +++ b/Aaru.Server/Components/Pages/Report/View.razor @@ -684,16 +684,16 @@ - @foreach(KeyValuePair Table, List List)> mediaInfo in MediaInformation) + @foreach((string Header, Dictionary Table, List List) mediaInfo in MediaInformation) { - @mediaInfo.Key + @mediaInfo.Header - @foreach(KeyValuePair kvp in mediaInfo.Value.Table) + @foreach(KeyValuePair kvp in mediaInfo.Table) { @@ -703,7 +703,7 @@
@kvp.Key
    - @foreach(string cap in mediaInfo.Value.List) + @foreach(string cap in mediaInfo.List) {
  • @cap
  • } diff --git a/Aaru.Server/Components/Pages/Report/View.razor.cs b/Aaru.Server/Components/Pages/Report/View.razor.cs index dc46dac3..1d12c2c8 100644 --- a/Aaru.Server/Components/Pages/Report/View.razor.cs +++ b/Aaru.Server/Components/Pages/Report/View.razor.cs @@ -3,8 +3,8 @@ using Aaru.CommonTypes.Structs.Devices.SCSI; using Aaru.Decoders.PCMCIA; using Aaru.Decoders.SCSI; using Aaru.Helpers; -using Aaru.Server.Database.Models; using Aaru.Server.Core; +using Aaru.Server.Database.Models; using Microsoft.AspNetCore.Components; using Microsoft.EntityFrameworkCore; using Ata = Aaru.CommonTypes.Metadata.Ata; @@ -69,7 +69,11 @@ public partial class View public List? MmcModeList { get; set; } public Dictionary>? EvpdPages { get; set; } - public Dictionary Table, List List)>? MediaInformation { get; set; } + public SortedSet<(string Header, Dictionary Table, List List)>? MediaInformation + { + get; + set; + } /// protected override async Task OnInitializedAsync() @@ -280,10 +284,10 @@ public partial class View } } - var removable = true; + bool removable = true; List? testedMedia = null; - var atapi = false; - var sscMedia = false; + bool atapi = false; + bool sscMedia = false; if(report.ATA != null || report.ATAPI != null) { @@ -396,7 +400,7 @@ public partial class View if(report.SCSI != null) { - var vendorId = ""; + string? vendorId = ""; if(report.SCSI.Inquiry != null) { @@ -406,9 +410,10 @@ public partial class View DeviceInquiry = new Dictionary { { - "Vendor:", VendorString.Prettify(vendorId) != vendorId - ? $"{vendorId} ({VendorString.Prettify(vendorId)})" - : vendorId + "Vendor:", + VendorString.Prettify(vendorId) != vendorId + ? $"{vendorId} ({VendorString.Prettify(vendorId)})" + : vendorId }, { "Product:", StringHandlers.CToString(inq.ProductIdentification) @@ -511,8 +516,8 @@ public partial class View sscMedia = true; SscTestedMedia.Report(report.SCSI.SequentialDevice.TestedMedia, - out Dictionary Table, List List)> - mediaInformation); + out SortedSet<(string Header, Dictionary Table, List + List)> mediaInformation); if(mediaInformation.Count > 0) MediaInformation = mediaInformation; } @@ -605,7 +610,7 @@ public partial class View if(removable && !sscMedia && testedMedia != null) { Core.TestedMedia.Report(testedMedia, - out Dictionary Table, List List)> + out SortedSet<(string Header, Dictionary Table, List List)> mediaInformation); if(mediaInformation.Count > 0) MediaInformation = mediaInformation; diff --git a/Aaru.Server/Core/SscTestedMedia.cs b/Aaru.Server/Core/SscTestedMedia.cs index 40aa25c0..ca747fee 100644 --- a/Aaru.Server/Core/SscTestedMedia.cs +++ b/Aaru.Server/Core/SscTestedMedia.cs @@ -36,14 +36,15 @@ namespace Aaru.Server.Core; public static class SscTestedMedia { - /// Takes the tested media from SCSI Streaming devices of a device report and prints it as a list of values - /// List to put values on + /// Takes the tested media from SCSI Streaming devices of a device report and prints it as a sorted set of values /// List of tested media public static void Report(IEnumerable testedMedia, - out Dictionary Table, List List)> + out SortedSet<(string Header, Dictionary Table, List List)> mediaInformation) { - mediaInformation = []; + mediaInformation = + new SortedSet<(string Header, Dictionary Table, List List + )>(new MediaInfoComparer()); foreach(TestedSequentialMedia media in testedMedia) { @@ -54,7 +55,6 @@ public static class SscTestedMedia if(!string.IsNullOrWhiteSpace(media.MediumTypeName)) { header = $"Information for medium named \"{media.MediumTypeName}\""; - if(media.MediumType.HasValue) table.Add("Medium type code", $"{media.MediumType:X2}h"); } else if(media.MediumType.HasValue) @@ -62,8 +62,7 @@ public static class SscTestedMedia else header = "Information for unknown medium type"; - if(!string.IsNullOrWhiteSpace(media.Manufacturer)) - table.Add("Medium manufacturer", media.Manufacturer); + if(!string.IsNullOrWhiteSpace(media.Manufacturer)) table.Add("Medium manufacturer", media.Manufacturer); if(!string.IsNullOrWhiteSpace(media.Model)) table.Add("Medium model", media.Model); @@ -73,7 +72,15 @@ public static class SscTestedMedia if(media.MediaIsRecognized) list.Add("Drive recognizes this medium."); - mediaInformation.Add(header, (table, list)); + mediaInformation.Add((header, table, list)); } } + + private sealed class + MediaInfoComparer : IComparer<(string Header, Dictionary Table, List List)> + { + public int Compare((string Header, Dictionary Table, List List) x, + (string Header, Dictionary Table, List List) y) => + string.Compare(x.Header, y.Header, StringComparison.Ordinal); + } } \ No newline at end of file diff --git a/Aaru.Server/Core/TestedMedia.cs b/Aaru.Server/Core/TestedMedia.cs index 8a185b3c..dec8e319 100644 --- a/Aaru.Server/Core/TestedMedia.cs +++ b/Aaru.Server/Core/TestedMedia.cs @@ -37,10 +37,13 @@ public static class TestedMedia /// Takes the tested media from a device report and prints it as a list of values /// List to put values on /// List of tested media - public static void Report(List testedMedias, - out Dictionary Table, List List)> mediaInformation) + public static void Report(List testedMedias, + out SortedSet<(string Header, Dictionary Table, List List)> + mediaInformation) { - mediaInformation = []; + mediaInformation = + new SortedSet<(string Header, Dictionary Table, List List + )>(new MediaInfoComparer()); foreach(CommonTypes.Metadata.TestedMedia testedMedia in testedMedias) { @@ -395,7 +398,15 @@ public static class TestedMedia if(testedMedia.CanReadF1_06LeadOut == true) list.Add("Device can read Lead-Out from cache using F1h command with subcommand 06h"); - mediaInformation.Add(header, (table, list)); + mediaInformation.Add((header, table, list)); } } + + private sealed class + MediaInfoComparer : IComparer<(string Header, Dictionary Table, List List)> + { + public int Compare((string Header, Dictionary Table, List List) x, + (string Header, Dictionary Table, List List) y) => + string.Compare(x.Header, y.Header, StringComparison.Ordinal); + } } \ No newline at end of file