73 lines
2.8 KiB
C#
73 lines
2.8 KiB
C#
using LANCommander.SDK.Models;
|
|
using System;
|
|
using System.IO;
|
|
using System.Threading.Tasks;
|
|
using LANCommander.SDK.Abstractions;
|
|
using LANCommander.SDK.Factories;
|
|
|
|
namespace LANCommander.SDK.Services
|
|
{
|
|
public class ServerClient(
|
|
ISettingsProvider settingsProvider,
|
|
ApiRequestFactory apiRequestFactory)
|
|
{
|
|
public delegate void OnArchiveEntryExtractionProgressHandler(object sender, ArchiveEntryExtractionProgressArgs e);
|
|
public event OnArchiveEntryExtractionProgressHandler OnArchiveEntryExtractionProgress;
|
|
|
|
public delegate void OnArchiveExtractionProgressHandler(long position, long length);
|
|
public event OnArchiveExtractionProgressHandler OnArchiveExtractionProgress;
|
|
|
|
public async Task ImportAsync(string archivePath)
|
|
{
|
|
using (var fs = new FileStream(archivePath, FileMode.Open, FileAccess.Read))
|
|
{
|
|
var objectKey = await apiRequestFactory
|
|
.Create()
|
|
.UseAuthenticationToken()
|
|
.UseVersioning()
|
|
.UploadInChunksAsync(settingsProvider.CurrentValue.Archives.UploadChunkSize, fs);
|
|
|
|
if (objectKey != Guid.Empty)
|
|
await apiRequestFactory
|
|
.Create()
|
|
.UseAuthenticationToken()
|
|
.UseVersioning()
|
|
.UseRoute($"/api/Servers/Import/{objectKey}")
|
|
.PostAsync();
|
|
}
|
|
}
|
|
|
|
[Obsolete]
|
|
public async Task ExportAsync(string destinationPath, Guid serverId)
|
|
{
|
|
throw new NotImplementedException();
|
|
}
|
|
|
|
public async Task UploadArchiveAsync(string archivePath, Guid serverId, string version, string changelog = "")
|
|
{
|
|
using (var fs = new FileStream(archivePath, FileMode.Open, FileAccess.Read))
|
|
{
|
|
var objectKey = await apiRequestFactory
|
|
.Create()
|
|
.UseAuthenticationToken()
|
|
.UseVersioning()
|
|
.UploadInChunksAsync(settingsProvider.CurrentValue.Archives.UploadChunkSize, fs);
|
|
|
|
if (objectKey != Guid.Empty)
|
|
await apiRequestFactory
|
|
.Create()
|
|
.UseAuthenticationToken()
|
|
.UseVersioning()
|
|
.UseRoute($"/api/Servers/UploadArchive")
|
|
.AddBody(new UploadArchiveRequest
|
|
{
|
|
Id = serverId,
|
|
ObjectKey = objectKey,
|
|
Version = version,
|
|
Changelog = changelog,
|
|
})
|
|
.PostAsync();
|
|
}
|
|
}
|
|
}
|
|
}
|