PackageManager.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Security.Cryptography;
  6. using System.Threading;
  7. using System.Threading.Tasks;
  8. using MediaBrowser.Common.Kernel;
  9. using MediaBrowser.Common.Net;
  10. using MediaBrowser.Common.Security;
  11. using MediaBrowser.Common.Updates;
  12. using MediaBrowser.Model.IO;
  13. using MediaBrowser.Model.Logging;
  14. using MediaBrowser.Model.Serialization;
  15. using MediaBrowser.Model.Updates;
  16. namespace MediaBrowser.Common.Implementations.Updates
  17. {
  18. public class PackageManager : IPackageManager
  19. {
  20. public async Task<IEnumerable<PackageInfo>> GetAvailablePackages(IHttpClient client,
  21. INetworkManager networkManager,
  22. ISecurityManager securityManager,
  23. ResourcePool resourcePool,
  24. IJsonSerializer serializer,
  25. CancellationToken cancellationToken)
  26. {
  27. var data = new Dictionary<string, string> { { "key", securityManager.SupporterKey }, { "mac", networkManager.GetMacAddress() } };
  28. using (var json = await client.Post(Constants.Constants.MBAdminUrl + "service/package/retrieveall", data, resourcePool.Mb, cancellationToken).ConfigureAwait(false))
  29. {
  30. cancellationToken.ThrowIfCancellationRequested();
  31. var packages = serializer.DeserializeFromStream<List<PackageInfo>>(json).ToList();
  32. foreach (var package in packages)
  33. {
  34. package.versions = package.versions.Where(v => !string.IsNullOrWhiteSpace(v.sourceUrl))
  35. .OrderByDescending(v => v.version).ToList();
  36. }
  37. return packages;
  38. }
  39. }
  40. public async Task InstallPackage(IHttpClient client, ILogger logger, ResourcePool resourcePool, IProgress<double> progress, IZipClient zipClient, IApplicationPaths appPaths, PackageVersionInfo package, CancellationToken cancellationToken)
  41. {
  42. // Target based on if it is an archive or single assembly
  43. // zip archives are assumed to contain directory structures relative to our ProgramDataPath
  44. var isArchive = string.Equals(Path.GetExtension(package.sourceUrl), ".zip", StringComparison.OrdinalIgnoreCase);
  45. var target = isArchive ? appPaths.ProgramDataPath : Path.Combine(appPaths.PluginsPath, package.targetFilename);
  46. // Download to temporary file so that, if interrupted, it won't destroy the existing installation
  47. var tempFile = await client.GetTempFile(package.sourceUrl, resourcePool.Mb, cancellationToken, progress).ConfigureAwait(false);
  48. cancellationToken.ThrowIfCancellationRequested();
  49. // Validate with a checksum
  50. if (package.checksum != Guid.Empty) // support for legacy uploads for now
  51. {
  52. using (var crypto = new MD5CryptoServiceProvider())
  53. using (var stream = new BufferedStream(File.OpenRead(tempFile), 100000))
  54. {
  55. var check = Guid.Parse(BitConverter.ToString(crypto.ComputeHash(stream)).Replace("-", String.Empty));
  56. if (check != package.checksum)
  57. {
  58. throw new ApplicationException(string.Format("Download validation failed for {0}. Probably corrupted during transfer.", package.name));
  59. }
  60. }
  61. }
  62. cancellationToken.ThrowIfCancellationRequested();
  63. // Success - move it to the real target based on type
  64. if (isArchive)
  65. {
  66. try
  67. {
  68. zipClient.ExtractAll(tempFile, target, true);
  69. }
  70. catch (IOException e)
  71. {
  72. logger.ErrorException("Error attempting to extract archive from {0} to {1}", e, tempFile, target);
  73. throw;
  74. }
  75. }
  76. else
  77. {
  78. try
  79. {
  80. File.Copy(tempFile, target, true);
  81. File.Delete(tempFile);
  82. }
  83. catch (IOException e)
  84. {
  85. logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target);
  86. throw;
  87. }
  88. }
  89. }
  90. }
  91. }