WindowsApp.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. using System;
  2. using MediaBrowser.Common.Net;
  3. using MediaBrowser.Model.Logging;
  4. using MediaBrowser.Server.Startup.Common;
  5. using MediaBrowser.ServerApplication.Networking;
  6. using System.Collections.Generic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Reflection;
  10. using CommonIO;
  11. using MediaBrowser.Controller.Power;
  12. using MediaBrowser.Server.Startup.Common.FFMpeg;
  13. using OperatingSystem = MediaBrowser.Server.Startup.Common.OperatingSystem;
  14. namespace MediaBrowser.ServerApplication.Native
  15. {
  16. public class WindowsApp : INativeApp
  17. {
  18. private readonly IFileSystem _fileSystem;
  19. private readonly ILogger _logger;
  20. public WindowsApp(IFileSystem fileSystem, ILogger logger)
  21. {
  22. _fileSystem = fileSystem;
  23. _logger = logger;
  24. }
  25. public List<Assembly> GetAssembliesWithParts()
  26. {
  27. var list = new List<Assembly>();
  28. if (!System.Environment.Is64BitProcess)
  29. {
  30. //list.Add(typeof(PismoIsoManager).Assembly);
  31. }
  32. list.Add(GetType().Assembly);
  33. return list;
  34. }
  35. public void AuthorizeServer(int udpPort, int httpServerPort, int httpsPort, string applicationPath, string tempDirectory)
  36. {
  37. ServerAuthorization.AuthorizeServer(udpPort, httpServerPort, httpsPort, applicationPath, tempDirectory);
  38. }
  39. public NativeEnvironment Environment
  40. {
  41. get
  42. {
  43. return new NativeEnvironment
  44. {
  45. OperatingSystem = OperatingSystem.Windows,
  46. SystemArchitecture = System.Environment.Is64BitOperatingSystem ? Architecture.X86_X64 : Architecture.X86,
  47. OperatingSystemVersionString = System.Environment.OSVersion.VersionString
  48. };
  49. }
  50. }
  51. public bool SupportsLibraryMonitor
  52. {
  53. get { return true; }
  54. }
  55. public bool SupportsRunningAsService
  56. {
  57. get
  58. {
  59. return true;
  60. }
  61. }
  62. public bool IsRunningAsService
  63. {
  64. get;
  65. set;
  66. }
  67. public bool CanSelfRestart
  68. {
  69. get
  70. {
  71. return MainStartup.CanSelfRestart;
  72. }
  73. }
  74. public bool SupportsAutoRunAtStartup
  75. {
  76. get
  77. {
  78. return true;
  79. }
  80. }
  81. public bool CanSelfUpdate
  82. {
  83. get
  84. {
  85. return MainStartup.CanSelfUpdate;
  86. }
  87. }
  88. public void Shutdown()
  89. {
  90. MainStartup.Shutdown();
  91. }
  92. public void Restart(StartupOptions startupOptions)
  93. {
  94. MainStartup.Restart();
  95. }
  96. public void ConfigureAutoRun(bool autorun)
  97. {
  98. var shortcutPath = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.StartMenu), "Emby", "Emby Server.lnk");
  99. var startupPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Startup);
  100. if (autorun)
  101. {
  102. //Copy our shortut into the startup folder for this user
  103. var targetPath = Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk");
  104. _fileSystem.CreateDirectory(Path.GetDirectoryName(targetPath));
  105. File.Copy(shortcutPath, targetPath, true);
  106. }
  107. else
  108. {
  109. //Remove our shortcut from the startup folder for this user
  110. _fileSystem.DeleteFile(Path.Combine(startupPath, Path.GetFileName(shortcutPath) ?? "Emby Server.lnk"));
  111. }
  112. }
  113. public INetworkManager CreateNetworkManager(ILogger logger)
  114. {
  115. return new NetworkManager(logger);
  116. }
  117. public void PreventSystemStandby()
  118. {
  119. Standby.PreventSystemStandby();
  120. }
  121. public IPowerManagement GetPowerManagement()
  122. {
  123. return new WindowsPowerManagement(_logger);
  124. }
  125. public FFMpegInstallInfo GetFfmpegInstallInfo()
  126. {
  127. var info = new FFMpegInstallInfo();
  128. info.FFMpegFilename = "ffmpeg.exe";
  129. info.FFProbeFilename = "ffprobe.exe";
  130. info.Version = "20160401";
  131. info.ArchiveType = "7z";
  132. info.IsEmbedded = true;
  133. info.DownloadUrls = GetDownloadUrls();
  134. return info;
  135. }
  136. public void LaunchUrl(string url)
  137. {
  138. var process = new Process
  139. {
  140. StartInfo = new ProcessStartInfo
  141. {
  142. FileName = url
  143. },
  144. EnableRaisingEvents = true,
  145. };
  146. process.Exited += ProcessExited;
  147. try
  148. {
  149. process.Start();
  150. }
  151. catch (Exception ex)
  152. {
  153. _logger.ErrorException("Error launching url: {0}", ex, url);
  154. throw;
  155. }
  156. }
  157. /// <summary>
  158. /// Processes the exited.
  159. /// </summary>
  160. /// <param name="sender">The sender.</param>
  161. /// <param name="e">The <see cref="EventArgs" /> instance containing the event data.</param>
  162. private static void ProcessExited(object sender, EventArgs e)
  163. {
  164. ((Process)sender).Dispose();
  165. }
  166. private string[] GetDownloadUrls()
  167. {
  168. switch (Environment.SystemArchitecture)
  169. {
  170. case Architecture.X86_X64:
  171. return new[] { "MediaBrowser.ServerApplication.ffmpeg.ffmpegx64.7z" };
  172. case Architecture.X86:
  173. return new[] { "MediaBrowser.ServerApplication.ffmpeg.ffmpegx86.7z" };
  174. }
  175. return new string[] { };
  176. }
  177. }
  178. }