155 lines
7.3 KiB
Plaintext
155 lines
7.3 KiB
Plaintext
@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
|
|
@rendermode InteractiveServer
|
|
|
|
<h1 class="page-title">File Search</h1>
|
|
|
|
@if (!Session.HasProfile) { <NoProfilePrompt /> return; }
|
|
|
|
<div class="card">
|
|
<div class="card-title">Search Options</div>
|
|
<SitePicker Profile="Session.CurrentProfile!" @bind-SelectedSites="_sites" />
|
|
<div class="form-row mt-8">
|
|
<div class="form-group">
|
|
<label class="form-label">File Extensions (comma-separated)</label>
|
|
<input class="form-input" @bind="_extensions" placeholder="docx, xlsx, pdf" />
|
|
</div>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label class="form-label">Regex filter (filename)</label>
|
|
<input class="form-input" @bind="_regex" placeholder="Optional regex pattern" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">Max results</label>
|
|
<input class="form-input" type="number" @bind="_maxResults" min="1" max="50000" style="width:120px" />
|
|
</div>
|
|
</div>
|
|
<div class="form-row">
|
|
<div class="form-group">
|
|
<label class="form-label">Created by</label>
|
|
<input class="form-input" @bind="_createdBy" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">Modified by</label>
|
|
<input class="form-input" @bind="_modifiedBy" />
|
|
</div>
|
|
<div class="form-group">
|
|
<label class="form-label">Library (optional)</label>
|
|
<input class="form-input" @bind="_library" />
|
|
</div>
|
|
</div>
|
|
<div class="flex-row mt-8">
|
|
<button class="btn btn-primary" @onclick="RunSearch" disabled="@_running">
|
|
@(_running ? "Searching…" : "Search")
|
|
</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</span></div>
|
|
<div class="spacer"></div>
|
|
<MergeModeSelect Value="_mergeMode" ValueChanged="v => _mergeMode = v" Visible="_bySite.Count > 1" />
|
|
<button class="btn btn-secondary btn-sm" @onclick="ExportCsv">Export CSV</button>
|
|
<button class="btn btn-secondary btn-sm" @onclick="ExportHtml">Export HTML</button>
|
|
</div>
|
|
<div class="data-table-wrap">
|
|
<table class="data-table">
|
|
<thead><tr><th>Name</th><th>Ext</th><th>Path</th><th>Created</th><th>Modified</th><th class="num">Size (KB)</th></tr></thead>
|
|
<tbody>
|
|
@foreach (var r in _results.Take(500))
|
|
{
|
|
<tr>
|
|
<td>@System.IO.Path.GetFileName(r.Path)</td>
|
|
<td>@r.FileExtension</td>
|
|
<td title="@r.Path" style="max-width:300px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap">@r.Path</td>
|
|
<td>@(r.Created?.ToString("yyyy-MM-dd") ?? "")</td>
|
|
<td>@(r.LastModified?.ToString("yyyy-MM-dd") ?? "")</td>
|
|
<td class="num">@((r.SizeBytes / 1024.0).ToString("F1"))</td>
|
|
</tr>
|
|
}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
@if (_results.Count > 500) { <div class="text-muted mt-8">Showing first 500 of @_results.Count. Export for full results.</div> }
|
|
</div>
|
|
}
|
|
|
|
@code {
|
|
private List<SiteInfo> _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<SearchResult> _results = new();
|
|
private List<(string Label, IReadOnlyList<SearchResult> 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 = "Please select at least one site."; _running = false; return; }
|
|
var progress = new Progress<OperationProgress>(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<SearchResult>)>();
|
|
var flat = new List<SearchResult>();
|
|
int i = 0;
|
|
foreach (var site in _sites)
|
|
{
|
|
_cts.Token.ThrowIfCancellationRequested();
|
|
_status = $"Searching {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 = $"Found {_results.Count} files across {_sites.Count} site(s).";
|
|
await Audit.LogAsync("FileSearch", Session.CurrentProfile?.Name ?? "", _sites.Select(s => s.Url),
|
|
$"{_results.Count} files; ext=[{_extensions}] regex=[{_regex}] lib=[{_library}]");
|
|
}
|
|
catch (OperationCanceledException) { _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);
|
|
}
|
|
}
|