@page "/folder-structure"
@attribute [Authorize]
@inject IUserSessionService Session
@inject IUserContextAccessor UserContext
@inject ISessionManager SessionMgr
@inject IElevationCoordinator Elevation
@inject IFolderStructureService FolderSvc
@inject ICsvValidationService CsvValidation
@rendermode InteractiveServer
Folder Structure
Create folder hierarchies in a document library from a CSV template.
@if (!Session.HasProfile) { return; }
@if (UserContext.Role < UserRole.TechN1) { return; }
@if (_rows.Count > 0)
{
@_rows.Count(r => r.IsValid) valid rows, @_rows.Count(r => !r.IsValid) errors.
}
@if (_running) { }
@if (!string.IsNullOrEmpty(_error)) { @_error
}
@if (_summary != null)
{
Created: @_summary.SuccessCount folders. Failures: @_summary.FailedCount
}
@code {
private string _siteUrl = string.Empty, _libraryTitle = string.Empty;
private List> _rows = new();
private bool _running; private string _status = string.Empty, _error = string.Empty;
private int _current, _total;
private BulkOperationSummary? _summary;
private CancellationTokenSource? _cts;
private async Task LoadFile(InputFileChangeEventArgs e)
{
_rows.Clear();
using var stream = e.File.OpenReadStream(maxAllowedSize: 5 * 1024 * 1024);
_rows = CsvValidation.ParseAndValidateFolders(stream);
}
private async Task RunCreate()
{
_error = string.Empty; _summary = null; _running = true;
_cts = new CancellationTokenSource();
var validRows = _rows.Where(r => r.IsValid && r.Record != null).Select(r => r.Record!).ToList();
var siteUrl = string.IsNullOrWhiteSpace(_siteUrl) ? Session.CurrentProfile!.TenantUrl : _siteUrl.Trim();
var progress = new Progress(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
try
{
_summary = await Elevation.RunAsync(async c =>
{
var ctx = await SessionMgr.GetOrCreateContextAsync(siteUrl, Session.CurrentProfile!, c);
return await FolderSvc.CreateFoldersAsync(ctx, _libraryTitle, validRows, progress, c);
}, _cts.Token);
_status = $"Complete: {_summary.SuccessCount} folders created.";
}
catch (OperationCanceledException) { _status = "Cancelled."; }
catch (Exception ex) { _error = ex.Message; }
finally { _running = false; await InvokeAsync(StateHasChanged); }
}
private void Cancel() => _cts?.Cancel();
}