Profiles - Added workshop installing

This commit is contained in:
Robert committed 2023-08-26 20:47:48 +02:00
1 parent 4143cc2de8
commit 671c587df6
21 files changed
+401 -73

No files matched your search

@@ -0,0 +1,28 @@
using Artemis.Web.Workshop.Entities;
namespace Artemis.WebClient.Workshop.DownloadHandlers;
public class EntryInstallResult<T>
{
public bool IsSuccess { get; set; }
public string? Message { get; set; }
public T? Result { get; set; }
public static EntryInstallResult<T> FromFailure(string? message)
{
return new EntryInstallResult<T>
{
IsSuccess = false,
Message = message
};
}
public static EntryInstallResult<T> FromSuccess(T installationResult)
{
return new EntryInstallResult<T>
{
IsSuccess = true,
Result = installationResult
};
}
}
@@ -0,0 +1,7 @@
using Artemis.UI.Shared.Utilities;
namespace Artemis.WebClient.Workshop.DownloadHandlers;
public interface IEntryDownloadHandler
{
}
@@ -0,0 +1,51 @@
using System.IO.Compression;
using Artemis.Core;
using Artemis.Core.Services;
using Artemis.UI.Shared.Extensions;
using Artemis.UI.Shared.Utilities;
using Newtonsoft.Json;
namespace Artemis.WebClient.Workshop.DownloadHandlers.Implementations;
public class ProfileEntryDownloadHandler : IEntryDownloadHandler
{
private readonly IHttpClientFactory _httpClientFactory;
private readonly IProfileService _profileService;
public ProfileEntryDownloadHandler(IHttpClientFactory httpClientFactory, IProfileService profileService)
{
_httpClientFactory = httpClientFactory;
_profileService = profileService;
}
public async Task<EntryInstallResult<ProfileConfiguration>> InstallProfileAsync(Guid releaseId, Progress<StreamProgress> progress, CancellationToken cancellationToken)
{
try
{
HttpClient client = _httpClientFactory.CreateClient(WorkshopConstants.WORKSHOP_CLIENT_NAME);
using MemoryStream stream = new();
await client.DownloadDataAsync($"releases/download/{releaseId}", stream, progress, cancellationToken);
using ZipArchive zipArchive = new(stream, ZipArchiveMode.Read);
List<ZipArchiveEntry> profiles = zipArchive.Entries.Where(e => e.Name.EndsWith("json", StringComparison.InvariantCultureIgnoreCase)).ToList();
ZipArchiveEntry userProfileEntry = profiles.First();
ProfileConfigurationExportModel profile = await GetProfile(userProfileEntry);
ProfileCategory category = _profileService.ProfileCategories.FirstOrDefault(c => c.Name == "Workshop") ?? _profileService.CreateProfileCategory("Workshop", true);
ProfileConfiguration profileConfiguration = _profileService.ImportProfile(category, profile, true, true, null);
return EntryInstallResult<ProfileConfiguration>.FromSuccess(profileConfiguration);
}
catch (Exception e)
{
return EntryInstallResult<ProfileConfiguration>.FromFailure(e.Message);
}
}
private async Task<ProfileConfigurationExportModel> GetProfile(ZipArchiveEntry userProfileEntry)
{
await using Stream stream = userProfileEntry.Open();
using StreamReader reader = new(stream);
return JsonConvert.DeserializeObject<ProfileConfigurationExportModel>(await reader.ReadToEndAsync(), IProfileService.ExportSettings)!;
}
}
@@ -1,4 +1,5 @@
using System.Reflection;
using Artemis.WebClient.Workshop.DownloadHandlers;
using Artemis.WebClient.Workshop.Extensions;
using Artemis.WebClient.Workshop.Repositories;
using Artemis.WebClient.Workshop.Services;
@@ -48,5 +49,6 @@ public static class ContainerExtensions
container.Register<EntryUploadHandlerFactory>(Reuse.Transient);
container.RegisterMany(workshopAssembly, type => type.IsAssignableTo<IEntryUploadHandler>(), Reuse.Transient);
container.RegisterMany(workshopAssembly, type => type.IsAssignableTo<IEntryDownloadHandler>(), Reuse.Transient);
}
}
@@ -1,4 +1,6 @@
using System.Net;
using System.Net.Http.Headers;
using Artemis.UI.Shared.Routing;
using Artemis.UI.Shared.Services.MainWindow;
using Artemis.UI.Shared.Utilities;
using Artemis.WebClient.Workshop.UploadHandlers;
@@ -11,11 +13,13 @@ public class WorkshopService : IWorkshopService
{
private readonly Dictionary<Guid, Stream> _entryIconCache = new();
private readonly IHttpClientFactory _httpClientFactory;
private readonly IRouter _router;
private readonly SemaphoreSlim _iconCacheLock = new(1);
public WorkshopService(IHttpClientFactory httpClientFactory, IMainWindowService mainWindowService)
public WorkshopService(IHttpClientFactory httpClientFactory, IMainWindowService mainWindowService, IRouter router)
{
_httpClientFactory = httpClientFactory;
_router = router;
mainWindowService.MainWindowClosed += (_, _) => Dispatcher.UIThread.InvokeAsync(async () =>
{
await Task.Delay(1000);
@@ -43,6 +47,31 @@ public class WorkshopService : IWorkshopService
return ImageUploadResult.FromSuccess();
}
/// <inheritdoc />
public async Task<IWorkshopService.WorkshopStatus> GetWorkshopStatus()
{
try
{
// Don't use the workshop client which adds auth headers
HttpClient client = _httpClientFactory.CreateClient();
HttpResponseMessage response = await client.SendAsync(new HttpRequestMessage(HttpMethod.Head, WorkshopConstants.WORKSHOP_URL + "/status"));
return new IWorkshopService.WorkshopStatus(response.IsSuccessStatusCode, response.StatusCode.ToString());
}
catch (HttpRequestException e)
{
return new IWorkshopService.WorkshopStatus(false, e.Message);
}
}
/// <inheritdoc />
public async Task<bool> ValidateWorkshopStatus()
{
IWorkshopService.WorkshopStatus status = await GetWorkshopStatus();
if (!status.IsReachable)
await _router.Navigate($"workshop/offline/{status.Message}");
return status.IsReachable;
}
private void ClearCache()
{
try
@@ -63,4 +92,8 @@ public class WorkshopService : IWorkshopService
public interface IWorkshopService
{
Task<ImageUploadResult> SetEntryIcon(Guid entryId, Progress<StreamProgress> progress, Stream icon, CancellationToken cancellationToken);
Task<WorkshopStatus> GetWorkshopStatus();
Task<bool> ValidateWorkshopStatus();
public record WorkshopStatus(bool IsReachable, string Message);
}