Wp2ghost/GhostImageManager/Components/Pages/Settings.razor
2026-09-28 19:09:09 +02:00

192 lines
7.1 KiB
Text

@page "/settings"
@attribute [Authorize]
@rendermode InteractiveServer
@inject GhostConfigService ConfigService
<PageTitle>Einstellungen</PageTitle>
<h1>Ghost-Instanzen verwalten</h1>
<div class="row">
<div class="col-md-8">
<div class="card">
<div class="card-header">
<h5 class="mb-0">Konfigurierte Instanzen</h5>
</div>
<div class="card-body">
@if (_config.Instances.Count == 0)
{
<div class="alert alert-info">Keine Instanzen konfiguriert.</div>
}
else
{
<table class="table table-striped">
<thead>
<tr>
<th>Name</th>
<th>URL</th>
<th>Bilderverzeichnis</th>
<th class="text-end">Aktionen</th>
</tr>
</thead>
<tbody>
@foreach (var instance in _config.Instances)
{
<tr>
<td>@instance.Name</td>
<td>@instance.Url</td>
<td class="text-truncate" style="max-width: 300px;" title="@instance.ImageFolderPath">@instance.ImageFolderPath</td>
<td class="text-end">
<button class="btn btn-sm btn-outline-primary me-1" @onclick="() => EditInstance(instance)">Bearbeiten</button>
<button class="btn btn-sm btn-outline-danger" @onclick="() => DeleteInstance(instance)">Löschen</button>
</td>
</tr>
}
</tbody>
</table>
}
</div>
</div>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-header">
<h5 class="mb-0">@(_editingInstance.Id == null || _isNew ? "Neue Instanz" : "Instanz bearbeiten")</h5>
</div>
<div class="card-body">
<EditForm Model="_editingInstance" OnValidSubmit="SaveInstance">
<DataAnnotationsValidator />
<ValidationSummary />
<div class="mb-3">
<label class="form-label">Name</label>
<InputText @bind-Value="_editingInstance.Name" class="form-control" placeholder="z.B. Produktion" />
</div>
<div class="mb-3">
<label class="form-label">Ghost URL</label>
<InputText @bind-Value="_editingInstance.Url" class="form-control" placeholder="https://mein-blog.de" />
</div>
<div class="mb-3">
<label class="form-label">Admin API Key</label>
<InputText @bind-Value="_editingInstance.AdminApiKey" class="form-control" type="password" placeholder="id:secret" />
<div class="form-text">Format: {id}:{secret} aus den Ghost-Integrationen</div>
</div>
<div class="mb-3">
<label class="form-label">Content API Key (optional)</label>
<InputText @bind-Value="_editingInstance.ContentApiKey" class="form-control" placeholder="Optional" />
</div>
<div class="mb-3">
<label class="form-label">Lokaler Image-Ordner</label>
<InputText @bind-Value="_editingInstance.ImageFolderPath" class="form-control" placeholder="C:\\Ghost\\content\\images oder /var/www/ghost/content/images" />
<div class="form-text">Vollständiger Pfad zum Ghost content/images Ordner (Windows oder Linux)</div>
</div>
<div class="d-flex justify-content-between">
<button type="button" class="btn btn-secondary" @onclick="ResetForm">Zurücksetzen</button>
<button type="submit" class="btn btn-primary">Speichern</button>
</div>
</EditForm>
@if (!string.IsNullOrEmpty(_message))
{
<div class="alert alert-success mt-3">@_message</div>
}
</div>
</div>
</div>
</div>
@code {
private GhostConfig _config = new();
private GhostInstanceForm _editingInstance = new();
private bool _isNew = true;
private string? _message;
protected override void OnInitialized()
{
LoadConfig();
}
private void LoadConfig()
{
_config = ConfigService.Load();
}
private void EditInstance(GhostInstance instance)
{
_editingInstance = new GhostInstanceForm
{
Id = instance.Id,
Name = instance.Name,
Url = instance.Url,
AdminApiKey = instance.AdminApiKey,
ContentApiKey = instance.ContentApiKey,
ImageFolderPath = instance.ImageFolderPath
};
_isNew = false;
_message = null;
}
private void DeleteInstance(GhostInstance instance)
{
_config.Instances.Remove(instance);
ConfigService.Save(_config);
LoadConfig();
ResetForm();
}
private void SaveInstance()
{
if (_isNew)
{
_config.Instances.Add(new GhostInstance
{
Id = Guid.NewGuid().ToString("N")[..8],
Name = _editingInstance.Name,
Url = _editingInstance.Url.TrimEnd('/'),
AdminApiKey = _editingInstance.AdminApiKey,
ContentApiKey = _editingInstance.ContentApiKey,
ImageFolderPath = _editingInstance.ImageFolderPath
});
}
else
{
var existing = _config.Instances.FirstOrDefault(i => i.Id == _editingInstance.Id);
if (existing != null)
{
existing.Name = _editingInstance.Name;
existing.Url = _editingInstance.Url.TrimEnd('/');
existing.AdminApiKey = _editingInstance.AdminApiKey;
existing.ContentApiKey = _editingInstance.ContentApiKey;
existing.ImageFolderPath = _editingInstance.ImageFolderPath;
}
}
ConfigService.Save(_config);
LoadConfig();
ResetForm();
_message = "Instanz gespeichert.";
}
private void ResetForm()
{
_editingInstance = new GhostInstanceForm();
_isNew = true;
_message = null;
}
private class GhostInstanceForm
{
public string? Id { get; set; }
public string Name { get; set; } = "";
public string Url { get; set; } = "";
public string AdminApiKey { get; set; } = "";
public string ContentApiKey { get; set; } = "";
public string ImageFolderPath { get; set; } = "";
}
}