using System.Reflection; using Microsoft.Extensions.FileProviders; namespace AIStudio.Tools; /// /// Reads the files below wwwroot, which every build embeds into the app. /// /// /// The app reads files such as its changelogs directly instead of requesting them over HTTP from /// its own server, which answers the app window only. The release build serves its static files /// from the same provider. /// public sealed class WebRootFiles { /// /// Gets the files below wwwroot. /// public IFileProvider FileProvider { get; } = new ManifestEmbeddedFileProvider(Assembly.GetAssembly(type: typeof(Program))!, "wwwroot"); /// /// Reads a text file below wwwroot. /// /// The path below wwwroot, such as changelog/v26.10.1.md. /// The text of the file, or an empty string when there is no such file. public async Task ReadTextAsync(string path) { var file = this.FileProvider.GetFileInfo(path); if (!file.Exists) return string.Empty; await using var fileStream = file.CreateReadStream(); using var reader = new StreamReader(fileStream); return await reader.ReadToEndAsync(); } /// /// Copies a file below wwwroot to the given place, such as one the user chose to save it to. /// /// The path below wwwroot, such as specs/eri/v1.json. /// Where to write the copy. An existing file there gets replaced. /// True when the copy was written, false when there is no such file below wwwroot. public async Task CopyToAsync(string path, string destinationPath) { var file = this.FileProvider.GetFileInfo(path); if (!file.Exists) return false; await using var source = file.CreateReadStream(); await using var destination = File.Create(destinationPath); await source.CopyToAsync(destination); return true; } }