ApplicationHost.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647
  1. using MediaBrowser.Api;
  2. using MediaBrowser.Common;
  3. using MediaBrowser.Common.Configuration;
  4. using MediaBrowser.Common.Constants;
  5. using MediaBrowser.Common.Extensions;
  6. using MediaBrowser.Common.Implementations;
  7. using MediaBrowser.Common.Implementations.IO;
  8. using MediaBrowser.Common.Implementations.ScheduledTasks;
  9. using MediaBrowser.Common.Implementations.Updates;
  10. using MediaBrowser.Common.IO;
  11. using MediaBrowser.Common.MediaInfo;
  12. using MediaBrowser.Common.Net;
  13. using MediaBrowser.Controller;
  14. using MediaBrowser.Controller.Configuration;
  15. using MediaBrowser.Controller.Drawing;
  16. using MediaBrowser.Controller.Entities;
  17. using MediaBrowser.Controller.IO;
  18. using MediaBrowser.Controller.Library;
  19. using MediaBrowser.Controller.Localization;
  20. using MediaBrowser.Controller.MediaInfo;
  21. using MediaBrowser.Controller.Notifications;
  22. using MediaBrowser.Controller.Persistence;
  23. using MediaBrowser.Controller.Plugins;
  24. using MediaBrowser.Controller.Providers;
  25. using MediaBrowser.Controller.Resolvers;
  26. using MediaBrowser.Controller.Session;
  27. using MediaBrowser.Controller.Sorting;
  28. using MediaBrowser.IsoMounter;
  29. using MediaBrowser.Model.IO;
  30. using MediaBrowser.Model.MediaInfo;
  31. using MediaBrowser.Model.System;
  32. using MediaBrowser.Model.Updates;
  33. using MediaBrowser.Providers;
  34. using MediaBrowser.Server.Implementations;
  35. using MediaBrowser.Server.Implementations.BdInfo;
  36. using MediaBrowser.Server.Implementations.Configuration;
  37. using MediaBrowser.Server.Implementations.HttpServer;
  38. using MediaBrowser.Server.Implementations.IO;
  39. using MediaBrowser.Server.Implementations.Library;
  40. using MediaBrowser.Server.Implementations.Localization;
  41. using MediaBrowser.Server.Implementations.MediaEncoder;
  42. using MediaBrowser.Server.Implementations.Persistence;
  43. using MediaBrowser.Server.Implementations.Providers;
  44. using MediaBrowser.Server.Implementations.ServerManager;
  45. using MediaBrowser.Server.Implementations.Session;
  46. using MediaBrowser.Server.Implementations.WebSocket;
  47. using MediaBrowser.ServerApplication.Implementations;
  48. using MediaBrowser.WebDashboard.Api;
  49. using System;
  50. using System.Collections.Generic;
  51. using System.Data.SQLite;
  52. using System.Diagnostics;
  53. using System.IO;
  54. using System.Linq;
  55. using System.Reflection;
  56. using System.Threading;
  57. using System.Threading.Tasks;
  58. namespace MediaBrowser.ServerApplication
  59. {
  60. /// <summary>
  61. /// Class CompositionRoot
  62. /// </summary>
  63. public class ApplicationHost : BaseApplicationHost<ServerApplicationPaths>, IServerApplicationHost
  64. {
  65. internal const int UdpServerPort = 7359;
  66. /// <summary>
  67. /// Gets the server kernel.
  68. /// </summary>
  69. /// <value>The server kernel.</value>
  70. protected Kernel ServerKernel { get; set; }
  71. /// <summary>
  72. /// Gets the server configuration manager.
  73. /// </summary>
  74. /// <value>The server configuration manager.</value>
  75. public IServerConfigurationManager ServerConfigurationManager
  76. {
  77. get { return (IServerConfigurationManager)ConfigurationManager; }
  78. }
  79. /// <summary>
  80. /// Gets the name of the log file prefix.
  81. /// </summary>
  82. /// <value>The name of the log file prefix.</value>
  83. protected override string LogFilePrefixName
  84. {
  85. get { return "server"; }
  86. }
  87. /// <summary>
  88. /// Gets the name of the web application that can be used for url building.
  89. /// All api urls will be of the form {protocol}://{host}:{port}/{appname}/...
  90. /// </summary>
  91. /// <value>The name of the web application.</value>
  92. public string WebApplicationName
  93. {
  94. get { return "mediabrowser"; }
  95. }
  96. /// <summary>
  97. /// Gets the HTTP server URL prefix.
  98. /// </summary>
  99. /// <value>The HTTP server URL prefix.</value>
  100. public string HttpServerUrlPrefix
  101. {
  102. get
  103. {
  104. return "http://+:" + ServerConfigurationManager.Configuration.HttpServerPortNumber + "/" + WebApplicationName + "/";
  105. }
  106. }
  107. /// <summary>
  108. /// Gets the configuration manager.
  109. /// </summary>
  110. /// <returns>IConfigurationManager.</returns>
  111. protected override IConfigurationManager GetConfigurationManager()
  112. {
  113. return new ServerConfigurationManager(ApplicationPaths, LogManager, XmlSerializer);
  114. }
  115. /// <summary>
  116. /// Gets or sets the server manager.
  117. /// </summary>
  118. /// <value>The server manager.</value>
  119. private IServerManager ServerManager { get; set; }
  120. /// <summary>
  121. /// Gets or sets the user manager.
  122. /// </summary>
  123. /// <value>The user manager.</value>
  124. public IUserManager UserManager { get; set; }
  125. /// <summary>
  126. /// Gets or sets the library manager.
  127. /// </summary>
  128. /// <value>The library manager.</value>
  129. internal ILibraryManager LibraryManager { get; set; }
  130. /// <summary>
  131. /// Gets or sets the directory watchers.
  132. /// </summary>
  133. /// <value>The directory watchers.</value>
  134. private IDirectoryWatchers DirectoryWatchers { get; set; }
  135. /// <summary>
  136. /// Gets or sets the provider manager.
  137. /// </summary>
  138. /// <value>The provider manager.</value>
  139. private IProviderManager ProviderManager { get; set; }
  140. /// <summary>
  141. /// Gets or sets the zip client.
  142. /// </summary>
  143. /// <value>The zip client.</value>
  144. private IZipClient ZipClient { get; set; }
  145. /// <summary>
  146. /// Gets or sets the HTTP server.
  147. /// </summary>
  148. /// <value>The HTTP server.</value>
  149. private IHttpServer HttpServer { get; set; }
  150. /// <summary>
  151. /// Gets or sets the media encoder.
  152. /// </summary>
  153. /// <value>The media encoder.</value>
  154. private IMediaEncoder MediaEncoder { get; set; }
  155. private IIsoManager IsoManager { get; set; }
  156. private ILocalizationManager LocalizationManager { get; set; }
  157. /// <summary>
  158. /// Gets or sets the user data repository.
  159. /// </summary>
  160. /// <value>The user data repository.</value>
  161. private IUserDataRepository UserDataRepository { get; set; }
  162. private IUserRepository UserRepository { get; set; }
  163. internal IDisplayPreferencesRepository DisplayPreferencesRepository { get; set; }
  164. private IItemRepository ItemRepository { get; set; }
  165. private INotificationsRepository NotificationsRepository { get; set; }
  166. /// <summary>
  167. /// The full path to our startmenu shortcut
  168. /// </summary>
  169. protected override string ProductShortcutPath
  170. {
  171. get { return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu), "Media Browser 3", "Media Browser Server.lnk"); }
  172. }
  173. private Task<IHttpServer> _httpServerCreationTask;
  174. /// <summary>
  175. /// Runs the startup tasks.
  176. /// </summary>
  177. /// <returns>Task.</returns>
  178. public override async Task RunStartupTasks()
  179. {
  180. await base.RunStartupTasks().ConfigureAwait(false);
  181. DirectoryWatchers.Start();
  182. Logger.Info("Core startup complete");
  183. Parallel.ForEach(GetExports<IServerEntryPoint>(), entryPoint =>
  184. {
  185. try
  186. {
  187. entryPoint.Run();
  188. }
  189. catch (Exception ex)
  190. {
  191. Logger.ErrorException("Error in {0}", ex, entryPoint.GetType().Name);
  192. }
  193. });
  194. }
  195. /// <summary>
  196. /// Called when [logger loaded].
  197. /// </summary>
  198. protected override void OnLoggerLoaded()
  199. {
  200. base.OnLoggerLoaded();
  201. _httpServerCreationTask = Task.Run(() => ServerFactory.CreateServer(this, LogManager, "Media Browser", "dashboard/index.html"));
  202. }
  203. /// <summary>
  204. /// Registers resources that classes will depend on
  205. /// </summary>
  206. /// <returns>Task.</returns>
  207. protected override async Task RegisterResources()
  208. {
  209. ServerKernel = new Kernel();
  210. await base.RegisterResources().ConfigureAwait(false);
  211. RegisterSingleInstance<IHttpResultFactory>(new HttpResultFactory(LogManager));
  212. RegisterSingleInstance<IServerApplicationHost>(this);
  213. RegisterSingleInstance<IServerApplicationPaths>(ApplicationPaths);
  214. RegisterSingleInstance(ServerKernel);
  215. RegisterSingleInstance(ServerConfigurationManager);
  216. RegisterSingleInstance<IWebSocketServer>(() => new AlchemyServer(Logger));
  217. IsoManager = new IsoManager();
  218. RegisterSingleInstance(IsoManager);
  219. RegisterSingleInstance<IBlurayExaminer>(() => new BdInfoExaminer());
  220. ZipClient = new DotNetZipClient();
  221. RegisterSingleInstance(ZipClient);
  222. UserDataRepository = new SqliteUserDataRepository(ApplicationPaths, JsonSerializer, LogManager);
  223. RegisterSingleInstance(UserDataRepository);
  224. UserRepository = await GetUserRepository().ConfigureAwait(false);
  225. RegisterSingleInstance(UserRepository);
  226. DisplayPreferencesRepository = new SqliteDisplayPreferencesRepository(ApplicationPaths, JsonSerializer, LogManager);
  227. RegisterSingleInstance(DisplayPreferencesRepository);
  228. ItemRepository = new SqliteItemRepository(ApplicationPaths, JsonSerializer, LogManager);
  229. RegisterSingleInstance(ItemRepository);
  230. UserManager = new UserManager(Logger, ServerConfigurationManager, UserRepository);
  231. RegisterSingleInstance(UserManager);
  232. LibraryManager = new LibraryManager(Logger, TaskManager, UserManager, ServerConfigurationManager, UserDataRepository, () => DirectoryWatchers);
  233. RegisterSingleInstance(LibraryManager);
  234. DirectoryWatchers = new DirectoryWatchers(LogManager, TaskManager, LibraryManager, ServerConfigurationManager);
  235. RegisterSingleInstance(DirectoryWatchers);
  236. ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, DirectoryWatchers, LogManager, LibraryManager);
  237. RegisterSingleInstance(ProviderManager);
  238. RegisterSingleInstance<ILibrarySearchEngine>(() => new LuceneSearchEngine(ApplicationPaths, LogManager, LibraryManager));
  239. MediaEncoder = new MediaEncoder(LogManager.GetLogger("MediaEncoder"), ZipClient, ApplicationPaths, JsonSerializer);
  240. RegisterSingleInstance(MediaEncoder);
  241. var clientConnectionManager = new SessionManager(UserDataRepository, ServerConfigurationManager, Logger, UserRepository);
  242. RegisterSingleInstance<ISessionManager>(clientConnectionManager);
  243. HttpServer = await _httpServerCreationTask.ConfigureAwait(false);
  244. RegisterSingleInstance(HttpServer, false);
  245. ServerManager = new ServerManager(this, JsonSerializer, Logger, ServerConfigurationManager);
  246. RegisterSingleInstance(ServerManager);
  247. LocalizationManager = new LocalizationManager(ServerConfigurationManager);
  248. RegisterSingleInstance(LocalizationManager);
  249. var displayPreferencesTask = Task.Run(async () => await ConfigureDisplayPreferencesRepositories().ConfigureAwait(false));
  250. var itemsTask = Task.Run(async () => await ConfigureItemRepositories().ConfigureAwait(false));
  251. var userdataTask = Task.Run(async () => await ConfigureUserDataRepositories().ConfigureAwait(false));
  252. await ConfigureNotificationsRepository().ConfigureAwait(false);
  253. await Task.WhenAll(itemsTask, displayPreferencesTask, userdataTask).ConfigureAwait(false);
  254. SetKernelProperties();
  255. }
  256. /// <summary>
  257. /// Sets the kernel properties.
  258. /// </summary>
  259. private void SetKernelProperties()
  260. {
  261. ServerKernel.ImageManager = new ImageManager(LogManager.GetLogger("ImageManager"),
  262. ApplicationPaths, ItemRepository);
  263. Parallel.Invoke(
  264. () => ServerKernel.FFMpegManager = new FFMpegManager(ApplicationPaths, MediaEncoder, LibraryManager, Logger, ItemRepository),
  265. () => ServerKernel.ImageManager.ImageEnhancers = GetExports<IImageEnhancer>().OrderBy(e => e.Priority).ToArray(),
  266. () => LocalizedStrings.StringFiles = GetExports<LocalizedStringData>(),
  267. SetStaticProperties
  268. );
  269. }
  270. private async Task<IUserRepository> GetUserRepository()
  271. {
  272. var dbFile = Path.Combine(ApplicationPaths.DataPath, "users.db");
  273. var connection = await ConnectToDb(dbFile).ConfigureAwait(false);
  274. return new SqliteUserRepository(connection, ApplicationPaths, JsonSerializer, LogManager);
  275. }
  276. /// <summary>
  277. /// Configures the repositories.
  278. /// </summary>
  279. /// <returns>Task.</returns>
  280. private async Task ConfigureNotificationsRepository()
  281. {
  282. var dbFile = Path.Combine(ApplicationPaths.DataPath, "notifications.db");
  283. var connection = await ConnectToDb(dbFile).ConfigureAwait(false);
  284. var repo = new SqliteNotificationsRepository(connection, LogManager);
  285. repo.Initialize();
  286. NotificationsRepository = repo;
  287. RegisterSingleInstance(NotificationsRepository);
  288. }
  289. /// <summary>
  290. /// Configures the repositories.
  291. /// </summary>
  292. /// <returns>Task.</returns>
  293. private async Task ConfigureDisplayPreferencesRepositories()
  294. {
  295. await DisplayPreferencesRepository.Initialize().ConfigureAwait(false);
  296. }
  297. /// <summary>
  298. /// Configures the item repositories.
  299. /// </summary>
  300. /// <returns>Task.</returns>
  301. private async Task ConfigureItemRepositories()
  302. {
  303. await ItemRepository.Initialize().ConfigureAwait(false);
  304. ((LibraryManager)LibraryManager).ItemRepository = ItemRepository;
  305. }
  306. /// <summary>
  307. /// Configures the user data repositories.
  308. /// </summary>
  309. /// <returns>Task.</returns>
  310. private Task ConfigureUserDataRepositories()
  311. {
  312. return UserDataRepository.Initialize();
  313. }
  314. /// <summary>
  315. /// Connects to db.
  316. /// </summary>
  317. /// <param name="dbPath">The db path.</param>
  318. /// <returns>Task{IDbConnection}.</returns>
  319. /// <exception cref="System.ArgumentNullException">dbPath</exception>
  320. private static async Task<SQLiteConnection> ConnectToDb(string dbPath)
  321. {
  322. if (string.IsNullOrEmpty(dbPath))
  323. {
  324. throw new ArgumentNullException("dbPath");
  325. }
  326. var connectionstr = new SQLiteConnectionStringBuilder
  327. {
  328. PageSize = 4096,
  329. CacheSize = 4096,
  330. SyncMode = SynchronizationModes.Normal,
  331. DataSource = dbPath,
  332. JournalMode = SQLiteJournalModeEnum.Wal
  333. };
  334. var connection = new SQLiteConnection(connectionstr.ConnectionString);
  335. await connection.OpenAsync().ConfigureAwait(false);
  336. return connection;
  337. }
  338. /// <summary>
  339. /// Dirty hacks
  340. /// </summary>
  341. private void SetStaticProperties()
  342. {
  343. // For now there's no real way to inject these properly
  344. BaseItem.Logger = LogManager.GetLogger("BaseItem");
  345. BaseItem.ConfigurationManager = ServerConfigurationManager;
  346. BaseItem.LibraryManager = LibraryManager;
  347. BaseItem.ProviderManager = ProviderManager;
  348. BaseItem.LocalizationManager = LocalizationManager;
  349. BaseItem.ItemRepository = ItemRepository;
  350. User.XmlSerializer = XmlSerializer;
  351. User.UserManager = UserManager;
  352. LocalizedStrings.ApplicationPaths = ApplicationPaths;
  353. }
  354. /// <summary>
  355. /// Finds the parts.
  356. /// </summary>
  357. protected override void FindParts()
  358. {
  359. if (IsFirstRun)
  360. {
  361. RegisterServerWithAdministratorAccess();
  362. }
  363. base.FindParts();
  364. HttpServer.Init(GetExports<IRestfulService>(false));
  365. ServerManager.AddWebSocketListeners(GetExports<IWebSocketListener>(false));
  366. StartServer(true);
  367. LibraryManager.AddParts(GetExports<IResolverIgnoreRule>(),
  368. GetExports<IVirtualFolderCreator>(),
  369. GetExports<IItemResolver>(),
  370. GetExports<IIntroProvider>(),
  371. GetExports<IBaseItemComparer>(),
  372. GetExports<ILibraryPrescanTask>(),
  373. GetExports<ILibraryPostScanTask>(),
  374. GetExports<IMetadataSaver>());
  375. ProviderManager.AddParts(GetExports<BaseMetadataProvider>().ToArray());
  376. IsoManager.AddParts(GetExports<IIsoMounter>().ToArray());
  377. }
  378. /// <summary>
  379. /// Starts the server.
  380. /// </summary>
  381. /// <param name="retryOnFailure">if set to <c>true</c> [retry on failure].</param>
  382. private void StartServer(bool retryOnFailure)
  383. {
  384. try
  385. {
  386. ServerManager.Start();
  387. }
  388. catch
  389. {
  390. if (retryOnFailure)
  391. {
  392. RegisterServerWithAdministratorAccess();
  393. StartServer(false);
  394. }
  395. else
  396. {
  397. throw;
  398. }
  399. }
  400. }
  401. /// <summary>
  402. /// Called when [configuration updated].
  403. /// </summary>
  404. /// <param name="sender">The sender.</param>
  405. /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
  406. protected override void OnConfigurationUpdated(object sender, EventArgs e)
  407. {
  408. base.OnConfigurationUpdated(sender, e);
  409. if (!string.Equals(HttpServer.UrlPrefix, HttpServerUrlPrefix, StringComparison.OrdinalIgnoreCase))
  410. {
  411. NotifyPendingRestart();
  412. }
  413. else if (!ServerManager.SupportsNativeWebSocket && ServerManager.WebSocketPortNumber != ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber)
  414. {
  415. NotifyPendingRestart();
  416. }
  417. }
  418. /// <summary>
  419. /// Restarts this instance.
  420. /// </summary>
  421. public override void Restart()
  422. {
  423. App.Instance.Restart();
  424. }
  425. /// <summary>
  426. /// Gets or sets a value indicating whether this instance can self update.
  427. /// </summary>
  428. /// <value><c>true</c> if this instance can self update; otherwise, <c>false</c>.</value>
  429. public override bool CanSelfUpdate
  430. {
  431. get
  432. {
  433. #if DEBUG
  434. return false;
  435. #endif
  436. return ConfigurationManager.CommonConfiguration.EnableAutoUpdate;
  437. }
  438. }
  439. /// <summary>
  440. /// Gets the composable part assemblies.
  441. /// </summary>
  442. /// <returns>IEnumerable{Assembly}.</returns>
  443. protected override IEnumerable<Assembly> GetComposablePartAssemblies()
  444. {
  445. // Gets all plugin assemblies by first reading all bytes of the .dll and calling Assembly.Load against that
  446. // This will prevent the .dll file from getting locked, and allow us to replace it when needed
  447. foreach (var pluginAssembly in Directory
  448. .EnumerateFiles(ApplicationPaths.PluginsPath, "*.dll", SearchOption.TopDirectoryOnly)
  449. .Select(LoadAssembly).Where(a => a != null))
  450. {
  451. yield return pluginAssembly;
  452. }
  453. // Include composable parts in the Api assembly
  454. yield return typeof(ApiEntryPoint).Assembly;
  455. // Include composable parts in the Dashboard assembly
  456. yield return typeof(DashboardInfo).Assembly;
  457. // Include composable parts in the Model assembly
  458. yield return typeof(SystemInfo).Assembly;
  459. // Include composable parts in the Common assembly
  460. yield return typeof(IApplicationHost).Assembly;
  461. // Include composable parts in the Controller assembly
  462. yield return typeof(Kernel).Assembly;
  463. // Include composable parts in the Providers assembly
  464. yield return typeof(ImagesByNameProvider).Assembly;
  465. // Common implementations
  466. yield return typeof(TaskManager).Assembly;
  467. // Server implementations
  468. yield return typeof(ServerApplicationPaths).Assembly;
  469. // Include composable parts in the running assembly
  470. yield return GetType().Assembly;
  471. }
  472. private readonly string _systemId = Environment.MachineName.GetMD5().ToString();
  473. /// <summary>
  474. /// Gets the system status.
  475. /// </summary>
  476. /// <returns>SystemInfo.</returns>
  477. public virtual SystemInfo GetSystemInfo()
  478. {
  479. return new SystemInfo
  480. {
  481. HasPendingRestart = HasPendingRestart,
  482. Version = ApplicationVersion.ToString(),
  483. IsNetworkDeployed = CanSelfUpdate,
  484. WebSocketPortNumber = ServerManager.WebSocketPortNumber,
  485. SupportsNativeWebSocket = ServerManager.SupportsNativeWebSocket,
  486. FailedPluginAssemblies = FailedAssemblies.ToArray(),
  487. InProgressInstallations = InstallationManager.CurrentInstallations.Select(i => i.Item1).ToArray(),
  488. CompletedInstallations = InstallationManager.CompletedInstallations.ToArray(),
  489. Id = _systemId,
  490. ProgramDataPath = ApplicationPaths.ProgramDataPath
  491. };
  492. }
  493. /// <summary>
  494. /// Shuts down.
  495. /// </summary>
  496. public override void Shutdown()
  497. {
  498. App.Instance.Dispatcher.Invoke(App.Instance.Shutdown);
  499. }
  500. /// <summary>
  501. /// Registers the server with administrator access.
  502. /// </summary>
  503. private void RegisterServerWithAdministratorAccess()
  504. {
  505. Logger.Info("Requesting administrative access to authorize http server");
  506. // Create a temp file path to extract the bat file to
  507. var tmpFile = Path.Combine(ConfigurationManager.CommonApplicationPaths.TempDirectory, Guid.NewGuid() + ".bat");
  508. // Extract the bat file
  509. using (var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MediaBrowser.ServerApplication.RegisterServer.bat"))
  510. {
  511. using (var fileStream = File.Create(tmpFile))
  512. {
  513. stream.CopyTo(fileStream);
  514. }
  515. }
  516. var startInfo = new ProcessStartInfo
  517. {
  518. FileName = tmpFile,
  519. Arguments = string.Format("{0} {1} {2} {3}", ServerConfigurationManager.Configuration.HttpServerPortNumber,
  520. HttpServerUrlPrefix,
  521. UdpServerPort,
  522. ServerConfigurationManager.Configuration.LegacyWebSocketPortNumber),
  523. CreateNoWindow = true,
  524. WindowStyle = ProcessWindowStyle.Hidden,
  525. Verb = "runas",
  526. ErrorDialog = false
  527. };
  528. using (var process = Process.Start(startInfo))
  529. {
  530. process.WaitForExit();
  531. }
  532. }
  533. public override async Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
  534. {
  535. var availablePackages = await InstallationManager.GetAvailablePackagesWithoutRegistrationInfo(CancellationToken.None).ConfigureAwait(false);
  536. var version = InstallationManager.GetLatestCompatibleVersion(availablePackages, Constants.MbServerPkgName, ConfigurationManager.CommonConfiguration.SystemUpdateLevel);
  537. return version != null ? new CheckForUpdateResult { AvailableVersion = version.version, IsUpdateAvailable = version.version > ApplicationVersion, Package = version } :
  538. new CheckForUpdateResult { AvailableVersion = ApplicationVersion, IsUpdateAvailable = false };
  539. }
  540. }
  541. }