Skip to content

Commit

Permalink
Cache work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Kees committed Oct 13, 2023
1 parent bac2fa3 commit 47cfd4b
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 36 deletions.
60 changes: 43 additions & 17 deletions ChromiumHtmlToPdfLib/Converter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,17 @@ private enum OutputFormat
/// <summary>
/// <see cref="SetDiskCache"/>
/// </summary>
private DirectoryInfo _cacheDirectory;
private string _cacheDirectory;

/// <summary>
/// <see cref="SetDiskCache"/>
/// </summary>
private long _cacheSize;

/// <summary>
/// <see cref="GetDocumentHelper"/>
/// </summary>
private DocumentHelper _documentHelper;
#endregion

#region Properties
Expand Down Expand Up @@ -352,9 +357,14 @@ private DirectoryInfo GetTempDirectory
public bool DoNotDeleteTempDirectory { get; set; }

/// <summary>
/// The directory used for temporary files
/// The directory used for temporary files (<see cref="GetTempDirectory"/>)
/// </summary>
public DirectoryInfo CurrentTempDirectory { get; internal set; }

/// <summary>
/// The directory used for cached files (<see cref="GetCacheDirectory"/>)
/// </summary>
public DirectoryInfo CurrentTempDirectory { get; set; }
public DirectoryInfo CurrentCacheDirectory { get; internal set; }

/// <summary>
/// Returns a <see cref="WebProxy" /> object
Expand Down Expand Up @@ -443,16 +453,18 @@ public bool DiskCacheDisabled
}

/// <summary>
/// Returns a reference to the temp directory
/// Returns a reference to the cache directory
/// </summary>
private DirectoryInfo GetCacheDirectory
{
get
{
// TODO: Make code work
CurrentTempDirectory = _tempDirectory == null
? new DirectoryInfo(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()))
: new DirectoryInfo(Path.Combine(_tempDirectory, Guid.NewGuid().ToString()));
if (_cacheDirectory != null)
CurrentCacheDirectory = new DirectoryInfo(_cacheDirectory);
else
CurrentCacheDirectory = _tempDirectory == null
? new DirectoryInfo(Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()))
: new DirectoryInfo(Path.Combine(_tempDirectory, Guid.NewGuid().ToString()));

if (!CurrentTempDirectory.Exists)
CurrentTempDirectory.Create();
Expand All @@ -461,6 +473,18 @@ private DirectoryInfo GetCacheDirectory
}
}

private DocumentHelper GetDocumentHelper
{
get
{
if (_documentHelper != null)
return _documentHelper;

_documentHelper = new DocumentHelper(GetTempDirectory, _useCache, GetCacheDirectory, _cacheSize, WebProxy, ImageLoadTimeout, _logger) { InstanceId = InstanceId };
return _documentHelper;
}
}

