From 340280edf29cfbf6b519b7fb6312f90e2c1f9871 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Wed, 27 Feb 2013 07:49:55 -0500 Subject: Partial implementation of PackageManager --- .../Updates/PackageManager.cs | 47 ++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 MediaBrowser.Common.Implementations/Updates/PackageManager.cs (limited to 'MediaBrowser.Common.Implementations/Updates') diff --git a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs new file mode 100644 index 0000000000..3f09615e76 --- /dev/null +++ b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +using System.Threading.Tasks; +using MediaBrowser.Common.Kernel; +using MediaBrowser.Common.Net; +using MediaBrowser.Common.Security; +using MediaBrowser.Common.Updates; +using MediaBrowser.Model.Serialization; +using MediaBrowser.Model.Updates; + +namespace MediaBrowser.Common.Implementations.Updates +{ + public class PackageManager : IPackageManager + { + public async Task> GetAvailablePackages(IHttpClient client, + INetworkManager networkManager, + ISecurityManager securityManager, + ResourcePool resourcePool, + IJsonSerializer serializer, + CancellationToken cancellationToken) + { + var data = new Dictionary { { "key", securityManager.SupporterKey }, { "mac", networkManager.GetMacAddress() } }; + + using (var json = await client.Post(Constants.Constants.MBAdminUrl + "service/package/retrieveall", data, resourcePool.Mb, cancellationToken).ConfigureAwait(false)) + { + cancellationToken.ThrowIfCancellationRequested(); + + var packages = serializer.DeserializeFromStream>(json).ToList(); + foreach (var package in packages) + { + package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl)) + .OrderByDescending(v => v.version).ToList(); + } + + return packages; + } + + } + + public Task InstallPackage(PackageVersionInfo package, CancellationToken cancellationToken) + { + throw new NotImplementedException(); + } + } +} -- cgit v1.2.3 From e15ff541c6cd3c26848b68708a3e51c7d6b512e6 Mon Sep 17 00:00:00 2001 From: Eric Reed Date: Wed, 27 Feb 2013 08:34:24 -0500 Subject: Move actual installation to PackageManager --- .../Updates/PackageManager.cs | 61 ++++++++++++++++- MediaBrowser.Common/Updates/IPackageManager.cs | 17 ++++- .../Updates/InstallationManager.cs | 78 +++++----------------- 3 files changed, 91 insertions(+), 65 deletions(-) (limited to 'MediaBrowser.Common.Implementations/Updates') diff --git a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs index 3f09615e76..a9335dce0e 100644 --- a/MediaBrowser.Common.Implementations/Updates/PackageManager.cs +++ b/MediaBrowser.Common.Implementations/Updates/PackageManager.cs @@ -1,12 +1,16 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; +using System.Security.Cryptography; using System.Threading; using System.Threading.Tasks; using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Common.Security; using MediaBrowser.Common.Updates; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Updates; @@ -39,9 +43,62 @@ namespace MediaBrowser.Common.Implementations.Updates } - public Task InstallPackage(PackageVersionInfo package, CancellationToken cancellationToken) + public async Task InstallPackage(IHttpClient client, ILogger logger, ResourcePool resourcePool, IProgress progress, IZipClient zipClient, IApplicationPaths appPaths, PackageVersionInfo package, CancellationToken cancellationToken) { - throw new NotImplementedException(); + // Target based on if it is an archive or single assembly + // zip archives are assumed to contain directory structures relative to our ProgramDataPath + var isArchive = string.Equals(Path.GetExtension(package.sourceUrl), ".zip", StringComparison.OrdinalIgnoreCase); + var target = isArchive ? appPaths.ProgramDataPath : Path.Combine(appPaths.PluginsPath, package.targetFilename); + + // Download to temporary file so that, if interrupted, it won't destroy the existing installation + var tempFile = await client.GetTempFile(package.sourceUrl, resourcePool.Mb, cancellationToken, progress).ConfigureAwait(false); + + cancellationToken.ThrowIfCancellationRequested(); + + // Validate with a checksum + if (package.checksum != Guid.Empty) // support for legacy uploads for now + { + using (var crypto = new MD5CryptoServiceProvider()) + using (var stream = new BufferedStream(File.OpenRead(tempFile), 100000)) + { + var check = Guid.Parse(BitConverter.ToString(crypto.ComputeHash(stream)).Replace("-", String.Empty)); + if (check != package.checksum) + { + throw new ApplicationException(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name)); + } + } + } + + cancellationToken.ThrowIfCancellationRequested(); + + // Success - move it to the real target based on type + if (isArchive) + { + try + { + zipClient.ExtractAll(tempFile, target, true); + } + catch (IOException e) + { + logger.ErrorException("Error attempting to extract archive from {0} to {1}", e, tempFile, target); + throw; + } + + } + else + { + try + { + File.Copy(tempFile, target, true); + File.Delete(tempFile); + } + catch (IOException e) + { + logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target); + throw; + } + } + } } } diff --git a/MediaBrowser.Common/Updates/IPackageManager.cs b/MediaBrowser.Common/Updates/IPackageManager.cs index d5a8a59b12..f3ca1d8cf2 100644 --- a/MediaBrowser.Common/Updates/IPackageManager.cs +++ b/MediaBrowser.Common/Updates/IPackageManager.cs @@ -7,6 +7,8 @@ using System.Threading.Tasks; using MediaBrowser.Common.Kernel; using MediaBrowser.Common.Net; using MediaBrowser.Common.Security; +using MediaBrowser.Model.IO; +using MediaBrowser.Model.Logging; using MediaBrowser.Model.Serialization; using MediaBrowser.Model.Updates; @@ -34,9 +36,22 @@ namespace MediaBrowser.Common.Updates /// /// Installs a package. /// + /// + /// + /// + /// + /// + /// /// The package. /// The cancellation token. /// Task. - Task InstallPackage(PackageVersionInfo package, CancellationToken cancellationToken); + Task InstallPackage(IHttpClient client, + ILogger logger, + ResourcePool resourcePool, + IProgress progress, + IZipClient zipClient, + IApplicationPaths appPaths, + PackageVersionInfo package, + CancellationToken cancellationToken); } } diff --git a/MediaBrowser.Controller/Updates/InstallationManager.cs b/MediaBrowser.Controller/Updates/InstallationManager.cs index ebd644a822..7765b8aeff 100644 --- a/MediaBrowser.Controller/Updates/InstallationManager.cs +++ b/MediaBrowser.Controller/Updates/InstallationManager.cs @@ -429,77 +429,31 @@ namespace MediaBrowser.Controller.Updates /// Task. private async Task InstallPackageInternal(PackageVersionInfo package, IProgress progress, CancellationToken cancellationToken) { - // Target based on if it is an archive or single assembly - // zip archives are assumed to contain directory structures relative to our ProgramDataPath - var isArchive = string.Equals(Path.GetExtension(package.sourceUrl), ".zip", StringComparison.OrdinalIgnoreCase); - var target = isArchive ? Kernel.ApplicationPaths.ProgramDataPath : Path.Combine(Kernel.ApplicationPaths.PluginsPath, package.targetFilename); + // Do the install + await _packageManager.InstallPackage(HttpClient, _logger, Kernel.ResourcePools, progress, ZipClient, Kernel.ApplicationPaths, package, cancellationToken).ConfigureAwait(false); - // Download to temporary file so that, if interrupted, it won't destroy the existing installation - var tempFile = await HttpClient.GetTempFile(package.sourceUrl, Kernel.ResourcePools.Mb, cancellationToken, progress).ConfigureAwait(false); - - cancellationToken.ThrowIfCancellationRequested(); - - // Validate with a checksum - if (package.checksum != Guid.Empty) // support for legacy uploads for now + // Do plugin-specific processing + if (!(Path.GetExtension(package.targetFilename) ?? "").Equals(".zip", StringComparison.OrdinalIgnoreCase)) { - using (var crypto = new MD5CryptoServiceProvider()) - using (var stream = new BufferedStream(File.OpenRead(tempFile), 100000)) + // Set last update time if we were installed before + var plugin = Kernel.Plugins.FirstOrDefault(p => p.Name.Equals(package.name, StringComparison.OrdinalIgnoreCase)); + + if (plugin != null) { - var check = Guid.Parse(BitConverter.ToString(crypto.ComputeHash(stream)).Replace("-", String.Empty)); - if (check != package.checksum) + // Synchronize the UpdateClass value + if (plugin.Configuration.UpdateClass != package.classification) { - throw new ApplicationException(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name)); + plugin.Configuration.UpdateClass = package.classification; + plugin.SaveConfiguration(); } - } - } - - cancellationToken.ThrowIfCancellationRequested(); - - // Success - move it to the real target based on type - if (isArchive) - { - try - { - ZipClient.ExtractAll(tempFile, target, true); - } - catch (IOException e) - { - _logger.ErrorException("Error attempting to extract archive from {0} to {1}", e, tempFile, target); - throw; - } - } - else - { - try - { - File.Copy(tempFile, target, true); - File.Delete(tempFile); + OnPluginUpdated(plugin, package); } - catch (IOException e) - { - _logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target); - throw; - } - } - - // Set last update time if we were installed before - var plugin = Kernel.Plugins.FirstOrDefault(p => p.Name.Equals(package.name, StringComparison.OrdinalIgnoreCase)); - - if (plugin != null) - { - // Synchronize the UpdateClass value - if (plugin.Configuration.UpdateClass != package.classification) + else { - plugin.Configuration.UpdateClass = package.classification; - plugin.SaveConfiguration(); + OnPluginInstalled(package); } - - OnPluginUpdated(plugin, package); - } - else - { - OnPluginInstalled(package); + } } -- cgit v1.2.3