ApplicationHost.cs 24 KB

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