using System.IO; using System.Text; namespace SharepointToolbox.Services.Export; /// /// Central file-write plumbing for export services so every CSV and HTML /// artefact gets a consistent encoding: CSV files are written with a UTF-8 /// BOM (required for Excel to detect the encoding when opening a /// double-clicked .csv), HTML files are written without a BOM (some browsers /// and iframe srcdoc paths render the BOM as a visible character). /// Export services should call these helpers rather than constructing /// inline. /// internal static class ExportFileWriter { private static readonly UTF8Encoding Utf8WithBom = new(encoderShouldEmitUTF8Identifier: true); private static readonly UTF8Encoding Utf8NoBom = new(encoderShouldEmitUTF8Identifier: false); /// Writes to as UTF-8 with BOM. public static Task WriteCsvAsync(string filePath, string csv, CancellationToken ct) => File.WriteAllTextAsync(filePath, csv, Utf8WithBom, ct); /// Writes to as UTF-8 without BOM. public static Task WriteHtmlAsync(string filePath, string html, CancellationToken ct) => File.WriteAllTextAsync(filePath, html, Utf8NoBom, ct); }