LANCommander/LANCommander.Server/UI/Pages/Servers/Components/ImportUploadDialog.razor

106 lines
2.9 KiB
Text
Raw Permalink Normal View History

@using LANCommander.SDK.Enums
2025-01-16 03:08:43 -06:00
@inherits FeedbackComponent<ImportDialogOptions>
@inject StorageLocationService StorageLocationService
@inject ServerService ServerService
2025-02-16 10:08:05 -06:00
@inject ImportService<Server> ImportService
2024-07-04 16:34:30 -05:00
@inject IMessageService MessageService
2024-07-07 16:33:46 -05:00
@inject ILogger<ImportUploadDialog> Logger
2024-07-04 16:34:30 -05:00
2025-01-16 03:22:46 -06:00
<ChunkUploader
@ref="ChunkUploader"
Accept=".lcx"
@bind-File="File"
@bind-Status="Status"
StorageLocationId="StorageLocation.Id"
OnUploadCompleted="OnUploadCompleted"
OnUploadError="OnUploadError">
<Text>
<p>Drag and Drop</p>
<p>or</p>
<p>
<Button Type="@ButtonType.Primary" Style="margin-top: 8px;">Browse</Button>
</p>
</Text>
<Hint>@Options.Hint</Hint>
</ChunkUploader>
2025-01-16 03:08:43 -06:00
2025-01-16 03:22:46 -06:00
<Flex Justify="FlexJustify.End" Gap="FlexGap.Small" Style="margin-top: 16px;">
2025-01-16 03:08:43 -06:00
<Button OnClick="Start" Disabled="@(File == null || Status != "")" Type="@ButtonType.Primary">Upload</Button>
<Button OnClick="Cancel">Cancel</Button>
</Flex>
2024-07-04 16:34:30 -05:00
@code {
IBrowserFile File;
ChunkUploader ChunkUploader;
2025-01-16 03:08:43 -06:00
StorageLocation StorageLocation = new();
2024-07-04 16:34:30 -05:00
2025-01-16 03:08:43 -06:00
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
2024-07-04 16:34:30 -05:00
2025-01-16 03:08:43 -06:00
bool IsValid = false;
2024-07-04 16:34:30 -05:00
string Filename;
string Status;
2025-01-16 03:22:46 -06:00
protected override async Task OnInitializedAsync()
{
StorageLocation = await StorageLocationService.FirstAsync(l => l.Default && l.Type == StorageLocationType.Archive);
2025-01-16 03:22:46 -06:00
}
async Task Start()
2024-07-04 16:34:30 -05:00
{
await ChunkUploader.Start();
}
2024-11-22 02:05:44 -06:00
async Task Clear()
2024-07-04 16:34:30 -05:00
{
await ChunkUploader.Clear();
}
2024-11-22 02:05:44 -06:00
async Task Cancel()
2024-07-04 16:34:30 -05:00
{
await ChunkUploader.Clear();
2025-01-16 03:08:43 -06:00
await CloseFeedbackAsync();
2024-07-04 16:34:30 -05:00
}
public async Task OnUploadCompleted(string data)
{
if (Guid.TryParse(data, out var objectKey))
{
try
{
2025-02-16 10:08:05 -06:00
var server = await ImportService.ImportFromUploadArchiveAsync(objectKey);
2024-07-04 16:34:30 -05:00
await InvokeAsync(StateHasChanged);
2024-07-04 16:34:30 -05:00
MessageService.Success($"{server.Name} successfully imported!");
2025-01-16 03:08:43 -06:00
await CloseFeedbackAsync();
2024-07-04 16:34:30 -05:00
}
catch (Exception ex)
{
MessageService.Error("An unknown error occurred while trying to import");
2024-07-07 16:33:46 -05:00
Logger.LogError(ex, "An unknown error occurred while trying to import");
2024-07-04 16:34:30 -05:00
}
}
else
{
await InvokeAsync(StateHasChanged);
MessageService.Error("Import file failed to upload!");
2024-07-07 16:33:46 -05:00
Logger.LogError("Import file failed to upload!");
2025-01-16 03:08:43 -06:00
await CloseFeedbackAsync();
2024-07-04 16:34:30 -05:00
}
2025-01-16 03:08:43 -06:00
await Clear();
2024-07-04 16:34:30 -05:00
}
public async Task OnUploadError(string message)
{
await InvokeAsync(StateHasChanged);
MessageService.Error("An error occurred while trying to import");
2024-07-07 16:33:46 -05:00
Logger.LogError($"An error occurred while trying to import: {message}");
2025-01-16 03:08:43 -06:00
await CloseFeedbackAsync();
2024-07-04 16:34:30 -05:00
}
}