141 lines
No EOL
4.1 KiB
Text
141 lines
No EOL
4.1 KiB
Text
@using System.Net;
|
|
@using System.Diagnostics;
|
|
@using Hangfire;
|
|
@using LANCommander.Server.Jobs.Background;
|
|
@using Microsoft.EntityFrameworkCore;
|
|
@inherits FeedbackComponent<ImportDialogOptions>
|
|
@inject DatabaseServiceFactory DatabaseServiceFactory
|
|
@inject NavigationManager NavigationManager
|
|
@inject ModalService ModalService
|
|
@inject IMessageService MessageService
|
|
@inject IJSRuntime JS
|
|
@inject ILogger<ImportUploadDialog> Logger
|
|
|
|
<Flex Vertical Gap="FlexGap.Small">
|
|
<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>
|
|
|
|
<StorageLocationSelector @bind-Value="StorageLocation" Type="StorageLocationType.Archive"/>
|
|
</Flex>
|
|
|
|
<Flex Justify="FlexJustify.End" Gap="FlexGap.Small" Style="margin-top: 16px;">
|
|
<FilePickerButton
|
|
EntrySelectable="@(entry => !String.IsNullOrWhiteSpace(entry.Name) && entry.Name.ToLower().EndsWith(".lcx"))"
|
|
OnSelected="OnLocalFileSelected"
|
|
Root="@RootPath"
|
|
Disabled="@(Status != "")">
|
|
Use Local File
|
|
</FilePickerButton>
|
|
<Button OnClick="Start" Disabled="@(File == null || Status != "")" Type="@ButtonType.Primary">Upload</Button>
|
|
<Button OnClick="Cancel">Cancel</Button>
|
|
</Flex>
|
|
|
|
@code {
|
|
IBrowserFile File;
|
|
ChunkUploader ChunkUploader;
|
|
StorageLocation StorageLocation = new();
|
|
|
|
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
|
|
|
|
bool IsValid = false;
|
|
string Filename;
|
|
string Status;
|
|
|
|
async Task Start()
|
|
{
|
|
await ChunkUploader.Start();
|
|
}
|
|
|
|
async Task Clear()
|
|
{
|
|
await ChunkUploader.Clear();
|
|
}
|
|
|
|
async Task Cancel()
|
|
{
|
|
await ChunkUploader.Clear();
|
|
await CloseFeedbackAsync();
|
|
}
|
|
|
|
public async Task OnUploadCompleted(string data)
|
|
{
|
|
if (Guid.TryParse(data, out var objectKey))
|
|
{
|
|
try
|
|
{
|
|
using (var gameService = DatabaseServiceFactory.Create<GameService>())
|
|
{
|
|
var game = await gameService.ImportAsync(objectKey);
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
MessageService.Success($"{game.Title} successfully imported!");
|
|
|
|
await CloseFeedbackAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "An unknown error occurred while trying to import");
|
|
MessageService.Error("An unknown error occurred while trying to import");
|
|
}
|
|
}
|
|
else
|
|
{
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
MessageService.Error("Import file failed to upload!");
|
|
|
|
await CloseFeedbackAsync();
|
|
}
|
|
|
|
await Clear();
|
|
}
|
|
|
|
public async Task OnUploadError(string message)
|
|
{
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
MessageService.Error("An error occurred while trying to import");
|
|
Logger?.LogError($"An error occurred while trying to import: {message}");
|
|
|
|
await CloseFeedbackAsync();
|
|
}
|
|
|
|
public async Task OnLocalFileSelected(string path)
|
|
{
|
|
try
|
|
{
|
|
using (var gameService = DatabaseServiceFactory.Create<GameService>())
|
|
{
|
|
var game = await gameService.ImportLocalFileAsync(path);
|
|
|
|
await InvokeAsync(StateHasChanged);
|
|
|
|
MessageService.Success($"{game.Title} successfully imported!");
|
|
|
|
await CloseFeedbackAsync();
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "An unknown error occurred while trying to import");
|
|
MessageService.Error("An unknown error occurred while trying to import");
|
|
}
|
|
}
|
|
} |