From 026b9d0cb1bb440af39f20ea5e85252d9ba0557e Mon Sep 17 00:00:00 2001 From: JT Date: Sat, 6 Apr 2024 13:34:14 -0700 Subject: [PATCH] Only download images from civitai with Type == "image" since they finally added that property --- CHANGELOG.md | 4 ++++ .../Services/ModelDownloadLinkHandler.cs | 5 ++++- .../CheckpointBrowserCardViewModel.cs | 9 +++++++-- .../ViewModels/CheckpointManager/CheckpointFolder.cs | 2 +- .../Dialogs/RecommendedModelItemViewModel.cs | 4 ++-- .../ViewModels/Dialogs/RecommendedModelsViewModel.cs | 5 ++++- .../Dialogs/SelectModelVersionViewModel.cs | 2 +- StabilityMatrix.Core/Models/Api/CivitImage.cs | 3 +++ .../Services/MetadataImportService.cs | 12 +++++++++--- 9 files changed, 35 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 38097f46..73eb4c18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,10 @@ All notable changes to Stability Matrix will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and this project adheres to [Semantic Versioning 2.0](https://semver.org/spec/v2.0.0.html). +## v2.10.0 +### Fixed +- Fixed [#559](https://github.com/LykosAI/StabilityMatrix/issues/559) - "Unable to load bitmap from provided data" error in Checkpoints page + ## v2.10.0-pre.2 ### Added - Added more metadata to the image dialog info flyout diff --git a/StabilityMatrix.Avalonia/Services/ModelDownloadLinkHandler.cs b/StabilityMatrix.Avalonia/Services/ModelDownloadLinkHandler.cs index 326d715c..62145809 100644 --- a/StabilityMatrix.Avalonia/Services/ModelDownloadLinkHandler.cs +++ b/StabilityMatrix.Avalonia/Services/ModelDownloadLinkHandler.cs @@ -229,7 +229,10 @@ public class ModelDownloadLinkHandler( return null; } - var image = modelVersion.Images[0]; + var image = modelVersion.Images.FirstOrDefault(x => x.Type == "image"); + if (image is null) + return null; + var imageExtension = Path.GetExtension(image.Url).TrimStart('.'); if (imageExtension is "jpg" or "jpeg" or "png") { diff --git a/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CheckpointBrowserCardViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CheckpointBrowserCardViewModel.cs index 341a3913..7edf9920 100644 --- a/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CheckpointBrowserCardViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/CheckpointBrowser/CheckpointBrowserCardViewModel.cs @@ -159,7 +159,9 @@ public partial class CheckpointBrowserCardViewModel : Base.ProgressViewModel // Try to find a valid image var image = images - ?.Where(img => LocalModelFile.SupportedImageExtensions.Any(img.Url.Contains)) + ?.Where( + img => LocalModelFile.SupportedImageExtensions.Any(img.Url.Contains) && img.Type == "image" + ) .FirstOrDefault(image => nsfwEnabled || image.NsfwLevel <= 1); if (image != null) { @@ -295,7 +297,10 @@ public partial class CheckpointBrowserCardViewModel : Base.ProgressViewModel return null; } - var image = modelVersion.Images[0]; + var image = modelVersion.Images.FirstOrDefault(x => x.Type == "image"); + if (image is null) + return null; + var imageExtension = Path.GetExtension(image.Url).TrimStart('.'); if (imageExtension is "jpg" or "jpeg" or "png") { diff --git a/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFolder.cs b/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFolder.cs index e11cccdc..39408129 100644 --- a/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFolder.cs +++ b/StabilityMatrix.Avalonia/ViewModels/CheckpointManager/CheckpointFolder.cs @@ -500,7 +500,7 @@ public partial class CheckpointFolder : ViewModelBase await modelInfo.SaveJsonToDirectory(DirectoryPath, modelFileName); // If available, save thumbnail - var image = version.Images?.FirstOrDefault(); + var image = version.Images?.FirstOrDefault(x => x.Type == "image"); if (image != null) { var imageExt = Path.GetExtension(image.Url).TrimStart('.'); diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelItemViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelItemViewModel.cs index 3de1d5da..501fa5db 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelItemViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelItemViewModel.cs @@ -22,9 +22,9 @@ public partial class RecommendedModelItemViewModel : ViewModelBase private CivitModel civitModel; public Uri ThumbnailUrl => - ModelVersion.Images?.FirstOrDefault()?.Url == null + ModelVersion.Images?.FirstOrDefault(x => x.Type == "image")?.Url == null ? Assets.NoImage - : new Uri(ModelVersion.Images.First().Url); + : new Uri(ModelVersion.Images.First(x => x.Type == "image").Url); [RelayCommand] public void ToggleSelection() => IsSelected = !IsSelected; diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelsViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelsViewModel.cs index a2aa480e..5eeaeb6a 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelsViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/RecommendedModelsViewModel.cs @@ -206,7 +206,10 @@ public partial class RecommendedModelsViewModel : ContentDialogViewModelBase return null; } - var image = modelVersion.Images[0]; + var image = modelVersion.Images.FirstOrDefault(x => x.Type == "image"); + if (image is null) + return null; + var imageExtension = Path.GetExtension(image.Url).TrimStart('.'); if (imageExtension is "jpg" or "jpeg" or "png") { diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs index 338aa20d..8327e996 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/SelectModelVersionViewModel.cs @@ -99,7 +99,7 @@ public partial class SelectModelVersionViewModel( var allImages = value ?.ModelVersion ?.Images - ?.Where(img => nsfwEnabled || img.NsfwLevel <= 1) + ?.Where(img => img.Type == "image" && (nsfwEnabled || img.NsfwLevel <= 1)) ?.Select(x => new ImageSource(x.Url)) .ToList(); diff --git a/StabilityMatrix.Core/Models/Api/CivitImage.cs b/StabilityMatrix.Core/Models/Api/CivitImage.cs index 82aadce2..c29e3bac 100644 --- a/StabilityMatrix.Core/Models/Api/CivitImage.cs +++ b/StabilityMatrix.Core/Models/Api/CivitImage.cs @@ -19,5 +19,8 @@ public class CivitImage [JsonPropertyName("hash")] public string Hash { get; set; } + [JsonPropertyName("type")] + public string Type { get; set; } + // TODO: "meta" ( object? ) } diff --git a/StabilityMatrix.Core/Services/MetadataImportService.cs b/StabilityMatrix.Core/Services/MetadataImportService.cs index d31a603d..21ce5a2c 100644 --- a/StabilityMatrix.Core/Services/MetadataImportService.cs +++ b/StabilityMatrix.Core/Services/MetadataImportService.cs @@ -107,7 +107,9 @@ public class MetadataImportService( .ConfigureAwait(false); var image = modelVersion.Images?.FirstOrDefault( - img => LocalModelFile.SupportedImageExtensions.Contains(Path.GetExtension(img.Url)) + img => + LocalModelFile.SupportedImageExtensions.Contains(Path.GetExtension(img.Url)) + && img.Type == "image" ); if (image == null) { @@ -201,7 +203,9 @@ public class MetadataImportService( .ConfigureAwait(false); var image = modelVersion.Images?.FirstOrDefault( - img => LocalModelFile.SupportedImageExtensions.Contains(Path.GetExtension(img.Url)) + img => + LocalModelFile.SupportedImageExtensions.Contains(Path.GetExtension(img.Url)) + && img.Type == "image" ); if (image == null) continue; @@ -264,7 +268,9 @@ public class MetadataImportService( .ConfigureAwait(false); var image = modelVersion.Images?.FirstOrDefault( - img => LocalModelFile.SupportedImageExtensions.Contains(Path.GetExtension(img.Url)) + img => + LocalModelFile.SupportedImageExtensions.Contains(Path.GetExtension(img.Url)) + && img.Type == "image" ); if (image == null)