Workshop - Added plugin upload support

This commit is contained in:
RobertBeekman committed 2024-02-14 23:04:19 +01:00
1 parent 38a1318170
commit d9df443970
12 files changed
+240 -15

No files matched your search

@@ -15,6 +15,7 @@ public class EntryUploadHandlerFactory
{
return entryType switch
{
EntryType.Plugin => _container.Resolve<PluginEntryUploadHandler>(),
EntryType.Profile => _container.Resolve<ProfileEntryUploadHandler>(),
EntryType.Layout => _container.Resolve<LayoutEntryUploadHandler>(),
_ => throw new NotSupportedException($"EntryType '{entryType}' is not supported.")
@@ -0,0 +1,15 @@
using Artemis.Core;
namespace Artemis.WebClient.Workshop.Handlers.UploadHandlers;
public class PluginEntrySource : IEntrySource
{
public PluginEntrySource(PluginInfo pluginInfo, string path)
{
PluginInfo = pluginInfo;
Path = path;
}
public PluginInfo PluginInfo { get; set; }
public string Path { get; set; }
}
@@ -0,0 +1,40 @@
using System.Net.Http.Headers;
using Artemis.WebClient.Workshop.Entities;
using Newtonsoft.Json;
namespace Artemis.WebClient.Workshop.Handlers.UploadHandlers;
public class PluginEntryUploadHandler : IEntryUploadHandler
{
private readonly IHttpClientFactory _httpClientFactory;
public PluginEntryUploadHandler(IHttpClientFactory httpClientFactory)
{
_httpClientFactory = httpClientFactory;
}
/// <inheritdoc />
public async Task<EntryUploadResult> CreateReleaseAsync(long entryId, IEntrySource entrySource, CancellationToken cancellationToken)
{
if (entrySource is not PluginEntrySource source)
throw new InvalidOperationException("Can only create releases for plugins");
// Submit the archive
HttpClient client = _httpClientFactory.CreateClient(WorkshopConstants.WORKSHOP_CLIENT_NAME);
// Construct the request
await using FileStream fileStream = File.Open(source.Path, FileMode.Open);
MultipartFormDataContent content = new();
StreamContent streamContent = new(fileStream);
streamContent.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
content.Add(streamContent, "file", "file.zip");
// Submit
HttpResponseMessage response = await client.PostAsync("releases/upload/" + entryId, content, cancellationToken);
if (!response.IsSuccessStatusCode)
return EntryUploadResult.FromFailure($"{response.StatusCode} - {await response.Content.ReadAsStringAsync(cancellationToken)}");
Release? release = JsonConvert.DeserializeObject<Release>(await response.Content.ReadAsStringAsync(cancellationToken));
return release != null ? EntryUploadResult.FromSuccess(release) : EntryUploadResult.FromFailure("Failed to deserialize response");
}
}