-
Notifications
You must be signed in to change notification settings - Fork 560
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d82e5c3
commit 41e7c35
Showing
18 changed files
with
160 additions
and
116 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Html/IHtmlWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.CrossCuttingConcerns.Html; | ||
|
||
public interface IHtmlWriter<T> | ||
where T : IHtmlRequest | ||
{ | ||
Task WriteAsync(T data, Stream stream); | ||
|
||
Task<string> GetStringAsync(T data); | ||
} | ||
|
||
public interface IHtmlRequest | ||
{ | ||
} |
8 changes: 0 additions & 8 deletions
8
src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/HtmlGenerator/IHtmlGenerator.cs
This file was deleted.
Oops, something went wrong.
16 changes: 16 additions & 0 deletions
16
src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/Pdf/IPdfWriter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
|
||
public interface IPdfWriter<T> | ||
where T : IPdfRequest | ||
{ | ||
Task WriteAsync(T data, Stream stream); | ||
|
||
Task<byte[]> GetBytesAsync(T data); | ||
} | ||
|
||
public interface IPdfRequest | ||
{ | ||
} |
16 changes: 0 additions & 16 deletions
16
src/ModularMonolith/ClassifiedAds.CrossCuttingConcerns/PdfConverter/IPdfConverter.cs
This file was deleted.
Oops, something went wrong.
9 changes: 3 additions & 6 deletions
9
...tors/HtmlGeneratorCollectionExtensions.cs → ...tructure/Html/HtmlCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 0 additions & 20 deletions
20
src/ModularMonolith/ClassifiedAds.Infrastructure/HtmlGenerators/HtmlGenerator.cs
This file was deleted.
Oops, something went wrong.
8 changes: 2 additions & 6 deletions
8
...DinkToPdfConverterCollectionExtensions.cs → ...inkToPdf/DinkToPdfCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,13 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.PdfConverter; | ||
using ClassifiedAds.Infrastructure.PdfConverters.DinkToPdf; | ||
using DinkToPdf; | ||
using DinkToPdf; | ||
using DinkToPdf.Contracts; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class DinkToPdfConverterCollectionExtensions | ||
public static class DinkToPdfCollectionExtensions | ||
{ | ||
public static IServiceCollection AddDinkToPdfConverter(this IServiceCollection services) | ||
{ | ||
services.AddSingleton(typeof(IConverter), new SynchronizedConverter(new PdfTools())); | ||
services.AddSingleton<IPdfConverter, DinkToPdfConverter>(); | ||
|
||
return services; | ||
} | ||
} |
9 changes: 2 additions & 7 deletions
9
...teerSharpConverterCollectionExtensions.cs → ...arp/PuppeteerSharpCollectionExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,13 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.PdfConverter; | ||
using ClassifiedAds.Infrastructure.PdfConverters.PuppeteerSharp; | ||
using PuppeteerSharp; | ||
using PuppeteerSharp; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection; | ||
|
||
public static class PuppeteerSharpConverterCollectionExtensions | ||
public static class PuppeteerSharpCollectionExtensions | ||
{ | ||
public static IServiceCollection AddPuppeteerSharpPdfConverter(this IServiceCollection services) | ||
{ | ||
var browserFetcher = new BrowserFetcher(); | ||
browserFetcher.DownloadAsync().GetAwaiter().GetResult(); | ||
|
||
services.AddSingleton<IPdfConverter, PuppeteerSharpConverter>(); | ||
|
||
return services; | ||
} | ||
} |
25 changes: 0 additions & 25 deletions
25
...lith/ClassifiedAds.Infrastructure/PdfConverters/PuppeteerSharp/PuppeteerSharpConverter.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/ModularMonolith/ClassifiedAds.Modules.Product/Html/ExportProductsToHtml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Html; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Modules.Product.Html; | ||
|
||
public record ExportProductsToHtml : IHtmlRequest | ||
{ | ||
public List<Entities.Product> Products { get; set; } | ||
} |
31 changes: 31 additions & 0 deletions
31
src/ModularMonolith/ClassifiedAds.Modules.Product/Html/ExportProductsToHtmlHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Html; | ||
using RazorLight; | ||
using System; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Modules.Product.Html; | ||
|
||
public class ExportProductsToHtmlHandler : IHtmlWriter<ExportProductsToHtml> | ||
{ | ||
private readonly IRazorLightEngine _razorLightEngine; | ||
|
||
public ExportProductsToHtmlHandler(IRazorLightEngine razorLightEngine) | ||
{ | ||
_razorLightEngine = razorLightEngine; | ||
} | ||
|
||
public async Task WriteAsync(ExportProductsToHtml data, Stream stream) | ||
{ | ||
using var sw = new StreamWriter(stream, Encoding.UTF8); | ||
await sw.WriteAsync(await GetStringAsync(data)); | ||
} | ||
|
||
public async Task<string> GetStringAsync(ExportProductsToHtml data) | ||
{ | ||
var template = Path.Combine(Environment.CurrentDirectory, $"Templates/ProductList.cshtml"); | ||
string html = await _razorLightEngine.CompileRenderAsync(template, data.Products); | ||
return html; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
src/ModularMonolith/ClassifiedAds.Modules.Product/Pdf/ExportProductsToPdf.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
using System.Collections.Generic; | ||
|
||
namespace ClassifiedAds.Modules.Product.Pdf; | ||
|
||
public record ExportProductsToPdf : IPdfRequest | ||
{ | ||
public List<Entities.Product> Products { get; set; } | ||
} |
39 changes: 39 additions & 0 deletions
39
...arMonolith/ClassifiedAds.Modules.Product/Pdf/PuppeteerSharp/ExportProductsToPdfHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Html; | ||
using ClassifiedAds.CrossCuttingConcerns.Pdf; | ||
using ClassifiedAds.Modules.Product.Html; | ||
using PuppeteerSharp; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Modules.Product.Pdf.PuppeteerSharp; | ||
|
||
public class ExportProductsToPdfHandler : IPdfWriter<ExportProductsToPdf> | ||
{ | ||
private readonly IHtmlWriter<ExportProductsToHtml> _htmlWriter; | ||
|
||
public ExportProductsToPdfHandler(IHtmlWriter<ExportProductsToHtml> htmlWriter) | ||
{ | ||
_htmlWriter = htmlWriter; | ||
} | ||
|
||
public async Task<byte[]> GetBytesAsync(ExportProductsToPdf data) | ||
{ | ||
var html = await _htmlWriter.GetStringAsync(new ExportProductsToHtml { Products = data.Products }); | ||
await using var browser = await Puppeteer.LaunchAsync(new LaunchOptions { Headless = true }); | ||
await using var page = await browser.NewPageAsync(); | ||
await page.SetContentAsync(html); | ||
|
||
var bytes = await page.PdfDataAsync(new PdfOptions | ||
{ | ||
PrintBackground = true, | ||
}); | ||
|
||
return bytes; | ||
} | ||
|
||
public async Task WriteAsync(ExportProductsToPdf data, Stream stream) | ||
{ | ||
using var sw = new BinaryWriter(stream); | ||
sw.Write(await GetBytesAsync(data)); | ||
} | ||
} |
Oops, something went wrong.