@page "/search"
@attribute [Authorize]
@inject IUserSessionService Session
@inject ISessionManager SessionMgr
@inject IElevationCoordinator Elevation
@inject ISearchService SearchSvc
@inject SearchCsvExportService CsvExport
@inject SearchHtmlExportService HtmlExport
@inject WebExportService WebExport
@inject IAuditService Audit
@inject TranslationSource T
@rendermode InteractiveServer
@T["tab.search"]
@if (!Session.HasProfile) { return; }
@T["srch.options"]
@if (_running) { }
@if (!string.IsNullOrEmpty(_error)) { @_error
}
@if (_results.Count > 0)
{
@T["srch.results"] @_results.Count
| @T["srch.col.name"] | @T["srch.col.ext"] | @T["srch.col.path"] | @T["srch.col.created"] | @T["srch.col.modified"] | @T["srch.col.size.kb"] |
@foreach (var r in _results.Take(500))
{
| @System.IO.Path.GetFileName(r.Path) |
@r.FileExtension |
@r.Path |
@(r.Created?.ToString("yyyy-MM-dd") ?? "") |
@(r.LastModified?.ToString("yyyy-MM-dd") ?? "") |
@((r.SizeBytes / 1024.0).ToString("F1")) |
}
@if (_results.Count > 500) {
@(string.Format(T["srch.truncated"], _results.Count))
}
}
@code {
private List _sites = new();
private string _extensions = string.Empty, _regex = string.Empty;
private string _createdBy = string.Empty, _modifiedBy = string.Empty, _library = string.Empty;
private int _maxResults = 5000;
private bool _running; private string _status = string.Empty, _error = string.Empty;
private int _current, _total;
private List _results = new();
private List<(string Label, IReadOnlyList Results)> _bySite = new();
private ReportMergeMode _mergeMode = ReportMergeMode.SingleMerged;
private CancellationTokenSource? _cts;
private async Task RunSearch()
{
_error = string.Empty; _results = new(); _bySite = new(); _running = true;
_cts = new CancellationTokenSource();
if (_sites.Count == 0) { _error = T["srch.err.noSites"]; _running = false; return; }
var progress = new Progress(p => { _status = p.Message; _current = p.Current; _total = p.Total; InvokeAsync(StateHasChanged); });
try
{
var exts = _extensions.Split(',', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries);
var bySite = new List<(string, IReadOnlyList)>();
var flat = new List();
int i = 0;
foreach (var site in _sites)
{
_cts.Token.ThrowIfCancellationRequested();
_status = string.Format(T["srch.status.searchingSite"], site.Title, ++i, _sites.Count);
await InvokeAsync(StateHasChanged);
var opts = new SearchOptions(exts, _regex, null, null, null, null, _createdBy.TrimOrNull(), _modifiedBy.TrimOrNull(), _library.TrimOrNull(), _maxResults, site.Url);
var found = await Elevation.RunAsync(async c =>
{
var ctx = await SessionMgr.GetOrCreateContextAsync(site.Url, Session.CurrentProfile!, c);
return await SearchSvc.SearchFilesAsync(ctx, opts, progress, c);
}, _cts.Token);
bySite.Add((site.Title, found));
flat.AddRange(found);
}
_bySite = bySite; _results = flat;
_status = string.Format(T["srch.status.found"], _results.Count, _sites.Count);
await Audit.LogAsync("FileSearch", Session.CurrentProfile?.Name ?? "", _sites.Select(s => s.Url),
$"{_results.Count} files; ext=[{_extensions}] regex=[{_regex}] lib=[{_library}]");
}
catch (OperationCanceledException) { _status = T["status.cancelled"]; }
catch (Exception ex) { _error = ex.Message; }
finally { _running = false; await InvokeAsync(StateHasChanged); }
}
private void Cancel() => _cts?.Cancel();
private async Task ExportCsv()
{
var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var output = ReportMergeHelper.Build(_bySite, _mergeMode, "search", ts, ReportFormat.Csv, rs => CsvExport.BuildCsv(rs));
await WebExport.DownloadBytesAsync(output.Bytes, output.FileName, output.Mime);
}
private async Task ExportHtml()
{
var ts = DateTime.Now.ToString("yyyyMMdd_HHmmss");
var output = ReportMergeHelper.Build(_bySite, _mergeMode, "search", ts, ReportFormat.Html, rs => HtmlExport.BuildHtml(rs, Session.CurrentBranding));
await WebExport.DownloadBytesAsync(output.Bytes, output.FileName, output.Mime);
}
}