using System.IO; using System.Text; using SharepointToolbox.Core.Models; using SharepointToolbox.Localization; namespace SharepointToolbox.Services.Export; /// /// Exports SearchResult list to a self-contained sortable/filterable HTML report. /// Port of PS Export-SearchToHTML (PS lines 2112-2233). /// Columns are sortable by clicking the header. A filter input narrows rows by text match. /// public class SearchHtmlExportService { public string BuildHtml(IReadOnlyList results, ReportBranding? branding = null) { var T = TranslationSource.Instance; var sb = new StringBuilder(); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine(""); sb.AppendLine($"{T["report.title.search"]}"); sb.AppendLine(""" """); sb.Append(BrandingHtmlHelper.BuildBrandingHeader(branding)); sb.AppendLine($"""

{T["report.title.search_short"]}

"""); sb.AppendLine($""" """); foreach (var r in results) { string fileName = System.IO.Path.GetFileName(r.Path); if (string.IsNullOrEmpty(fileName)) fileName = r.Title; sb.AppendLine($""" """); } sb.AppendLine(" \n
{T["report.col.file_name"]} {T["report.col.extension"]} {T["report.col.path"]} {T["report.col.created"]} {T["report.col.created_by"]} {T["report.col.modified"]} {T["report.col.modified_by"]} {T["report.col.size"]}
{H(fileName)} {H(r.FileExtension)} {H(r.Path)} {(r.Created.HasValue ? r.Created.Value.ToString("yyyy-MM-dd") : string.Empty)} {H(r.Author)} {(r.LastModified.HasValue ? r.LastModified.Value.ToString("yyyy-MM-dd") : string.Empty)} {H(r.ModifiedBy)} {FormatSize(r.SizeBytes)}
"); int count = results.Count; sb.AppendLine($"

{T["report.text.generated_colon"]} {DateTime.Now:yyyy-MM-dd HH:mm} — {count:N0} {T["report.text.results_parens"]}

"); sb.AppendLine($$""" """); return sb.ToString(); } public async Task WriteAsync(IReadOnlyList results, string filePath, CancellationToken ct, ReportBranding? branding = null) { var html = BuildHtml(results, branding); await System.IO.File.WriteAllTextAsync(filePath, html, Encoding.UTF8, ct); } private static string H(string value) => System.Net.WebUtility.HtmlEncode(value ?? string.Empty); private static string FormatSize(long bytes) { if (bytes >= 1_073_741_824L) return $"{bytes / 1_073_741_824.0:F2} GB"; if (bytes >= 1_048_576L) return $"{bytes / 1_048_576.0:F2} MB"; if (bytes >= 1024L) return $"{bytes / 1024.0:F2} KB"; return $"{bytes} B"; } }