PluginUpdater.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Common.Logging;
  3. using MediaBrowser.Model.Net;
  4. using MediaBrowser.Model.Plugins;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Threading.Tasks;
  11. namespace MediaBrowser.UI.Controller
  12. {
  13. /// <summary>
  14. /// This keeps ui plugin assemblies in sync with plugins installed on the server
  15. /// </summary>
  16. public class PluginUpdater
  17. {
  18. /// <summary>
  19. /// Updates the plugins.
  20. /// </summary>
  21. /// <returns>Task{PluginUpdateResult}.</returns>
  22. public async Task<PluginUpdateResult> UpdatePlugins()
  23. {
  24. Logger.LogInfo("Downloading list of installed plugins");
  25. var allInstalledPlugins = await UIKernel.Instance.ApiClient.GetInstalledPluginsAsync().ConfigureAwait(false);
  26. var uiPlugins = allInstalledPlugins.Where(p => p.DownloadToUI).ToList();
  27. var result = new PluginUpdateResult { };
  28. result.DeletedPlugins = DeleteUninstalledPlugins(uiPlugins);
  29. await DownloadPluginAssemblies(uiPlugins, result).ConfigureAwait(false);
  30. result.UpdatedConfigurations = await DownloadPluginConfigurations(uiPlugins).ConfigureAwait(false);
  31. return result;
  32. }
  33. /// <summary>
  34. /// Downloads plugin assemblies from the server, if they need to be installed or updated.
  35. /// </summary>
  36. /// <param name="uiPlugins">The UI plugins.</param>
  37. /// <param name="result">The result.</param>
  38. /// <returns>Task.</returns>
  39. private async Task DownloadPluginAssemblies(IEnumerable<PluginInfo> uiPlugins, PluginUpdateResult result)
  40. {
  41. var newlyInstalledPlugins = new List<PluginInfo>();
  42. var updatedPlugins = new List<PluginInfo>();
  43. // Loop through the list of plugins that are on the server
  44. foreach (var pluginInfo in uiPlugins)
  45. {
  46. // See if it is already installed in the UI
  47. var currentAssemblyPath = Path.Combine(UIKernel.Instance.ApplicationPaths.PluginsPath, pluginInfo.AssemblyFileName);
  48. var isPluginInstalled = File.Exists(currentAssemblyPath);
  49. // Download the plugin if it is not present, or if the current version is out of date
  50. bool downloadPlugin;
  51. if (!isPluginInstalled)
  52. {
  53. downloadPlugin = true;
  54. Logger.LogInfo("{0} is not installed and needs to be downloaded.", pluginInfo.Name);
  55. }
  56. else
  57. {
  58. var serverVersion = Version.Parse(pluginInfo.Version);
  59. var fileVersion = FileVersionInfo.GetVersionInfo(currentAssemblyPath).FileVersion ?? string.Empty;
  60. downloadPlugin = string.IsNullOrEmpty(fileVersion) || Version.Parse(fileVersion) < serverVersion;
  61. if (downloadPlugin)
  62. {
  63. Logger.LogInfo("{0} has an updated version on the server and needs to be downloaded. Server version: {1}, UI version: {2}", pluginInfo.Name, serverVersion, fileVersion);
  64. }
  65. }
  66. if (downloadPlugin)
  67. {
  68. if (UIKernel.Instance.ApplicationVersion < Version.Parse(pluginInfo.MinimumRequiredUIVersion))
  69. {
  70. Logger.LogWarning("Can't download new version of {0} because the application needs to be updated first.", pluginInfo.Name);
  71. continue;
  72. }
  73. try
  74. {
  75. await DownloadPlugin(pluginInfo).ConfigureAwait(false);
  76. if (isPluginInstalled)
  77. {
  78. updatedPlugins.Add(pluginInfo);
  79. }
  80. else
  81. {
  82. newlyInstalledPlugins.Add(pluginInfo);
  83. }
  84. }
  85. catch (HttpException ex)
  86. {
  87. Logger.LogException("Error downloading {0} configuration", ex, pluginInfo.Name);
  88. }
  89. catch (IOException ex)
  90. {
  91. Logger.LogException("Error saving plugin assembly for {0}", ex, pluginInfo.Name);
  92. }
  93. }
  94. }
  95. result.NewlyInstalledPlugins = newlyInstalledPlugins;
  96. result.UpdatedPlugins = updatedPlugins;
  97. }
  98. /// <summary>
  99. /// Downloads plugin configurations from the server.
  100. /// </summary>
  101. /// <param name="uiPlugins">The UI plugins.</param>
  102. /// <returns>Task{List{PluginInfo}}.</returns>
  103. private async Task<List<PluginInfo>> DownloadPluginConfigurations(IEnumerable<PluginInfo> uiPlugins)
  104. {
  105. var updatedPlugins = new List<PluginInfo>();
  106. // Loop through the list of plugins that are on the server
  107. foreach (var pluginInfo in uiPlugins
  108. .Where(p => UIKernel.Instance.ApplicationVersion >= Version.Parse(p.MinimumRequiredUIVersion)))
  109. {
  110. // See if it is already installed in the UI
  111. var path = Path.Combine(UIKernel.Instance.ApplicationPaths.PluginConfigurationsPath, pluginInfo.ConfigurationFileName);
  112. var download = false;
  113. if (!File.Exists(path))
  114. {
  115. download = true;
  116. Logger.LogInfo("{0} configuration was not found needs to be downloaded.", pluginInfo.Name);
  117. }
  118. else if (File.GetLastWriteTimeUtc(path) < pluginInfo.ConfigurationDateLastModified)
  119. {
  120. download = true;
  121. Logger.LogInfo("{0} has an updated configuration on the server and needs to be downloaded.", pluginInfo.Name);
  122. }
  123. if (download)
  124. {
  125. if (UIKernel.Instance.ApplicationVersion < Version.Parse(pluginInfo.MinimumRequiredUIVersion))
  126. {
  127. Logger.LogWarning("Can't download updated configuration of {0} because the application needs to be updated first.", pluginInfo.Name);
  128. continue;
  129. }
  130. try
  131. {
  132. await DownloadPluginConfiguration(pluginInfo, path).ConfigureAwait(false);
  133. updatedPlugins.Add(pluginInfo);
  134. }
  135. catch (HttpException ex)
  136. {
  137. Logger.LogException("Error downloading {0} configuration", ex, pluginInfo.Name);
  138. }
  139. catch (IOException ex)
  140. {
  141. Logger.LogException("Error saving plugin configuration to {0}", ex, path);
  142. }
  143. }
  144. }
  145. return updatedPlugins;
  146. }
  147. /// <summary>
  148. /// Downloads a plugin assembly from the server
  149. /// </summary>
  150. /// <param name="plugin">The plugin.</param>
  151. /// <returns>Task.</returns>
  152. private async Task DownloadPlugin(PluginInfo plugin)
  153. {
  154. Logger.LogInfo("Downloading {0} Plugin", plugin.Name);
  155. var path = Path.Combine(UIKernel.Instance.ApplicationPaths.PluginsPath, plugin.AssemblyFileName);
  156. // First download to a MemoryStream. This way if the download is cut off, we won't be left with a partial file
  157. using (var memoryStream = new MemoryStream())
  158. {
  159. var assemblyStream = await UIKernel.Instance.ApiClient.GetPluginAssemblyAsync(plugin).ConfigureAwait(false);
  160. await assemblyStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  161. memoryStream.Position = 0;
  162. using (var fileStream = new FileStream(path, FileMode.Create))
  163. {
  164. await memoryStream.CopyToAsync(fileStream).ConfigureAwait(false);
  165. }
  166. }
  167. }
  168. /// <summary>
  169. /// Downloads the latest configuration for a plugin
  170. /// </summary>
  171. /// <param name="pluginInfo">The plugin info.</param>
  172. /// <param name="path">The path.</param>
  173. /// <returns>Task.</returns>
  174. private async Task DownloadPluginConfiguration(PluginInfo pluginInfo, string path)
  175. {
  176. Logger.LogInfo("Downloading {0} Configuration", pluginInfo.Name);
  177. // First download to a MemoryStream. This way if the download is cut off, we won't be left with a partial file
  178. using (var stream = await UIKernel.Instance.ApiClient.GetPluginConfigurationFileAsync(pluginInfo.UniqueId).ConfigureAwait(false))
  179. {
  180. using (var memoryStream = new MemoryStream())
  181. {
  182. await stream.CopyToAsync(memoryStream).ConfigureAwait(false);
  183. memoryStream.Position = 0;
  184. using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read, StreamDefaults.DefaultFileStreamBufferSize, true))
  185. {
  186. await memoryStream.CopyToAsync(fs).ConfigureAwait(false);
  187. }
  188. }
  189. }
  190. File.SetLastWriteTimeUtc(path, pluginInfo.ConfigurationDateLastModified);
  191. }
  192. /// <summary>
  193. /// Deletes any plugins that have been uninstalled from the server
  194. /// </summary>
  195. /// <param name="uiPlugins">The UI plugins.</param>
  196. /// <returns>IEnumerable{System.String}.</returns>
  197. private IEnumerable<string> DeleteUninstalledPlugins(IEnumerable<PluginInfo> uiPlugins)
  198. {
  199. var deletedPlugins = new List<string>();
  200. foreach (var plugin in Directory.EnumerateFiles(UIKernel.Instance.ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  201. .Select(Path.GetFileName)
  202. .ToList())
  203. {
  204. var serverPlugin = uiPlugins.FirstOrDefault(p => p.AssemblyFileName.Equals(plugin, StringComparison.OrdinalIgnoreCase));
  205. if (serverPlugin == null)
  206. {
  207. try
  208. {
  209. DeletePlugin(plugin);
  210. deletedPlugins.Add(plugin);
  211. }
  212. catch (IOException ex)
  213. {
  214. Logger.LogException("Error deleting plugin assembly {0}", ex, plugin);
  215. }
  216. }
  217. }
  218. return deletedPlugins;
  219. }
  220. /// <summary>
  221. /// Deletes an installed ui plugin.
  222. /// Leaves config and data behind in the event it is later re-installed
  223. /// </summary>
  224. /// <param name="plugin">The plugin.</param>
  225. private void DeletePlugin(string plugin)
  226. {
  227. Logger.LogInfo("Deleting {0} Plugin", plugin);
  228. if (File.Exists(plugin))
  229. {
  230. File.Delete(plugin);
  231. }
  232. }
  233. }
  234. /// <summary>
  235. /// Class PluginUpdateResult
  236. /// </summary>
  237. public class PluginUpdateResult
  238. {
  239. /// <summary>
  240. /// Gets or sets the deleted plugins.
  241. /// </summary>
  242. /// <value>The deleted plugins.</value>
  243. public IEnumerable<string> DeletedPlugins { get; set; }
  244. /// <summary>
  245. /// Gets or sets the newly installed plugins.
  246. /// </summary>
  247. /// <value>The newly installed plugins.</value>
  248. public IEnumerable<PluginInfo> NewlyInstalledPlugins { get; set; }
  249. /// <summary>
  250. /// Gets or sets the updated plugins.
  251. /// </summary>
  252. /// <value>The updated plugins.</value>
  253. public IEnumerable<PluginInfo> UpdatedPlugins { get; set; }
  254. /// <summary>
  255. /// Gets or sets the updated configurations.
  256. /// </summary>
  257. /// <value>The updated configurations.</value>
  258. public IEnumerable<PluginInfo> UpdatedConfigurations { get; set; }
  259. }
  260. }