WindowsApp.cs 6.2 KB

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