Initial commit
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
@page "/versions"
|
||||
@attribute [Authorize]
|
||||
@inject IUserSessionService Session
|
||||
@inject IUserContextAccessor UserContext
|
||||
@inject ISessionManager SessionMgr
|
||||
@inject IVersionCleanupService VersionSvc
|
||||
@inject VersionCleanupHtmlExportService HtmlExport
|
||||
@inject WebExportService WebExport
|
||||
@rendermode InteractiveServer
|
||||
|
||||
<h1 class="page-title">Version Cleanup</h1>
|
||||
|
||||
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
||||
@if (UserContext.Role < UserRole.TechN1) { <WriteGuard /> return; }
|
||||
|
||||
<div class="card">
|
||||
<div class="form-row">
|
||||
<div class="form-group">
|
||||
<label class="form-label">Site URL</label>
|
||||
<input class="form-input" @bind="_siteUrl" placeholder="@Session.CurrentProfile!.TenantUrl" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex-row">
|
||||
<button class="btn btn-secondary" @onclick="LoadLibraries" disabled="@_loading">
|
||||
@(_loading ? "Loading…" : "Load Libraries")
|
||||
</button>
|
||||
</div>
|
||||
@if (_libraries.Count > 0)
|
||||
{
|
||||
<div class="form-group mt-8">
|
||||
<label class="form-label">Libraries (none = all)</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">Keep last N versions</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" /> Keep first version</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="flex-row mt-8">
|
||||
<button class="btn btn-danger" @onclick="RunCleanup" disabled="@_running">
|
||||
@(_running ? "Cleaning…" : "Delete Old Versions")
|
||||
</button>
|
||||
@if (_running) { <button class="btn btn-secondary" @onclick="Cancel">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 string _siteUrl = string.Empty;
|
||||
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 = string.IsNullOrWhiteSpace(_siteUrl) ? Session.CurrentProfile!.TenantUrl : _siteUrl.Trim();
|
||||
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, CancellationToken.None);
|
||||
_libraries = (await VersionSvc.ListLibraryTitlesAsync(ctx, 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 = string.IsNullOrWhiteSpace(_siteUrl) ? Session.CurrentProfile!.TenantUrl : _siteUrl.Trim();
|
||||
var progress = new Progress<OperationProgress>(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
|
||||
try
|
||||
{
|
||||
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, _cts.Token);
|
||||
var opts = new VersionCleanupOptions(_selectedLibs, _keepLast, _keepFirst);
|
||||
_results = (await VersionSvc.DeleteOldVersionsAsync(ctx, opts, progress, _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), $"versions_{DateTime.Now:yyyyMMdd_HHmmss}.html"); }
|
||||
}
|
||||
Reference in New Issue
Block a user