LANCommander/LANCommander/Components/ArchiveUploader.razor
2023-04-07 19:11:09 -05:00

186 lines
5.2 KiB
Text

@using System.Net;
@using System.Diagnostics;
@inject HttpClient HttpClient
@inject NavigationManager Navigator
@inject ArchiveService ArchiveService
@inject IMessageService MessageService
@inject IJSRuntime JS
<Space Direction="DirectionVHType.Vertical" Style="width: 100%">
<SpaceItem>
<Table TItem="Archive" DataSource="@Game.Archives.OrderByDescending(a => a.CreatedOn)" HidePagination="true">
<PropertyColumn Property="a => a.Version" />
<PropertyColumn Property="a => a.CompressedSize">
@ByteSizeLib.ByteSize.FromBytes(context.CompressedSize)
</PropertyColumn>
<PropertyColumn Property="a => a.CreatedBy">
@context.CreatedBy?.UserName
</PropertyColumn>
<PropertyColumn Property="a => a.CreatedOn" Format="MM/dd/yyyy hh:mm tt" />
<ActionColumn Title="">
<Space Style="display: flex; justify-content: end">
<SpaceItem>
<Popconfirm Title="Are you sure you want to delete this archive?" OnConfirm="() => Delete(context)">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger />
</Popconfirm>
</SpaceItem>
</Space>
</ActionColumn>
</Table>
</SpaceItem>
<SpaceItem>
<GridRow Justify="end">
<GridCol>
<Button OnClick="AddArchive" Type="@ButtonType.Primary">Upload Archive</Button>
</GridCol>
</GridRow>
</SpaceItem>
</Space>
@{
RenderFragment Footer =
@<Template>
<Button OnClick="UploadArchiveJS" Disabled="@(File == null || Uploading)" Type="@ButtonType.Primary">Upload</Button>
<Button OnClick="Clear" Disabled="File == null || Uploading" Danger>Clear</Button>
<Button OnClick="Cancel">Cancel</Button>
</Template>;
}
<Modal Visible="@ModalVisible" Title="Upload Archive" OnOk="UploadArchive" OnCancel="Cancel" Footer="@Footer">
<Form Model="@Archive" Layout="@FormLayout.Vertical">
<FormItem Label="Version">
<Input @bind-Value="@context.Version" />
</FormItem>
<FormItem Label="Changelog">
<TextArea @bind-Value="@context.Changelog" MaxLength=500 ShowCount />
</FormItem>
<FormItem>
<InputFile id="FileInput" OnChange="FileSelected" hidden />
<Upload Name="files" FileList="FileList">
<label class="ant-btn" for="FileInput">
<Icon Type="upload" />
Select Archive
</label>
</Upload>
</FormItem>
<FormItem>
<Progress Percent="Progress" />
<Text>@ByteSizeLib.ByteSize.FromBytes(Speed)/s</Text>
</FormItem>
</Form>
</Modal>
@code {
[Parameter] public Game Game { get; set; }
Archive Archive;
IBrowserFile File { get; set; }
List<UploadFileItem> FileList = new List<UploadFileItem>();
bool IsValid = false;
bool ModalVisible = false;
private static string DefaultDragClass = "relative rounded-lg border-2 border-dashed pa-4 mt-4 mud-width-full mud-height-full z-10";
private string DragClass = DefaultDragClass;
const int ChunkSize = 1024 * 1024 * 10;
int Progress = 0;
bool Uploading = false;
double Speed = 0;
Stopwatch Watch;
long WatchBytesTransferred = 0;
protected override async Task OnInitializedAsync()
{
if (Game.Archives == null)
Game.Archives = new List<Archive>();
HttpClient.BaseAddress = new Uri(Navigator.BaseUri);
Archive = new Archive()
{
GameId = Game.Id,
Id = Guid.NewGuid()
};
}
private void AddArchive()
{
Archive = new Archive()
{
GameId = Game.Id,
Id = Guid.NewGuid()
};
ModalVisible = true;
}
private async Task Delete(Archive archive)
{
try
{
await ArchiveService.Delete(archive);
await MessageService.Success("Archive deleted!");
}
catch
{
await MessageService.Error("Archive could not be deleted.");
}
}
private void Clear()
{
File = null;
}
private void Cancel()
{
File = null;
ModalVisible = false;
}
private async void FileSelected(InputFileChangeEventArgs args)
{
File = args.File;
}
private async Task UploadArchiveJS()
{
Uploading = true;
var response = (await JS.InvokeAsync<string>("Uploader.Upload", "FileInput"));
if (Guid.TryParse(response, out var objectKey))
{
Uploading = false;
await UploadComplete(objectKey);
}
else
{
await MessageService.Error("Archive failed to upload!");
}
await InvokeAsync(StateHasChanged);
}
private async Task UploadComplete(Guid objectKey)
{
Archive.ObjectKey = objectKey.ToString();
Archive.CompressedSize = File.Size;
await ArchiveService.Add(Archive);
ModalVisible = false;
await MessageService.Success("Archive uploaded!");
}
}