147 lines
6.7 KiB
Plaintext
147 lines
6.7 KiB
Plaintext
@page "/versions"
|
|
@attribute [Authorize]
|
|
@inject IUserSessionService Session
|
|
@inject IUserContextAccessor UserContext
|
|
@inject ISessionManager SessionMgr
|
|
@inject IElevationCoordinator Elevation
|
|
@inject IVersionCleanupService VersionSvc
|
|
@inject VersionCleanupHtmlExportService HtmlExport
|
|
@inject WebExportService WebExport
|
|
@inject TranslationSource T
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">@T["versions.page.title"]</h1>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
|
|
|
<div class="card">
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_sites" Single="true" />
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-secondary" @onclick="LoadLibraries" disabled="@_loading">
|
|
@(_loading ? T["versions.btn.loading"] : T["versions.btn.loadLibs"])
|
|
</button>
|
|
</div>
|
|
@if (_libraries.Count > 0)
|
|
{
|
|
<div class="form-group mt-8">
|
|
<label class="form-label">@T["versions.lbl.libsNoneAll"]</label>
|
|
<div style="max-height:200px;overflow-y:auto;border:1px solid var(--border);border-radius:4px;padding:4px">
|
|
@foreach (var lib in _libraries)
|
|
{
|
|
<label style="display:flex;align-items:center;gap:6px;padding:4px 8px;cursor:pointer">
|
|
<input type="checkbox" checked="@_selectedLibs.Contains(lib)" @onchange="e => ToggleLib(lib, (bool)e.Value!)" />
|
|
@lib
|
|
</label>
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
<div class="form-row mt-8">
|
|
<div class="form-group" style="flex:0 0 auto">
|
|
<label class="form-label">@T["versions.lbl.keepLastN"]</label>
|
|
<input class="form-input" type="number" @bind="_keepLast" min="0" max="999" style="width:80px" />
|
|
</div>
|
|
<div class="form-group" style="display:flex;align-items:center;padding-top:20px">
|
|
<label><input type="checkbox" @bind="_keepFirst" /> @T["versions.chk.keepFirstShort"]</label>
|
|
</div>
|
|
</div>
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-danger" @onclick="RunCleanup" disabled="@_running">
|
|
@(_running ? T["versions.btn.cleaning"] : T["versions.btn.run"])
|
|
</button>
|
|
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">@T["btn.cancel"]</button> }
|
|
</div>
|
|
<ProgressPanel IsRunning="_running" StatusMessage="@_status" Current="_current" Total="_total" />
|
|
</div>
|
|
|
|
@if (!string.IsNullOrEmpty(_error)) { <div class="alert alert-error">@_error</div> }
|
|
|
|
@if (_results.Count > 0)
|
|
{
|
|
<div class="card">
|
|
<div class="flex-row">
|
|
<div class="card-title">Results <span class="count-badge">@_results.Count files</span></div>
|
|
<div class="spacer"></div>
|
|
<button class="btn btn-secondary btn-sm" @onclick="ExportHtml">Export HTML</button>
|
|
</div>
|
|
<div class="alert alert-info">
|
|
Versions deleted: <strong>@_results.Sum(r => r.VersionsDeleted)</strong> |
|
|
Freed: <strong>@((_results.Sum(r => r.BytesFreed) / 1048576.0).ToString("F2")) MB</strong> |
|
|
Errors: <strong>@_results.Count(r => r.Error != null)</strong>
|
|
</div>
|
|
<div class="data-table-wrap">
|
|
<table class="data-table">
|
|
<thead><tr><th>Library</th><th>File</th><th class="num">Before</th><th class="num">Deleted</th><th class="num">Freed (KB)</th><th>Error</th></tr></thead>
|
|
<tbody>
|
|
@foreach (var r in _results.Take(500))
|
|
{
|
|
<tr>
|
|
<td>@r.Library</td>
|
|
<td title="@r.FileServerRelativeUrl">@r.FileName</td>
|
|
<td class="num">@r.VersionsBefore</td>
|
|
<td class="num">@r.VersionsDeleted</td>
|
|
<td class="num">@((r.BytesFreed / 1024.0).ToString("F1"))</td>
|
|
<td style="color:var(--danger)">@r.Error</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private List<SiteInfo> _sites = new();
|
|
private int _keepLast = 5; private bool _keepFirst;
|
|
private List<string> _libraries = new(), _selectedLibs = new();
|
|
private bool _running, _loading; private string _status = string.Empty, _error = string.Empty;
|
|
private int _current, _total;
|
|
private List<VersionCleanupResult> _results = new();
|
|
private CancellationTokenSource? _cts;
|
|
|
|
private async Task LoadLibraries()
|
|
{
|
|
_loading = true; _error = string.Empty;
|
|
try
|
|
{
|
|
var siteUrl = _sites.FirstOrDefault()?.Url;
|
|
if (string.IsNullOrWhiteSpace(siteUrl)) { _error = "Please select a site."; return; }
|
|
_libraries = (await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await VersionSvc.ListLibraryTitlesAsync(ctx, c);
|
|
}, CancellationToken.None)).ToList();
|
|
}
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _loading = false; }
|
|
}
|
|
|
|
private void ToggleLib(string lib, bool selected) { if (selected) _selectedLibs.Add(lib); else _selectedLibs.Remove(lib); }
|
|
|
|
private async Task RunCleanup()
|
|
{
|
|
_error = string.Empty; _results.Clear(); _running = true;
|
|
_cts = new CancellationTokenSource();
|
|
var siteUrl = _sites.FirstOrDefault()?.Url;
|
|
if (string.IsNullOrWhiteSpace(siteUrl)) { _error = "Please select a site."; _running = false; return; }
|
|
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
|
try
|
|
{
|
|
var opts = new VersionCleanupOptions(_selectedLibs, _keepLast, _keepFirst);
|
|
_results = (await Elevation.RunAsync(async c =>
|
|
{
|
|
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
|
|
return await VersionSvc.DeleteOldVersionsAsync(ctx, opts, progress, c);
|
|
}, _cts.Token)).ToList();
|
|
_status = $"Complete: {_results.Sum(r => r.VersionsDeleted)} versions deleted.";
|
|
}
|
|
catch (OperationCanceledException) { _status = "Cancelled."; }
|
|
catch (Exception ex) { _error = ex.Message; }
|
|
finally { _running = false; await InvokeAsync(StateHasChanged); }
|
|
}
|
|
|
|
private void Cancel() => _cts?.Cancel();
|
|
private async Task ExportHtml() { await WebExport.DownloadHtmlAsync(HtmlExport.BuildHtml(_results, Session.CurrentBranding), $"versions_{DateTime.Now:yyyyMMdd_HHmmss}.html"); }
|
|
}
|