Files
SharepointToolbox-Web/Components/Pages/VersionCleanup.razor
T
kawa 5df7b72800 Add report logos and configurable folder scan depth
Report branding (top-left MSP logo, top-right client logo):
- Add MspLogo to AppSettings; client logo already on TenantProfile
- IUserSessionService.CurrentBranding composes MSP + active profile logo
- New reusable LogoUpload component (InputFile -> base64 LogoData, 512KB cap)
- MSP logo upload in Settings; optional client logo in profile create/edit
- Wire ReportBranding into all 6 HTML export pages
- Fix EditProfile dropping ClientLogo on edit

Storage metrics: expose folder scan depth (0-20) in scan options UI,
passed to existing StorageScanOptions.FolderDepth recursion.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-02 14:56:49 +02:00

149 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
@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();
_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 = 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 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"); }
}