LANCommander/LANCommander.Server/UI/Pages/FirstTimeSetup/Paths.razor

230 lines
8.1 KiB
Text

@page "/FirstTimeSetup/Paths"
@using LANCommander.SDK
@using LANCommander.SDK.Enums
@using LANCommander.Server.Settings
@using Microsoft.Extensions.Options
@layout FirstTimeSetupLayout
@inject SetupService SetupService
@inject StorageLocationService StorageLocationService
@inject SettingsProvider<Settings> SettingsProvider
@inject IOptions<Settings> Settings
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<Index> Logger
<PageTitle>Configure Paths - First Time Setup</PageTitle>
<Form Model="@this" Loading="Loading" OnFinish="ValidatePaths" Layout="@FormLayout.Vertical">
<FormItem>
Configure where archives, saves, and media get stored.
</FormItem>
<Divider Text="Archives" />
<FormItem>
<Flex Vertical Gap="FlexGap.Middle">
<Table TItem="StorageLocation" DataSource="@ArchiveStorageLocations" HidePagination="true" Responsive Context="storageLocation" Size="TableSize.Small">
<PropertyColumn Property="l => l.Path">
<FilePicker @bind-Value="storageLocation.Path" AllowDirectories="true" Title="Select Storage Location" Root="@RootPath" OnSelected="(path) => OnPathSelected(storageLocation, path)"/>
</PropertyColumn>
<PropertyColumn Property="l => l.Default" Title="Default" Align="ColumnAlign.Right">
<Radio TValue="bool" Checked="storageLocation.Default" CheckedChanged="() => SetDefault(storageLocation)"/>
</PropertyColumn>
<ActionColumn>
<Flex Justify="FlexJustify.End">
<Popconfirm OnConfirm="() => RemoveArchiveStorageLocation(storageLocation)" Title="Are you sure you want to remove this storage location?">
<Button Icon="@IconType.Outline.Close" Type="ButtonType.Text" Danger/>
</Popconfirm>
</Flex>
</ActionColumn>
</Table>
<Flex Justify="FlexJustify.End">
<Button OnClick="AddArchiveStorageLocation" Type="@ButtonType.Primary">Add Path</Button>
</Flex>
</Flex>
</FormItem>
@if (SavesStorageLocation != null)
{
<Divider Text="Saves" />
<FormItem>
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@SavesStorageLocation.Path" OkText="Select Path" Title="Choose Path" OnSelected="(path) => OnPathSelected(SavesStorageLocation, path)" />
</FormItem>
}
@if (MediaStorageLocation != null)
{
<Divider Text="Media" />
<FormItem>
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@MediaStorageLocation.Path" OkText="Select Path" Title="Choose Path" OnSelected="(path) => OnPathSelected(MediaStorageLocation, path)" />
</FormItem>
}
<Divider Text="Backups" />
<FormItem>
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@Settings.Value.Server.Backups.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="(path) => Settings.Value.Server.Backups.StoragePath = OnPathSelected(path)" />
</FormItem>
<Divider Text="Launchers" />
<FormItem>
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@Settings.Value.Server.Launcher.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="(path) => Settings.Value.Server.Launcher.StoragePath = OnPathSelected(path)" />
</FormItem>
<Divider Text="Servers" />
<FormItem>
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@Settings.Value.Server.GameServers.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="(path) => Settings.Value.Server.GameServers.StoragePath = OnPathSelected(path)" />
</FormItem>
<FormItem>
<Flex Justify="FlexJustify.End">
<Button Type="ButtonType.Primary" HtmlType="submit">
Next
</Button>
</Flex>
</FormItem>
</Form>
@code {
[CascadingParameter] FirstTimeSetupLayout Layout { get; set; }
ICollection<StorageLocation> ArchiveStorageLocations = new List<StorageLocation>();
StorageLocation MediaStorageLocation = new()
{
Path = AppPaths.GetConfigPath("Media"),
Type = StorageLocationType.Media,
Default = true,
};
StorageLocation SavesStorageLocation = new()
{
Path = AppPaths.GetConfigPath("Saves"),
Type = StorageLocationType.Save,
Default = true,
};
bool Loading = false;
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
protected override async Task OnInitializedAsync()
{
await Layout.ChangeCurrentStep(FirstTimeSetupStep.Paths);
var isSetupInitialized = await SetupService.IsSetupInitialized();
if (isSetupInitialized)
{
NavigationManager.NavigateTo("/");
return;
}
try
{
ArchiveStorageLocations = await StorageLocationService
.AsNoTracking()
.GetAsync(l => l.Type == StorageLocationType.Archive);
}
catch (Exception ex)
{
Logger?.LogError(ex, "Couldn't retrieve existing archive storage locations");
}
finally
{
if (!ArchiveStorageLocations.Any())
ArchiveStorageLocations.Add(new StorageLocation
{
Path = Path.Join(AppPaths.GetConfigDirectory(), "Uploads"),
Type = StorageLocationType.Archive,
Default = true,
});
}
try
{
var existingSaves = await StorageLocationService
.AsNoTracking()
.FirstOrDefaultAsync(l => l.Type == StorageLocationType.Save && l.Default);
if (existingSaves != null)
SavesStorageLocation = existingSaves;
}
catch (Exception ex)
{
Logger?.LogError(ex, "Couldn't retrieve existing store locations");
}
try
{
var existingMedia = await StorageLocationService
.AsNoTracking()
.FirstOrDefaultAsync(l => l.Type == SDK.Enums.StorageLocationType.Media && l.Default);
if (existingMedia != null)
MediaStorageLocation = existingMedia;
}
catch (Exception ex)
{
Logger?.LogError(ex, "Couldn't retrieve existing store locations");
}
}
async Task ValidatePaths()
{
try
{
var locations = new List<StorageLocation>(ArchiveStorageLocations);
locations.Add(SavesStorageLocation);
locations.Add(MediaStorageLocation);
await SetupService.UpdatePaths(locations);
NavigationManager.NavigateTo("/FirstTimeSetup/Metadata");
}
catch (Exception ex)
{
Logger?.LogError(ex, $"Error validating path");
MessageService.Error($"Error validating path: {ex.Message}", 10);
}
}
void SetDefault(StorageLocation storageLocation)
{
foreach (var archiveStorageLocation in ArchiveStorageLocations)
{
archiveStorageLocation.Default = archiveStorageLocation.Id == storageLocation.Id;
}
}
string OnPathSelected(string path)
{
var appPath = Directory.GetCurrentDirectory();
if (path != null && path.StartsWith(appPath))
path = path.Substring(appPath.Length).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
return path;
}
void OnPathSelected(StorageLocation storageLocation, string path)
{
storageLocation.Path = OnPathSelected(path);
}
void AddArchiveStorageLocation()
{
ArchiveStorageLocations.Add(new StorageLocation());
StateHasChanged();
}
void RemoveArchiveStorageLocation(StorageLocation storageLocation)
{
ArchiveStorageLocations.Remove(storageLocation);
}
}