196 lines
7 KiB
Text
196 lines
7 KiB
Text
@page "/FirstTimeSetup/Paths"
|
|
@layout FirstTimeSetupLayout
|
|
@using Microsoft.EntityFrameworkCore
|
|
@inject DatabaseServiceFactory DatabaseServiceFactory
|
|
@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="Data.Models.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>
|
|
}
|
|
|
|
<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 StorageLocation();
|
|
StorageLocation SavesStorageLocation = new StorageLocation();
|
|
|
|
bool Loading = false;
|
|
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
await Layout.ChangeCurrentStep(FirstTimeSetupStep.Paths);
|
|
|
|
using (var storageLocationService = DatabaseServiceFactory.Create<StorageLocationService>())
|
|
{
|
|
try
|
|
{
|
|
ArchiveStorageLocations = await storageLocationService.GetAsync(l => l.Type == SDK.Enums.StorageLocationType.Archive);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "Couldn't retrieve existing archive storage locations");
|
|
}
|
|
finally
|
|
{
|
|
if (!ArchiveStorageLocations.Any())
|
|
ArchiveStorageLocations.Add(new StorageLocation
|
|
{
|
|
Path = "Uploads",
|
|
Type = SDK.Enums.StorageLocationType.Archive,
|
|
Default = true,
|
|
});
|
|
}
|
|
|
|
try
|
|
{
|
|
|
|
SavesStorageLocation = await storageLocationService.FirstAsync(l => l.Type == SDK.Enums.StorageLocationType.Save && l.Default);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "Couldn't retrieve existing store locations");
|
|
|
|
SavesStorageLocation = new StorageLocation
|
|
{
|
|
Path = "Saves",
|
|
Type = SDK.Enums.StorageLocationType.Save
|
|
};
|
|
}
|
|
|
|
try
|
|
{
|
|
MediaStorageLocation = await storageLocationService.FirstAsync(l => l.Type == SDK.Enums.StorageLocationType.Media && l.Default);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, "Couldn't retrieve existing store locations");
|
|
|
|
MediaStorageLocation = new StorageLocation
|
|
{
|
|
Path = "Media",
|
|
Type = SDK.Enums.StorageLocationType.Media
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|
|
void ValidatePaths()
|
|
{
|
|
var valid = false;
|
|
|
|
try
|
|
{
|
|
foreach (var storageLocation in ArchiveStorageLocations)
|
|
{
|
|
if (!Directory.Exists(storageLocation.Path))
|
|
Directory.CreateDirectory(storageLocation.Path);
|
|
}
|
|
|
|
if (!Directory.Exists(SavesStorageLocation.Path))
|
|
Directory.CreateDirectory(SavesStorageLocation.Path);
|
|
|
|
if (!Directory.Exists(MediaStorageLocation.Path))
|
|
Directory.CreateDirectory(MediaStorageLocation.Path);
|
|
|
|
valid = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Logger?.LogError(ex, $"Error validating path");
|
|
MessageService.Error($"Error validating path: {ex.Message}", 10);
|
|
}
|
|
|
|
if (valid)
|
|
NavigationManager.NavigateTo("/FirstTimeSetup/Metadata");
|
|
}
|
|
|
|
void SetDefault(StorageLocation storageLocation)
|
|
{
|
|
foreach (var archiveStorageLocation in ArchiveStorageLocations)
|
|
{
|
|
archiveStorageLocation.Default = archiveStorageLocation.Id == storageLocation.Id;
|
|
}
|
|
}
|
|
|
|
void OnPathSelected(StorageLocation storageLocation, string path)
|
|
{
|
|
var appPath = Directory.GetCurrentDirectory();
|
|
|
|
if (path != null && path.StartsWith(appPath))
|
|
path = path.Substring(appPath.Length).TrimStart(Path.DirectorySeparatorChar).TrimEnd(Path.DirectorySeparatorChar);
|
|
|
|
storageLocation.Path = path;
|
|
}
|
|
|
|
void AddArchiveStorageLocation()
|
|
{
|
|
ArchiveStorageLocations.Add(new StorageLocation());
|
|
|
|
StateHasChanged();
|
|
}
|
|
|
|
void RemoveArchiveStorageLocation(StorageLocation storageLocation)
|
|
{
|
|
ArchiveStorageLocations.Remove(storageLocation);
|
|
}
|
|
}
|