From 64cc5889f2be0dae6b0425c569bd84f996a0e435 Mon Sep 17 00:00:00 2001 From: dkanada Date: Tue, 23 Feb 2021 23:11:17 +0900 Subject: [PATCH] add suggested changes --- .../Plugins/PluginManager.cs | 45 ++++++++++++++++--- .../Updates/InstallationManager.cs | 4 +- MediaBrowser.Common/Plugins/IPluginManager.cs | 2 +- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/Emby.Server.Implementations/Plugins/PluginManager.cs b/Emby.Server.Implementations/Plugins/PluginManager.cs index fec7bcb87e..b4ab551579 100644 --- a/Emby.Server.Implementations/Plugins/PluginManager.cs +++ b/Emby.Server.Implementations/Plugins/PluginManager.cs @@ -4,14 +4,16 @@ using System; using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; +using System.Net.Http; using System.Reflection; using System.Text; using System.Text.Json; +using System.Threading.Tasks; using MediaBrowser.Common; using MediaBrowser.Common.Extensions; using MediaBrowser.Common.Json; using MediaBrowser.Common.Json.Converters; +using MediaBrowser.Common.Net; using MediaBrowser.Common.Plugins; using MediaBrowser.Model.Configuration; using MediaBrowser.Model.Plugins; @@ -35,6 +37,21 @@ namespace Emby.Server.Implementations.Plugins private readonly IList _plugins; private readonly Version _minimumVersion; + private IHttpClientFactory? _httpClientFactory; + + private IHttpClientFactory HttpClientFactory + { + get + { + if (_httpClientFactory == null) + { + _httpClientFactory = _appHost.Resolve(); + } + + return _httpClientFactory; + } + } + /// /// Initializes a new instance of the class. /// @@ -351,15 +368,29 @@ namespace Emby.Server.Implementations.Plugins } /// - public bool GenerateManifest(PackageInfo packageInfo, Version version, string path) + public async Task GenerateManifest(PackageInfo packageInfo, Version version, string path) { + if (packageInfo == null) + { + return false; + } + var versionInfo = packageInfo.Versions.First(v => v.Version == version.ToString()); - var url = packageInfo.ImageUrl ?? string.Empty; - var imageFilename = url.Substring(url.LastIndexOf('/') + 1, url.Length); + var imagePath = string.Empty; - using (var client = new WebClient()) + if (!string.IsNullOrEmpty(packageInfo.ImageUrl)) { - client.DownloadFile(url, Path.Join(path, imageFilename)); + var url = new Uri(packageInfo.ImageUrl); + imagePath = Path.Join(path, url.Segments.Last()); + + await using var fileStream = File.OpenWrite(imagePath); + var downloadStream = await HttpClientFactory + .CreateClient(NamedClient.Default) + .GetStreamAsync(url) + .ConfigureAwait(false); + + await downloadStream.CopyToAsync(fileStream).ConfigureAwait(false); + await fileStream.DisposeAsync(); } var manifest = new PluginManifest @@ -376,7 +407,7 @@ namespace Emby.Server.Implementations.Plugins Version = versionInfo.Version, Status = PluginStatus.Active, AutoUpdate = true, - ImagePath = Path.Join(path, imageFilename) + ImagePath = imagePath }; return SaveManifest(manifest, path); diff --git a/Emby.Server.Implementations/Updates/InstallationManager.cs b/Emby.Server.Implementations/Updates/InstallationManager.cs index 534c0aa4b8..7af52ea652 100644 --- a/Emby.Server.Implementations/Updates/InstallationManager.cs +++ b/Emby.Server.Implementations/Updates/InstallationManager.cs @@ -194,7 +194,7 @@ namespace Emby.Server.Implementations.Updates var plugin = _pluginManager.GetPlugin(packageGuid, version.VersionNumber); if (plugin != null) { - _pluginManager.GenerateManifest(package, version.VersionNumber, plugin.Path); + await _pluginManager.GenerateManifest(package, version.VersionNumber, plugin.Path); } // Remove versions with a target ABI greater then the current application version. @@ -567,7 +567,7 @@ namespace Emby.Server.Implementations.Updates stream.Position = 0; _zipClient.ExtractAllFromZip(stream, targetDir, true); - _pluginManager.GenerateManifest(package.PackageInfo, package.Version, targetDir); + await _pluginManager.GenerateManifest(package.PackageInfo, package.Version, targetDir); _pluginManager.ImportPluginFrom(targetDir); } diff --git a/MediaBrowser.Common/Plugins/IPluginManager.cs b/MediaBrowser.Common/Plugins/IPluginManager.cs index 6f0a0fbfc8..fc2fcb5179 100644 --- a/MediaBrowser.Common/Plugins/IPluginManager.cs +++ b/MediaBrowser.Common/Plugins/IPluginManager.cs @@ -52,7 +52,7 @@ namespace MediaBrowser.Common.Plugins /// Version to be installed. /// The path where to save the manifest. /// True if successful. - bool GenerateManifest(PackageInfo packageInfo, Version version, string path); + Task GenerateManifest(PackageInfo packageInfo, Version version, string path); /// /// Imports plugin details from a folder.