LANCommander/LANCommander.Server/UI/Components/ArchiveEditor.razor
2025-03-17 02:02:17 -05:00

131 lines
4.1 KiB
Text

@using System.Net;
@using System.Diagnostics;
@using Hangfire;
@using LANCommander.Server.Jobs.Background;
@using Microsoft.EntityFrameworkCore;
@inject ArchiveService archiveService
@inject HttpClient HttpClient
@inject NavigationManager Navigator
@inject ModalService ModalService
@inject IMessageService MessageService
@inject IJSRuntime JS
@inject ILogger<ArchiveEditor> Logger
<DataTable
@ref="Table"
TItem="Archive"
HidePagination
Responsive
Query="a => (GameId != Guid.Empty && a.GameId == GameId) || (RedistributableId != Guid.Empty && a.RedistributableId == RedistributableId)">
<RightToolbar>
<Button OnClick="UploadArchive" Type="@ButtonType.Primary">Upload Archive</Button>
</RightToolbar>
<Columns>
<BoundDataColumn Property="a => a.Version">
<Input Type="InputType.Text" Bordered="false" @bind-Value="context.Version" OnBlur="() => Update(context)"/>
</BoundDataColumn>
<BoundDataColumn Property="a => a.CompressedSize">
<ByteSize Value="context.CompressedSize" />
</BoundDataColumn>
<BoundDataColumn Property="a => a.UncompressedSize">
<ByteSize Value="context.UncompressedSize" />
</BoundDataColumn>
<BoundDataColumn Property="a => a.CreatedBy != null ? a.CreatedBy.UserName : String.Empty" Include="CreatedBy"/>
<BoundDataColumn Property="s => s.CreatedOn" DefaultSortOrder="SortDirection.Descending">
<LocalTime Value="context.CreatedOn" />
</BoundDataColumn>
<DataActions>
<a href="/Download/Archive/@context.Id" target="_blank" class="ant-btn ant-btn-text ant-btn-icon-only">
<Icon Type="@IconType.Outline.Download"/>
</a>
<Button Icon="@IconType.Outline.FolderOpen" Type="@ButtonType.Text" OnClick="() => BrowseArchive(context)"/>
<Popconfirm Title="Are you sure you want to delete this archive?" OnConfirm="() => Delete(context)">
<Button Icon="@IconType.Outline.Close" Type="@ButtonType.Text" Danger/>
</Popconfirm>
</DataActions>
</Columns>
</DataTable>
<ArchiveUploader @ref="Uploader" GameId="GameId" RedistributableId="RedistributableId" OnArchiveUploaded="ArchiveUploaded" />
@code {
[Parameter] public Guid GameId { get; set; }
[Parameter] public Guid RedistributableId { get; set; }
DataTable<Archive> Table;
ArchiveUploader Uploader;
protected override void OnInitialized()
{
HttpClient.BaseAddress = new Uri(Navigator.BaseUri);
}
private async Task Download(Archive archive)
{
string url = $"/Download/Game/{archive.Id}";
await JS.InvokeAsync<object>("open", url, "_blank");
}
private async Task ArchiveUploaded(Guid archiveId)
{
Table.Reload();
}
private async Task UploadArchive()
{
await Uploader.Open();
}
private async Task BrowseArchive(Archive archive)
{
var modalOptions = new ModalOptions()
{
Title = "Browse Archive",
Maximizable = false,
DefaultMaximized = true,
Closable = true,
WrapClassName = "file-picker-dialog",
};
var modalRef = await ModalService.CreateModalAsync<ArchiveBrowserDialog, Guid, Guid>(modalOptions, archive.Id);
}
private async Task Update(Archive archive)
{
try
{
await archiveService.UpdateAsync(archive);
Table.Reload();
MessageService.Success("Archive updated!");
}
catch (Exception ex)
{
MessageService.Error("Archive could not be updated.");
Logger.LogError(ex, "Archive could not be updated.");
}
}
private async Task Delete(Archive archive)
{
try
{
await archiveService.DeleteAsync(archive);
Table.Reload();
MessageService.Success("Archive deleted!");
}
catch (Exception ex)
{
MessageService.Error("Archive could not be deleted.");
Logger.LogError(ex, "Archive could not be deleted.");
}
}
}