diff options
| author | Andrew Rabert <6550543+nvllsvm@users.noreply.github.com> | 2019-01-20 20:45:06 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-01-20 20:45:06 -0500 |
| commit | c7f648f86a543c889a92a15f9d8d1f95d28987c8 (patch) | |
| tree | cadac43ca42a70a126084e1508fba0b8c4f4fce3 /Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs | |
| parent | 7e4cc9f513ff583e7a8ecf596e61d33dd9ce41d9 (diff) | |
| parent | 803bf563d74754139ff92810364262e3181e399d (diff) | |
Merge branch 'dev' into imagesize
Diffstat (limited to 'Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs')
| -rw-r--r-- | Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs | 54 |
1 files changed, 22 insertions, 32 deletions
diff --git a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs index 3aab10026..ea620cb2e 100644 --- a/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs +++ b/Emby.Server.Implementations/HttpClientManager/HttpClientManager.cs @@ -44,18 +44,22 @@ namespace Emby.Server.Implementations.HttpClientManager /// <summary> /// Initializes a new instance of the <see cref="HttpClientManager" /> class. /// </summary> - public HttpClientManager(IApplicationPaths appPaths, ILogger logger, IFileSystem fileSystem, Func<string> defaultUserAgentFn) + public HttpClientManager( + IApplicationPaths appPaths, + ILoggerFactory loggerFactory, + IFileSystem fileSystem, + Func<string> defaultUserAgentFn) { if (appPaths == null) { throw new ArgumentNullException(nameof(appPaths)); } - if (logger == null) + if (loggerFactory == null) { - throw new ArgumentNullException(nameof(logger)); + throw new ArgumentNullException(nameof(loggerFactory)); } - _logger = logger; + _logger = loggerFactory.CreateLogger("HttpClient"); _fileSystem = fileSystem; _appPaths = appPaths; _defaultUserAgentFn = defaultUserAgentFn; @@ -264,7 +268,7 @@ namespace Emby.Server.Implementations.HttpClientManager var responseCachePath = Path.Combine(_appPaths.CachePath, "httpclient", urlHash); - var response = await GetCachedResponse(responseCachePath, options.CacheLength, url).ConfigureAwait(false); + var response = GetCachedResponse(responseCachePath, options.CacheLength, url); if (response != null) { return response; @@ -280,30 +284,24 @@ namespace Emby.Server.Implementations.HttpClientManager return response; } - private async Task<HttpResponseInfo> GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) + private HttpResponseInfo GetCachedResponse(string responseCachePath, TimeSpan cacheLength, string url) { try { if (_fileSystem.GetLastWriteTimeUtc(responseCachePath).Add(cacheLength) > DateTime.UtcNow) { - using (var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true)) - { - var memoryStream = new MemoryStream(); - - await stream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; + var stream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read, true); - return new HttpResponseInfo - { - ResponseUrl = url, - Content = memoryStream, - StatusCode = HttpStatusCode.OK, - ContentLength = memoryStream.Length - }; - } + return new HttpResponseInfo + { + ResponseUrl = url, + Content = stream, + StatusCode = HttpStatusCode.OK, + ContentLength = stream.Length + }; } } - catch (FileNotFoundException) + catch (FileNotFoundException) // REVIEW: @bond Is this really faster? { } @@ -319,19 +317,11 @@ namespace Emby.Server.Implementations.HttpClientManager { _fileSystem.CreateDirectory(_fileSystem.GetDirectoryName(responseCachePath)); - using (var responseStream = response.Content) + using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None, true)) { - var memoryStream = new MemoryStream(); - await responseStream.CopyToAsync(memoryStream).ConfigureAwait(false); - memoryStream.Position = 0; + await response.Content.CopyToAsync(fileStream).ConfigureAwait(false); - using (var fileStream = _fileSystem.GetFileStream(responseCachePath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.None, true)) - { - await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false); - - memoryStream.Position = 0; - response.Content = memoryStream; - } + response.Content.Position = 0; } } |