/// <summary>
/// The timeout in milliseconds when waiting for a websocket to open
/// </summary>
Expand Down Expand Up @@ -1035,20 +1059,22 @@ public void SetUserAgent(string value)
/// </remarks>
public void SetDiskCache(string directory, long? size)
{
_cacheDirectory = new DirectoryInfo(directory);
var temp = new DirectoryInfo(_cacheDirectory);

if (!_cacheDirectory.Exists)
if (!temp.Exists)
throw new DirectoryNotFoundException($"The directory '{directory}' does not exists");

AddChromiumArgument("--disk-cache-dir", directory.TrimEnd('\\', '/'));
_cacheDirectory = temp.FullName;

// Chromium does not like a trailing slash
AddChromiumArgument("--disk-cache-dir", _cacheDirectory.TrimEnd('\\', '/'));

if (size.HasValue)
{
if (size.Value <= 0)
throw new ArgumentException("Has to be a value of 1 or greater", nameof(size));

_cacheSize = size.Value * 1024 * 1024;

AddChromiumArgument("--disk-cache-size", (size.Value * 1024 * 1024).ToString());
}

Expand Down Expand Up @@ -1295,11 +1321,9 @@ private async Task ConvertAsync(

if (ImageResize || ImageRotate || SanitizeHtml || pageSettings.PaperFormat == PaperFormat.FitPageToContent)
{
using var documentHelper = new DocumentHelper(GetTempDirectory, WebProxy, _useCache, ImageLoadTimeout, _logger) { InstanceId = InstanceId };

if (SanitizeHtml)
{
var result = await documentHelper.SanitizeHtmlAsync(inputUri, Sanitizer, safeUrls, cancellationToken);
var result = await GetDocumentHelper.SanitizeHtmlAsync(inputUri, Sanitizer, safeUrls, cancellationToken);
if (result.Success)
{
inputUri = result.OutputUri;
Expand All @@ -1315,7 +1339,7 @@ private async Task ConvertAsync(
if (pageSettings.PaperFormat == PaperFormat.FitPageToContent)
{
WriteToLog("The paper format 'FitPageToContent' is set, modifying html so that the PDF fits the HTML content");
var result = await documentHelper.FitPageToContentAsync(inputUri, cancellationToken);
var result = await GetDocumentHelper.FitPageToContentAsync(inputUri, cancellationToken);
if (result.Success)
{
inputUri = result.OutputUri;
Expand All @@ -1325,7 +1349,7 @@ private async Task ConvertAsync(

if (ImageResize || ImageRotate)
{
var result = await documentHelper.ValidateImagesAsync(
var result = await GetDocumentHelper.ValidateImagesAsync(
inputUri,
ImageResize,
ImageRotate,
Expand Down Expand Up @@ -2577,6 +2601,8 @@ private async Task InternalDisposeAsync()
if (_disposed)
return;

_documentHelper?.Dispose();
_documentHelper = null;
_chromiumWaitEvent.Dispose();
_chromiumWaitEvent = null;

Expand Down
40 changes: 21 additions & 19 deletions ChromiumHtmlToPdfLib/Helpers/DocumentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ internal class DocumentHelper : IDisposable
/// <summary>
/// The cache folder
/// </summary>
readonly DirectoryInfo _cacheFolder;
readonly DirectoryInfo _cacheDirectory;

/// <summary>
/// The cache size
/// </summary>
private readonly long _cacheSize;
private readonly long? _cacheSize;
#endregion

#region Properties
Expand Down Expand Up @@ -140,30 +140,32 @@ internal int TimeLeft
/// Makes this object and sets its needed properties
/// </summary>
/// <param name="tempDirectory">When set then this directory will be used for temporary files</param>
/// <param name="webProxy">The web proxy to use when downloading</param>
/// <param name="useCache">When <c>true</c> then caching is enabled on the <see cref="WebClient" /></param>
/// <param name="imageLoadTimeout">
/// When set then this timeout is used for loading images, <c>null</c> when no timeout is
/// needed
/// </param>
/// <param name="logger">
/// When set then logging is written to this ILogger instance for all conversions at the Information
/// log level
/// </param>
/// <param name="cacheDirectory">The cache directory when <paramref name="useCache"/> is set to <c>true</c>, otherwise <c>null</c></param>
/// <param name="cacheSize">The cache size when <paramref name="useCache"/> is set to <c>true</c>, otherwise <c>null</c></param>
/// <param name="webProxy">The web proxy to use when downloading</param>
/// <param name="imageLoadTimeout">When set then this timeout is used for loading images, <c>null</c> when no timeout is needed</param>
/// <param name="logger">When set then logging is written to this ILogger instance for all conversions at the Information log level</param>
public DocumentHelper(DirectoryInfo tempDirectory,
WebProxy webProxy,
bool useCache,
DirectoryInfo cacheDirectory,
long? cacheSize,
WebProxy webProxy,
int? imageLoadTimeout,
ILogger logger,
DirectoryInfo cacheFolder,
long cacheSize)
ILogger logger)
{
_tempDirectory = tempDirectory;
_webProxy = webProxy;
_useCache = useCache;

if (useCache)
{
_cacheDirectory = cacheDirectory;
WriteToLog($"Setting cache directory to '{_cacheDirectory.FullName}' with a size of {cacheSize}");
_cacheSize = cacheSize;
}

_webProxy = webProxy;
_logger = logger;
_cacheFolder = cacheFolder;
_cacheSize = cacheSize;

if (!imageLoadTimeout.HasValue) return;
_imageLoadTimeout = imageLoadTimeout.Value;
Expand Down Expand Up @@ -919,7 +921,7 @@ private async Task<Stream> OpenDownloadStream(Uri sourceUri, bool checkTimeout =
if (_webProxy != null)
httpClientHandler.Proxy = _webProxy;

var handler = new FileCacheHandler(httpClientHandler);
var handler = new FileCacheHandler(httpClientHandler, _cacheDirectory, _cacheSize.Value);
using var client = new HttpClient(handler);
var timeLeft = TimeLeft;

Expand Down

0 comments on commit 47cfd4b

Please sign in to comment.