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

238 lines
8.3 KiB
Text
Raw Normal View History

2024-10-12 18:36:00 -05:00
@page "/FirstTimeSetup/Paths"
@using LANCommander.SDK.Enums
2024-10-12 18:36:00 -05:00
@layout FirstTimeSetupLayout
@inject SetupService SetupService
@inject StorageLocationService StorageLocationService
2024-10-12 18:36:00 -05:00
@inject NavigationManager NavigationManager
@inject IMessageService MessageService
@inject ILogger<Index> Logger
<PageTitle>Configure Paths - First Time Setup</PageTitle>
2024-10-12 18:36:00 -05:00
<Form Model="@this" Loading="Loading" OnFinish="ValidatePaths" Layout="@FormLayout.Vertical">
<FormItem>
Configure where archives, saves, and media get stored.
</FormItem>
<Divider Text="Archives" />
<FormItem>
2025-01-22 20:54:41 -06:00
<Flex Vertical Gap="FlexGap.Middle">
<Table TItem="StorageLocation" DataSource="@ArchiveStorageLocations" HidePagination="true" Responsive Context="storageLocation" Size="TableSize.Small">
2025-01-22 20:54:41 -06:00
<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>
2025-01-22 20:54:41 -06:00
<Flex Justify="FlexJustify.End">
<Button OnClick="AddArchiveStorageLocation" Type="@ButtonType.Primary">Add Path</Button>
</Flex>
</Flex>
2024-10-12 18:36:00 -05:00
</FormItem>
@if (SavesStorageLocation != null)
{
<Divider Text="Saves" />
2024-10-12 18:36:00 -05:00
<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>
}
2024-10-12 18:36:00 -05:00
@if (MediaStorageLocation != null)
{
<Divider Text="Media" />
2024-10-12 18:36:00 -05:00
<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.Backups.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="(path) => Settings.Backups.StoragePath = OnPathSelected(path)" />
</FormItem>
<Divider Text="Launchers" />
<FormItem>
2025-03-13 21:51:41 +00:00
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@Settings.Launcher.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="(path) => Settings.Launcher.StoragePath = OnPathSelected(path)" />
</FormItem>
<Divider Text="Servers" />
<FormItem>
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@Settings.Servers.StoragePath" OkText="Select Path" Title="Choose Path" OnSelected="(path) => Settings.Servers.StoragePath = OnPathSelected(path)" />
</FormItem>
2024-10-12 18:36:00 -05:00
<FormItem>
2025-01-22 20:54:41 -06:00
<Flex Justify="FlexJustify.End">
<Button Type="ButtonType.Primary" HtmlType="submit">
Next
</Button>
</Flex>
2024-10-12 18:36:00 -05:00
</FormItem>
</Form>
@code {
[CascadingParameter] FirstTimeSetupLayout Layout { get; set; }
WIP fix for MySQL connection concurrency issues This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL: - The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no. - Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory. - A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function. - Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml` - The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution. - The application can now log to Seq when using debug build - Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future. - Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled. - All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation. - Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
ICollection<StorageLocation> ArchiveStorageLocations = new List<StorageLocation>();
StorageLocation MediaStorageLocation = new()
{
2025-03-07 20:44:32 -06:00
Path = Path.Combine(SettingService.WorkingDirectory, "Media"),
Type = StorageLocationType.Media,
Default = true,
};
StorageLocation SavesStorageLocation = new()
{
2025-03-07 20:44:32 -06:00
Path = Path.Combine(SettingService.WorkingDirectory, "Saves"),
Type = StorageLocationType.Save,
Default = true,
};
2024-10-12 18:36:00 -05:00
Settings Settings = SettingService.GetSettings();
2024-10-12 18:36:00 -05:00
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
{
2025-03-07 20:44:32 -06:00
Path = Path.Combine(SettingService.WorkingDirectory, "Uploads"),
Type = StorageLocationType.Archive,
Default = true,
});
}
2024-10-12 18:36:00 -05:00
try
{
SavesStorageLocation = await StorageLocationService
.AsNoTracking()
.FirstOrDefaultAsync(l => l.Type == StorageLocationType.Save && l.Default);
}
catch (Exception ex)
{
Logger?.LogError(ex, "Couldn't retrieve existing store locations");
SavesStorageLocation = new StorageLocation
{
2025-03-07 20:44:32 -06:00
Path = Path.Combine(SettingService.WorkingDirectory, "Saves"),
Type = StorageLocationType.Save,
Default = true,
};
}
try
{
MediaStorageLocation = await StorageLocationService
.AsNoTracking()
.FirstOrDefaultAsync(l => l.Type == SDK.Enums.StorageLocationType.Media && l.Default);
}
catch (Exception ex)
{
Logger?.LogError(ex, "Couldn't retrieve existing store locations");
MediaStorageLocation = new StorageLocation
{
2025-03-07 20:44:32 -06:00
Path = Path.Combine(SettingService.WorkingDirectory, "Media"),
Type = StorageLocationType.Media,
Default = true,
};
WIP fix for MySQL connection concurrency issues This is a large commit. There are a number of things that this commit does to try to fix various issues that were occurring when the database provider was set to MySQL: - The DAL `Repository` has been completely refactored to follow best practices. The repository is now being injected into services instead of the database context itself. This allows the DI to handle the repository's lifetime instead of creating a new repository for every transaction and sharing the context across repositories. As part of these changes, there is no more allowed usage of `IQueryable` and all service/repository methods must actually execute database queries before their return. This is to ensure that the context does not stay open longer than it needs to. Abusing `IQueryable`s by tossing them into Blazor components seems to be a big no-no. - Some deletion behaviors on relationships have been tweaked as MySQL wasn't able to apply migrations with behaviors that were contradictory. - A `ConnectionInterceptor` was added to try to keep track of `DatabaseContext` lifetimes. This is really only for debugging and should be put into `#if DEBUG` regions. This helped identify some potential issues where some contexts were basically never closing, causing the MySQL connector to not function. - Docs for generating migrations have been updated to reflect the addition of being able to specify the database provider and connection string when adding a migration, avoiding the need to edit `Settings.yml` - The application can now be put into a pause state on startup by adding the `--debugger` argument when used from the command line. When a debugger is attached, it resumes execution. - The application can now log to Seq when using debug build - Service lifetime on `DatabaseContext` has switched to transient. This may be reverted in the future. - Lazy loading has been disabled for debugging purposes. It didn't directly help the concurrency issues, but it needs to be tested individually to be re-enabled. - All usage of `UserManager`, `RoleManager`, and `SignInManager` have been removed from all controllers, pages, and Blazor components. Functionality has been moved to `UserService` and `RoleService`. This might have done the most amount of help, but could probably be improved upon in the future by not relying on them and instead having our own implementation. - Application startup migrations and server autostarts have been disabled temporarily. There might be an issue of `DatabaseContext` lifetimes that spawn from this.
2024-10-13 20:42:45 -05:00
}
2024-10-12 18:36:00 -05:00
}
async Task ValidatePaths()
2024-10-12 18:36:00 -05:00
{
try
{
var locations = new List<StorageLocation>(ArchiveStorageLocations);
locations.Add(SavesStorageLocation);
locations.Add(MediaStorageLocation);
await SetupService.UpdatePaths(locations);
SettingService.SaveSettings(Settings);
NavigationManager.NavigateTo("/FirstTimeSetup/Metadata");
2024-10-12 18:36:00 -05:00
}
catch (Exception ex)
{
Logger?.LogError(ex, $"Error validating path");
MessageService.Error($"Error validating path: {ex.Message}", 10);
}
}
2024-11-22 02:05:44 -06:00
void SetDefault(StorageLocation storageLocation)
2024-10-12 18:36:00 -05:00
{
foreach (var archiveStorageLocation in ArchiveStorageLocations)
{
archiveStorageLocation.Default = archiveStorageLocation.Id == storageLocation.Id;
}
}
string OnPathSelected(string path)
2024-10-12 18:36:00 -05:00
{
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);
2024-10-12 18:36:00 -05:00
}
2024-11-22 02:05:44 -06:00
void AddArchiveStorageLocation()
2024-10-12 18:36:00 -05:00
{
ArchiveStorageLocations.Add(new StorageLocation());
StateHasChanged();
}
2024-11-22 02:05:44 -06:00
void RemoveArchiveStorageLocation(StorageLocation storageLocation)
2024-10-12 18:36:00 -05:00
{
ArchiveStorageLocations.Remove(storageLocation);
}
}