migrate to IHttpClientFactory in InstallationManager

pull/4030/head
crobibero 4 years ago
parent 8215f15c45
commit 533b981668

@ -34,7 +34,7 @@ namespace Emby.Server.Implementations.Updates
/// </summary> /// </summary>
private readonly ILogger<InstallationManager> _logger; private readonly ILogger<InstallationManager> _logger;
private readonly IApplicationPaths _appPaths; private readonly IApplicationPaths _appPaths;
private readonly IHttpClient _httpClient; private readonly IHttpClientFactory _httpClientFactory;
private readonly IJsonSerializer _jsonSerializer; private readonly IJsonSerializer _jsonSerializer;
private readonly IServerConfigurationManager _config; private readonly IServerConfigurationManager _config;
private readonly IFileSystem _fileSystem; private readonly IFileSystem _fileSystem;
@ -63,7 +63,7 @@ namespace Emby.Server.Implementations.Updates
ILogger<InstallationManager> logger, ILogger<InstallationManager> logger,
IApplicationHost appHost, IApplicationHost appHost,
IApplicationPaths appPaths, IApplicationPaths appPaths,
IHttpClient httpClient, IHttpClientFactory httpClientFactory,
IJsonSerializer jsonSerializer, IJsonSerializer jsonSerializer,
IServerConfigurationManager config, IServerConfigurationManager config,
IFileSystem fileSystem, IFileSystem fileSystem,
@ -80,7 +80,7 @@ namespace Emby.Server.Implementations.Updates
_logger = logger; _logger = logger;
_applicationHost = appHost; _applicationHost = appHost;
_appPaths = appPaths; _appPaths = appPaths;
_httpClient = httpClient; _httpClientFactory = httpClientFactory;
_jsonSerializer = jsonSerializer; _jsonSerializer = jsonSerializer;
_config = config; _config = config;
_fileSystem = fileSystem; _fileSystem = fileSystem;
@ -116,26 +116,18 @@ namespace Emby.Server.Implementations.Updates
{ {
try try
{ {
using (var response = await _httpClient.SendAsync( using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
new HttpRequestOptions .GetAsync(manifest, cancellationToken).ConfigureAwait(false);
{ await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
Url = manifest,
CancellationToken = cancellationToken, try
CacheMode = CacheMode.Unconditional,
CacheLength = TimeSpan.FromMinutes(3)
},
HttpMethod.Get).ConfigureAwait(false))
using (Stream stream = response.Content)
{ {
try return await _jsonSerializer.DeserializeFromStreamAsync<IReadOnlyList<PackageInfo>>(stream).ConfigureAwait(false);
{ }
return await _jsonSerializer.DeserializeFromStreamAsync<IReadOnlyList<PackageInfo>>(stream).ConfigureAwait(false); catch (SerializationException ex)
} {
catch (SerializationException ex) _logger.LogError(ex, "Failed to deserialize the plugin manifest retrieved from {Manifest}", manifest);
{ return Array.Empty<PackageInfo>();
_logger.LogError(ex, "Failed to deserialize the plugin manifest retrieved from {Manifest}", manifest);
return Array.Empty<PackageInfo>();
}
} }
} }
catch (UriFormatException ex) catch (UriFormatException ex)
@ -360,42 +352,34 @@ namespace Emby.Server.Implementations.Updates
// Always override the passed-in target (which is a file) and figure it out again // Always override the passed-in target (which is a file) and figure it out again
string targetDir = Path.Combine(_appPaths.PluginsPath, package.Name); string targetDir = Path.Combine(_appPaths.PluginsPath, package.Name);
using var response = await _httpClientFactory.CreateClient(NamedClient.Default)
.GetAsync(package.SourceUrl, cancellationToken).ConfigureAwait(false);
await using var stream = await response.Content.ReadAsStreamAsync().ConfigureAwait(false);
// CA5351: Do Not Use Broken Cryptographic Algorithms // CA5351: Do Not Use Broken Cryptographic Algorithms
#pragma warning disable CA5351 #pragma warning disable CA5351
using (var res = await _httpClient.SendAsync( using var md5 = MD5.Create();
new HttpRequestOptions cancellationToken.ThrowIfCancellationRequested();
{
Url = package.SourceUrl,
CancellationToken = cancellationToken,
// We need it to be buffered for setting the position
BufferContent = true
},
HttpMethod.Get).ConfigureAwait(false))
using (var stream = res.Content)
using (var md5 = MD5.Create())
{
cancellationToken.ThrowIfCancellationRequested();
var hash = Hex.Encode(md5.ComputeHash(stream)); var hash = Hex.Encode(md5.ComputeHash(stream));
if (!string.Equals(package.Checksum, hash, StringComparison.OrdinalIgnoreCase)) if (!string.Equals(package.Checksum, hash, StringComparison.OrdinalIgnoreCase))
{ {
_logger.LogError( _logger.LogError(
"The checksums didn't match while installing {Package}, expected: {Expected}, got: {Received}", "The checksums didn't match while installing {Package}, expected: {Expected}, got: {Received}",
package.Name, package.Name,
package.Checksum, package.Checksum,
hash); hash);
throw new InvalidDataException("The checksum of the received data doesn't match."); throw new InvalidDataException("The checksum of the received data doesn't match.");
} }
if (Directory.Exists(targetDir))
{
Directory.Delete(targetDir, true);
}
stream.Position = 0; if (Directory.Exists(targetDir))
_zipClient.ExtractAllFromZip(stream, targetDir, true); {
Directory.Delete(targetDir, true);
} }
stream.Position = 0;
_zipClient.ExtractAllFromZip(stream, targetDir, true);
#pragma warning restore CA5351 #pragma warning restore CA5351
} }

Loading…
Cancel
Save