From 7e80d9847d46052b55ec916a1fc4e27016711e12 Mon Sep 17 00:00:00 2001 From: Ionite Date: Sat, 17 Feb 2024 16:20:19 -0500 Subject: [PATCH] Auto extract for TrackedDownload and RemoteResource --- .../Dialogs/DownloadResourceViewModel.cs | 49 ++++++++++++++----- .../Dialogs/DownloadResourceDialog.axaml | 2 +- StabilityMatrix.Core/Models/RemoteResource.cs | 10 +++- 3 files changed, 48 insertions(+), 13 deletions(-) diff --git a/StabilityMatrix.Avalonia/ViewModels/Dialogs/DownloadResourceViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/Dialogs/DownloadResourceViewModel.cs index 47625eaf..9174c1aa 100644 --- a/StabilityMatrix.Avalonia/ViewModels/Dialogs/DownloadResourceViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/Dialogs/DownloadResourceViewModel.cs @@ -1,4 +1,5 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; using Avalonia.Controls; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; @@ -8,8 +9,10 @@ using StabilityMatrix.Avalonia.Languages; using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.Views.Dialogs; using StabilityMatrix.Core.Attributes; +using StabilityMatrix.Core.Extensions; using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Models; +using StabilityMatrix.Core.Models.FileInterfaces; using StabilityMatrix.Core.Processes; using StabilityMatrix.Core.Services; @@ -18,15 +21,17 @@ namespace StabilityMatrix.Avalonia.ViewModels.Dialogs; [View(typeof(DownloadResourceDialog))] [ManagedService] [Transient] -public partial class DownloadResourceViewModel : ContentDialogViewModelBase +public partial class DownloadResourceViewModel( + IDownloadService downloadService, + ISettingsManager settingsManager, + ITrackedDownloadService trackedDownloadService +) : ContentDialogViewModelBase { - private readonly IDownloadService downloadService; - [ObservableProperty] [NotifyPropertyChangedFor(nameof(FileNameWithHash))] private string? fileName; - public string FileNameWithHash => $"{FileName} [{Resource.HashSha256.ToLowerInvariant()[..7]}]"; + public string FileNameWithHash => $"{FileName} [{Resource.HashSha256?.ToLowerInvariant()[..7]}]"; [ObservableProperty] [NotifyPropertyChangedFor(nameof(FileSizeText))] @@ -37,12 +42,7 @@ public partial class DownloadResourceViewModel : ContentDialogViewModelBase public string? FileSizeText => FileSize == 0 ? null : Size.FormatBase10Bytes(FileSize); - public string ShortHash => Resource.HashSha256.ToLowerInvariant(); - - public DownloadResourceViewModel(IDownloadService downloadService) - { - this.downloadService = downloadService; - } + public string? ShortHash => Resource.HashSha256?.ToLowerInvariant()[..7]; /// public override async Task OnLoadedAsync() @@ -65,6 +65,33 @@ public partial class DownloadResourceViewModel : ContentDialogViewModelBase } } + public TrackedDownload StartDownload() + { + var sharedFolderType = + Resource.ContextType as SharedFolderType? + ?? throw new InvalidOperationException("ContextType is not SharedFolderType"); + + var modelsDir = new DirectoryPath(settingsManager.ModelsDirectory).JoinDir( + sharedFolderType.GetStringValue() + ); + + var download = trackedDownloadService.NewDownload( + Resource.Url, + modelsDir.JoinFile(Resource.FileName) + ); + + // Set extraction properties + download.AutoExtractArchive = Resource.AutoExtractArchive; + download.ExtractRelativePath = Resource.ExtractRelativePath; + + download.ContextAction = new ModelPostDownloadContextAction(); + download.Start(); + + EventManager.Instance.OnToggleProgressFlyout(); + + return download; + } + public BetterContentDialog GetDialog() { return new BetterContentDialog diff --git a/StabilityMatrix.Avalonia/Views/Dialogs/DownloadResourceDialog.axaml b/StabilityMatrix.Avalonia/Views/Dialogs/DownloadResourceDialog.axaml index 910ea333..4dfa9983 100644 --- a/StabilityMatrix.Avalonia/Views/Dialogs/DownloadResourceDialog.axaml +++ b/StabilityMatrix.Avalonia/Views/Dialogs/DownloadResourceDialog.axaml @@ -20,7 +20,7 @@ diff --git a/StabilityMatrix.Core/Models/RemoteResource.cs b/StabilityMatrix.Core/Models/RemoteResource.cs index eaaf5b3f..6a32ac45 100644 --- a/StabilityMatrix.Core/Models/RemoteResource.cs +++ b/StabilityMatrix.Core/Models/RemoteResource.cs @@ -28,5 +28,13 @@ public readonly record struct RemoteResource public string? Author { get; init; } - public string? ByAuthor => string.IsNullOrEmpty(Author) ? null : $"by {Author}"; + /// + /// Whether to auto-extract the archive after download + /// + public bool AutoExtractArchive { get; init; } + + /// + /// Optional relative path to extract the archive to, if AutoExtractArchive is true + /// + public string? ExtractRelativePath { get; init; } }