private async Task _GenerateProjectLogosAsync(ProjectInfo project, IHtmlSite site) { var _logo = _LogoBuilder.GetLogo(project); var _svgDocument = await _logo.GetSvgDocumentAsync().ConfigureAwait(false); var _svgResource = HtmlStreamResourceProperties.CreateStreamResource(null, () => { var _stream = new MemoryStream(); _svgDocument.Save(_stream); _stream.Position = 0; return(_stream); }); _svgResource.Name = new[] { "external-assets", "logo", "svg", _logo.Key + ".svg" }; site.Resources.Add(_svgResource); foreach (var _pixelSize in new[] { 16, 32, 48, 256, 512, 4096 }) { var _sizeString = _pixelSize.ToString(CultureInfo.InvariantCulture); var _pngResource = HtmlStreamResourceProperties.CreateStreamResource("image/png", await _logo.GetPngDataAsync(_pixelSize).ConfigureAwait(false)); _pngResource.Name = new[] { "external-assets", "logo", _sizeString + "x" + _sizeString + "_png", _logo.Key + ".png" }; site.Resources.Add(_pngResource); } }
public static async Task WriteToDirectory(this IHtmlSite site, string directoryPath, CancellationToken cancellationToken = default(CancellationToken)) { Must.Assertion .AssertArgumentNotNull(site, nameof(site)) .Assert(Directory.Exists(directoryPath)); foreach (var _resource in site.Resources) { cancellationToken.ThrowIfCancellationRequested(); var _filePath = Path.Combine(directoryPath, Path.Combine(_resource.Name.Select(_SanitizePathPart).ToArray())); if (!Path.HasExtension(_filePath) && _resource is IHtmlDocumentResource) { _filePath = Path.Combine(_filePath, "index.html"); } var _fileDirectoryPath = Path.GetDirectoryName(_filePath); if (!Directory.Exists(_fileDirectoryPath)) { Directory.CreateDirectory(_fileDirectoryPath); } using (var _fileStream = new FileStream(_filePath, FileMode.Create, FileAccess.Write, FileShare.None)) { await _resource.WriteToStreamAsync(_fileStream).ConfigureAwait(false); await _fileStream.FlushAsync(cancellationToken).ConfigureAwait(false); } } }
private static void _AddFileResourcesToSite(IHtmlSite site, string resourcesDirectoryPath) { var _cleanResourcesDirectoryPath = resourcesDirectoryPath.EndsWith("\\", StringComparison.Ordinal) ? resourcesDirectoryPath.Substring(0, resourcesDirectoryPath.Length - 1) : resourcesDirectoryPath; foreach (var _resourceFilePath in Directory.GetFiles(_cleanResourcesDirectoryPath, "*", SearchOption.AllDirectories)) { Must.Assertion .Assert(_resourceFilePath.StartsWith(_cleanResourcesDirectoryPath, StringComparison.Ordinal)); var _nameParts = _resourceFilePath.Substring(_cleanResourcesDirectoryPath.Length + 1).Split(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar); var _resource = HtmlStreamResourceProperties.CreateStreamResource(null, _ => new FileStream(_resourceFilePath, FileMode.Open, FileAccess.Read)); _resource.Name = new[] { "assets" }.Concat(_nameParts).ToArray(); site.Resources.Add(_resource); } }