MainWindow.xaml.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Configuration;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Net;
  7. using System.Threading.Tasks;
  8. using System.Windows;
  9. using System.Linq;
  10. using Ionic.Zip;
  11. using MediaBrowser.Installer.Code;
  12. using ServiceStack.Text;
  13. namespace MediaBrowser.Installer
  14. {
  15. /// <summary>
  16. /// Interaction logic for MainWindow.xaml
  17. /// </summary>
  18. public partial class MainWindow : Window
  19. {
  20. protected PackageVersionClass PackageClass = PackageVersionClass.Release;
  21. protected Version PackageVersion = new Version(4,0,0,0);
  22. protected string PackageName = "MBServer";
  23. protected string RootSuffix = "-Server";
  24. protected string TargetExe = "MediaBrowser.ServerApplication.exe";
  25. protected string FriendlyName = "Media Browser Server";
  26. protected string Archive = null;
  27. protected string RootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser-Server");
  28. protected bool SystemClosing = false;
  29. protected string TempLocation = Path.Combine(Path.GetTempPath(), "MediaBrowser");
  30. protected WebClient MainClient = new WebClient();
  31. public MainWindow()
  32. {
  33. GetArgs();
  34. InitializeComponent();
  35. DoInstall(Archive);
  36. }
  37. private void btnCancel_Click(object sender, RoutedEventArgs e)
  38. {
  39. this.Close();
  40. }
  41. protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
  42. {
  43. if (!SystemClosing && MessageBox.Show("Cancel Installation - Are you sure?", "Cancel", MessageBoxButton.YesNo) == MessageBoxResult.No)
  44. {
  45. e.Cancel = true;
  46. }
  47. if (MainClient.IsBusy)
  48. {
  49. MainClient.CancelAsync();
  50. while (MainClient.IsBusy)
  51. {
  52. // wait to finish
  53. }
  54. }
  55. MainClient.Dispose();
  56. ClearTempLocation(TempLocation);
  57. base.OnClosing(e);
  58. }
  59. protected void SystemClose(string message = null)
  60. {
  61. if (message != null)
  62. {
  63. MessageBox.Show(message, "Error");
  64. }
  65. SystemClosing = true;
  66. this.Close();
  67. }
  68. protected void GetArgs()
  69. {
  70. var product = ConfigurationManager.AppSettings["product"] ?? "server";
  71. PackageClass = (PackageVersionClass) Enum.Parse(typeof (PackageVersionClass), ConfigurationManager.AppSettings["class"] ?? "Release");
  72. var cmdArgs = Environment.GetCommandLineArgs();
  73. Archive = cmdArgs.Length > 1 ? cmdArgs[1] : null;
  74. var callerId = cmdArgs.Length > 2 ? cmdArgs[2] : null;
  75. if (callerId != null)
  76. {
  77. // Wait for our caller to exit
  78. try
  79. {
  80. var process = Process.GetProcessById(Convert.ToInt32(callerId));
  81. process.WaitForExit();
  82. }
  83. catch (ArgumentException)
  84. {
  85. // wasn't running
  86. }
  87. }
  88. switch (product.ToLower())
  89. {
  90. case "mbt":
  91. PackageName = "MBTheater";
  92. RootSuffix = "-UI";
  93. TargetExe = "MediaBrowser.UI.exe";
  94. FriendlyName = "Media Browser Theater";
  95. break;
  96. default:
  97. PackageName = "MBServer";
  98. RootSuffix = "-Server";
  99. TargetExe = "MediaBrowser.ServerApplication.exe";
  100. FriendlyName = "Media Browser Server";
  101. break;
  102. }
  103. RootPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData), "MediaBrowser" + RootSuffix);
  104. }
  105. /// <summary>
  106. /// Execute the install process
  107. /// </summary>
  108. /// <returns></returns>
  109. protected async Task DoInstall(string archive)
  110. {
  111. lblStatus.Text = string.Format("Installing {0}...", FriendlyName);
  112. // Determine Package version
  113. var version = await GetPackageVersion();
  114. // Now try and shut down the server if that is what we are installing and it is running
  115. var procs = Process.GetProcessesByName("mediabrowser.serverapplication");
  116. var server = procs.Length > 0 ? procs[0] : null;
  117. if (PackageName == "MBServer" && server != null)
  118. {
  119. lblStatus.Text = "Shutting Down Media Browser Server...";
  120. using (var client = new WebClient())
  121. {
  122. try
  123. {
  124. client.UploadString("http://localhost:8096/mediabrowser/System/Shutdown", "");
  125. try
  126. {
  127. server.WaitForExit();
  128. }
  129. catch (ArgumentException)
  130. {
  131. // already gone
  132. }
  133. }
  134. catch (WebException e)
  135. {
  136. if (e.GetStatus() == HttpStatusCode.NotFound || e.Message.StartsWith("Unable to connect",StringComparison.OrdinalIgnoreCase)) return; // just wasn't running
  137. MessageBox.Show("Error shutting down server. Please be sure it is not running before hitting OK.\n\n" + e.GetStatus() + "\n\n" + e.Message);
  138. }
  139. }
  140. }
  141. else
  142. {
  143. if (PackageName == "MBTheater")
  144. {
  145. // Uninstalling MBT - shut it down if it is running
  146. var processes = Process.GetProcessesByName("mediabrowser.ui");
  147. if (processes.Length > 0)
  148. {
  149. lblStatus.Text = "Shutting Down Media Browser Theater...";
  150. try
  151. {
  152. processes[0].Kill();
  153. }
  154. catch (Exception ex)
  155. {
  156. MessageBox.Show("Unable to shutdown Media Browser Theater. Please ensure it is not running before hitting OK.\n\n" + ex.Message, "Error");
  157. }
  158. }
  159. }
  160. }
  161. // Download if we don't already have it
  162. if (archive == null)
  163. {
  164. lblStatus.Text = string.Format("Downloading {0} (version {1})...", FriendlyName, version.versionStr);
  165. try
  166. {
  167. archive = await DownloadPackage(version);
  168. }
  169. catch (Exception e)
  170. {
  171. SystemClose("Error Downloading Package - " + e.GetType().FullName + "\n\n" + e.Message);
  172. }
  173. }
  174. if (archive == null) return; //we canceled or had an error that was already reported
  175. // Extract
  176. lblStatus.Text = "Extracting Package...";
  177. try
  178. {
  179. ExtractPackage(archive);
  180. }
  181. catch (Exception e)
  182. {
  183. SystemClose("Error Extracting - " + e.GetType().FullName + "\n\n" + e.Message);
  184. }
  185. // Create shortcut
  186. var fullPath = Path.Combine(RootPath, "System", TargetExe);
  187. try
  188. {
  189. CreateShortcuts(fullPath);
  190. }
  191. catch (Exception e)
  192. {
  193. SystemClose("Error Creating Shortcut - "+e.GetType().FullName+"\n\n"+e.Message);
  194. }
  195. // And run
  196. try
  197. {
  198. Process.Start(fullPath);
  199. }
  200. catch (Exception e)
  201. {
  202. SystemClose("Error Executing - "+fullPath+ " "+e.GetType().FullName+"\n\n"+e.Message);
  203. }
  204. SystemClose();
  205. }
  206. protected async Task<PackageVersionInfo> GetPackageVersion()
  207. {
  208. try
  209. {
  210. // get the package information for the server
  211. var json = await MainClient.DownloadStringTaskAsync("http://www.mb3admin.com/admin/service/package/retrieveAll?name=" + PackageName);
  212. var packages = JsonSerializer.DeserializeFromString<List<PackageInfo>>(json);
  213. var version = packages[0].versions.Where(v => v.classification <= PackageClass).OrderByDescending(v => v.version).FirstOrDefault(v => v.version <= PackageVersion);
  214. if (version == null)
  215. {
  216. SystemClose("Could not locate download package. Aborting.");
  217. return null;
  218. }
  219. return version;
  220. }
  221. catch (Exception e)
  222. {
  223. SystemClose(e.GetType().FullName + "\n\n" + e.Message);
  224. }
  225. return null;
  226. }
  227. /// <summary>
  228. /// Download our specified package to an archive in a temp location
  229. /// </summary>
  230. /// <returns>The fully qualified name of the downloaded package</returns>
  231. protected async Task<string> DownloadPackage(PackageVersionInfo version)
  232. {
  233. try
  234. {
  235. var archiveFile = Path.Combine(PrepareTempLocation(), version.targetFilename);
  236. // setup download progress and download the package
  237. MainClient.DownloadProgressChanged += DownloadProgressChanged;
  238. try
  239. {
  240. await MainClient.DownloadFileTaskAsync(version.sourceUrl, archiveFile);
  241. }
  242. catch (WebException e)
  243. {
  244. if (e.Status == WebExceptionStatus.RequestCanceled)
  245. {
  246. return null;
  247. }
  248. throw;
  249. }
  250. return archiveFile;
  251. }
  252. catch (Exception e)
  253. {
  254. SystemClose(e.GetType().FullName + "\n\n" + e.Message);
  255. }
  256. return "";
  257. }
  258. void DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
  259. {
  260. rectProgress.Width = (this.Width * e.ProgressPercentage)/100f;
  261. }
  262. /// <summary>
  263. /// Extract the provided archive to our program root
  264. /// It is assumed the archive is a zip file relative to that root (with all necessary sub-folders)
  265. /// </summary>
  266. /// <param name="archive"></param>
  267. protected void ExtractPackage(string archive)
  268. {
  269. // Delete old content of system
  270. var systemDir = Path.Combine(RootPath, "system");
  271. if (Directory.Exists(systemDir))
  272. {
  273. try
  274. {
  275. Directory.Delete(systemDir, true);
  276. }
  277. catch
  278. {
  279. // we tried...
  280. }
  281. }
  282. // And extract
  283. using (var fileStream = File.OpenRead(archive))
  284. {
  285. using (var zipFile = ZipFile.Read(fileStream))
  286. {
  287. zipFile.ExtractAll(RootPath, ExtractExistingFileAction.OverwriteSilently);
  288. }
  289. }
  290. }
  291. /// <summary>
  292. /// Create a shortcut in the current user's start menu
  293. /// Only do current user to avoid need for admin elevation
  294. /// </summary>
  295. /// <param name="targetExe"></param>
  296. protected void CreateShortcuts(string targetExe)
  297. {
  298. // get path to all users start menu
  299. var startMenu = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu),"Media Browser 3");
  300. if (!Directory.Exists(startMenu)) Directory.CreateDirectory(startMenu);
  301. var product = new ShellShortcut(Path.Combine(startMenu, FriendlyName+".lnk")) {Path = targetExe, Description = "Run " + FriendlyName};
  302. product.Save();
  303. if (PackageName == "MBServer")
  304. {
  305. var path = Path.Combine(startMenu, "MB Dashboard.lnk");
  306. var dashboard = new ShellShortcut(path)
  307. {Path = @"http://localhost:8096/mediabrowser/dashboard/dashboard.html", Description = "Open the Media Browser Server Dashboard (configuration)"};
  308. dashboard.Save();
  309. }
  310. var uninstall = new ShellShortcut(Path.Combine(startMenu, "Uninstall " + FriendlyName + ".lnk"))
  311. {Path = Path.Combine(Path.GetDirectoryName(targetExe), "MediaBrowser.Uninstaller.exe"), Arguments = (PackageName == "MBServer" ? "server" : "mbt"), Description = "Uninstall " + FriendlyName};
  312. uninstall.Save();
  313. }
  314. /// <summary>
  315. /// Prepare a temporary location to download to
  316. /// </summary>
  317. /// <returns>The path to the temporary location</returns>
  318. protected string PrepareTempLocation()
  319. {
  320. ClearTempLocation(TempLocation);
  321. Directory.CreateDirectory(TempLocation);
  322. return TempLocation;
  323. }
  324. /// <summary>
  325. /// Clear out (delete recursively) the supplied temp location
  326. /// </summary>
  327. /// <param name="location"></param>
  328. protected void ClearTempLocation(string location)
  329. {
  330. if (Directory.Exists(location))
  331. {
  332. Directory.Delete(location, true);
  333. }
  334. }
  335. }
  336. }