-
-
Notifications
You must be signed in to change notification settings - Fork 85
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
Kees
committed
Oct 12, 2023
1 parent
7f5b494
commit 335a641
Showing
4 changed files
with
89 additions
and
27 deletions.
There are no files selected for viewing
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
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
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
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,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ChromiumHtmlToPdfLib.Helpers; | ||
|
||
internal class FileCacheHandler : HttpClientHandler | ||
{ | ||
HttpClientHandler _httpClientHandler; | ||
|
||
public FileCacheHandler(HttpClientHandler httpClientHandler, DirectoryInfo cacheFolder) | ||
{ | ||
_httpClientHandler = httpClientHandler; | ||
} | ||
|
||
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) | ||
{ | ||
var hash = GetMd5HashFromString(request.RequestUri.ToString()); | ||
|
||
if (File.Exists(hash)) | ||
{ | ||
var response = new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = new StreamContent(new FileStream(hash, FileMode.OpenOrCreate)), | ||
ReasonPhrase = "Loaded from cache" | ||
}; | ||
return Task.FromResult(response); | ||
} | ||
|
||
var response = new HttpResponseMessage(HttpStatusCode.OK) | ||
{ | ||
Content = new StreamContent(new FileStream(hash, FileMode.OpenOrCreate)), | ||
ReasonPhrase = "Loaded from cache" | ||
}; | ||
|
||
return Task.FromResult(response); | ||
|
||
//throw new NotImplementedException(); | ||
return base.SendAsync(request, cancellationToken); | ||
} | ||
|
||
#region GetMd5HashFromString | ||
/// <summary> | ||
/// Retourneert een MD5 hash voor de opgegeven <paramref name="value" /> | ||
/// </summary> | ||
/// <param name="value"></param> | ||
/// <returns></returns> | ||
public string GetMd5HashFromString(string value) | ||
{ | ||
value ??= string.Empty; | ||
using var md5 = MD5.Create(); | ||
var bytes = md5.ComputeHash(Encoding.UTF8.GetBytes(value)); | ||
return bytes.Aggregate(string.Empty, (current, b) => current + b.ToString("X2")); | ||
} | ||
#endregion | ||
} |