PackageManager.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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, 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.targetFilename), ".zip", StringComparison.OrdinalIgnoreCase);
  45. var target = Path.Combine(isArchive ? appPaths.TempUpdatePath : 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
  64. try
  65. {
  66. File.Copy(tempFile, target, true);
  67. File.Delete(tempFile);
  68. }
  69. catch (IOException e)
  70. {
  71. logger.ErrorException("Error attempting to move file from {0} to {1}", e, tempFile, target);
  72. throw;
  73. }
  74. }
  75. }
  76. }