LANCommander/LANCommander.Server/UI/Pages/Settings/Media/Index.razor
Pat Hartl add770b227 Refactor Settings
Settings for the server are now combined with the settings from the SDK. This introduces a large breaking change and a migration should be created. This refactor utilizes .NET Configuration and the Options pattern. This will allow for the overriding of any setting using envionment variables. It also means that Settings.yml can be used to override any .NET configuration. A SettingsProvider implementation was created, and any updating of settings was changed from SettingsService.SaveSettings() to SettingsProvider.Update(s => { ... })
2025-11-16 12:31:25 -06:00

104 lines
3.7 KiB
Text

@page "/Settings/Media"
@using LANCommander.SDK.Enums
@using LANCommander.Server.Settings
@using LANCommander.Server.Settings.Models
@using LANCommander.Server.UI.Pages.Settings.Media.Components
@using Microsoft.Extensions.Options
@inject StorageLocationService StorageLocationService
@inject IOptions<Settings> Settings
@inject SettingsProvider<Settings> SettingsProvider
@inject IMessageService MessageService
@inject ILogger<Media> Logger
@attribute [Authorize(Roles = RoleService.AdministratorRoleName)]
<PageHeader Title="Media">
<PageHeaderExtra>
<Button OnClick="Save" Type="@ButtonType.Primary">Save</Button>
</PageHeaderExtra>
</PageHeader>
<PageContent>
<Form Model="Settings.Value" Layout="@FormLayout.Vertical">
<FormItem Label="Storage Path">
<FilePicker Root="@RootPath" EntrySelectable="x => x is FileManagerDirectory" @bind-Value="@StorageLocation.Path" OkText="Select Path" Title="Choose Path" OnSelected="OnPathSelected" />
</FormItem>
<Divider Text="SteamGridDB Credentials" />
<Text Type="@TextElementType.Secondary">In order to automatically search SteamGridDB for media, you need an API key. <a href="https://www.steamgriddb.com/profile/preferences/api" target="_blank">Click here</a> to get your key.</Text>
<FormItem Label="API Key">
<InputPassword @bind-Value="context.Server.Media.SteamGridDbApiKey" />
</FormItem>
<CascadingValue Value="Settings">
<Collapse Style="margin-bottom: 24px;">
@foreach (var mediaType in Enum.GetValues<MediaType>())
{
if (_mediaTypeSettings.ContainsKey(mediaType))
{
<MediaTypeSettingsEditor @bind-Value="@(_mediaTypeSettings[mediaType])" />
}
}
</Collapse>
</CascadingValue>
</Form>
</PageContent>
@code {
string RootPath = Path.GetPathRoot(Directory.GetCurrentDirectory());
StorageLocation StorageLocation = new();
Dictionary<MediaType, MediaTypeSettings> _mediaTypeSettings = new();
protected override async Task OnInitializedAsync()
{
StorageLocation = await StorageLocationService.FirstOrDefaultAsync(l => l.Default && l.Type == SDK.Enums.StorageLocationType.Media);
if (StorageLocation == null)
{
StorageLocation = new StorageLocation
{
Default = true,
Path = "Media",
Type = StorageLocationType.Media
};
StorageLocation = await StorageLocationService.AddAsync(StorageLocation);
}
_mediaTypeSettings = Settings.Value.Server.Media.MediaTypes.DistinctBy(m => m.Type).ToDictionary(m => m.Type, m => m);
}
async Task Save()
{
try
{
Settings.Value.Server.Media.MediaTypes = _mediaTypeSettings.Values;
SettingsProvider.Update(s =>
{
s.Server.Media = Settings.Value.Server.Media;
});
await StorageLocationService.UpdateAsync(StorageLocation);
MessageService.Success("Settings saved!");
}
catch (Exception ex)
{
MessageService.Error("An unknown error occurred.");
Logger.LogError(ex, "An unknown error occurred.");
}
}
void OnPathSelected(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;
}
}