DashboardService.cs 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788
  1. using MediaBrowser.Common.Extensions;
  2. using MediaBrowser.Common.IO;
  3. using MediaBrowser.Common.Net;
  4. using MediaBrowser.Controller;
  5. using MediaBrowser.Controller.Configuration;
  6. using MediaBrowser.Controller.Localization;
  7. using MediaBrowser.Controller.Net;
  8. using MediaBrowser.Controller.Plugins;
  9. using MediaBrowser.Model.Logging;
  10. using MediaBrowser.Model.Serialization;
  11. using ServiceStack;
  12. using ServiceStack.Web;
  13. using System;
  14. using System.Collections.Generic;
  15. using System.IO;
  16. using System.Linq;
  17. using System.Text;
  18. using System.Threading.Tasks;
  19. using WebMarkupMin.Core.Minifiers;
  20. using WebMarkupMin.Core.Settings;
  21. namespace MediaBrowser.WebDashboard.Api
  22. {
  23. /// <summary>
  24. /// Class GetDashboardConfigurationPages
  25. /// </summary>
  26. [Route("/dashboard/ConfigurationPages", "GET")]
  27. [Route("/web/ConfigurationPages", "GET")]
  28. public class GetDashboardConfigurationPages : IReturn<List<ConfigurationPageInfo>>
  29. {
  30. /// <summary>
  31. /// Gets or sets the type of the page.
  32. /// </summary>
  33. /// <value>The type of the page.</value>
  34. public ConfigurationPageType? PageType { get; set; }
  35. }
  36. /// <summary>
  37. /// Class GetDashboardConfigurationPage
  38. /// </summary>
  39. [Route("/dashboard/ConfigurationPage", "GET")]
  40. [Route("/web/ConfigurationPage", "GET")]
  41. public class GetDashboardConfigurationPage
  42. {
  43. /// <summary>
  44. /// Gets or sets the name.
  45. /// </summary>
  46. /// <value>The name.</value>
  47. public string Name { get; set; }
  48. }
  49. /// <summary>
  50. /// Class GetDashboardResource
  51. /// </summary>
  52. [Route("/web/{ResourceName*}", "GET")]
  53. [Route("/dashboard/{ResourceName*}", "GET")]
  54. public class GetDashboardResource
  55. {
  56. /// <summary>
  57. /// Gets or sets the name.
  58. /// </summary>
  59. /// <value>The name.</value>
  60. public string ResourceName { get; set; }
  61. /// <summary>
  62. /// Gets or sets the V.
  63. /// </summary>
  64. /// <value>The V.</value>
  65. public string V { get; set; }
  66. }
  67. /// <summary>
  68. /// Class DashboardService
  69. /// </summary>
  70. public class DashboardService : IRestfulService, IHasResultFactory
  71. {
  72. /// <summary>
  73. /// Gets or sets the logger.
  74. /// </summary>
  75. /// <value>The logger.</value>
  76. public ILogger Logger { get; set; }
  77. /// <summary>
  78. /// Gets or sets the HTTP result factory.
  79. /// </summary>
  80. /// <value>The HTTP result factory.</value>
  81. public IHttpResultFactory ResultFactory { get; set; }
  82. /// <summary>
  83. /// Gets or sets the request context.
  84. /// </summary>
  85. /// <value>The request context.</value>
  86. public IRequest Request { get; set; }
  87. /// <summary>
  88. /// The _app host
  89. /// </summary>
  90. private readonly IServerApplicationHost _appHost;
  91. /// <summary>
  92. /// The _server configuration manager
  93. /// </summary>
  94. private readonly IServerConfigurationManager _serverConfigurationManager;
  95. private readonly IFileSystem _fileSystem;
  96. private readonly ILocalizationManager _localization;
  97. private readonly IJsonSerializer _jsonSerializer;
  98. /// <summary>
  99. /// Initializes a new instance of the <see cref="DashboardService" /> class.
  100. /// </summary>
  101. /// <param name="appHost">The app host.</param>
  102. /// <param name="serverConfigurationManager">The server configuration manager.</param>
  103. /// <param name="fileSystem">The file system.</param>
  104. public DashboardService(IServerApplicationHost appHost, IServerConfigurationManager serverConfigurationManager, IFileSystem fileSystem, ILocalizationManager localization, IJsonSerializer jsonSerializer)
  105. {
  106. _appHost = appHost;
  107. _serverConfigurationManager = serverConfigurationManager;
  108. _fileSystem = fileSystem;
  109. _localization = localization;
  110. _jsonSerializer = jsonSerializer;
  111. }
  112. /// <summary>
  113. /// Gets the dashboard UI path.
  114. /// </summary>
  115. /// <value>The dashboard UI path.</value>
  116. public string DashboardUIPath
  117. {
  118. get
  119. {
  120. if (!string.IsNullOrEmpty(_serverConfigurationManager.Configuration.DashboardSourcePath))
  121. {
  122. return _serverConfigurationManager.Configuration.DashboardSourcePath;
  123. }
  124. var runningDirectory = Path.GetDirectoryName(_serverConfigurationManager.ApplicationPaths.ApplicationPath);
  125. return Path.Combine(runningDirectory, "dashboard-ui");
  126. }
  127. }
  128. /// <summary>
  129. /// Gets the dashboard resource path.
  130. /// </summary>
  131. /// <param name="virtualPath">The virtual path.</param>
  132. /// <returns>System.String.</returns>
  133. private string GetDashboardResourcePath(string virtualPath)
  134. {
  135. return Path.Combine(DashboardUIPath, virtualPath.Replace('/', Path.DirectorySeparatorChar));
  136. }
  137. /// <summary>
  138. /// Gets the specified request.
  139. /// </summary>
  140. /// <param name="request">The request.</param>
  141. /// <returns>System.Object.</returns>
  142. public object Get(GetDashboardConfigurationPage request)
  143. {
  144. var page = ServerEntryPoint.Instance.PluginConfigurationPages.First(p => p.Name.Equals(request.Name, StringComparison.OrdinalIgnoreCase));
  145. return ResultFactory.GetStaticResult(Request, page.Plugin.Version.ToString().GetMD5(), null, null, MimeTypes.GetMimeType("page.html"), () => ModifyHtml(page.GetHtmlStream(), null));
  146. }
  147. /// <summary>
  148. /// Gets the specified request.
  149. /// </summary>
  150. /// <param name="request">The request.</param>
  151. /// <returns>System.Object.</returns>
  152. public object Get(GetDashboardConfigurationPages request)
  153. {
  154. const string unavilableMessage = "The server is still loading. Please try again momentarily.";
  155. var instance = ServerEntryPoint.Instance;
  156. if (instance == null)
  157. {
  158. throw new InvalidOperationException(unavilableMessage);
  159. }
  160. var pages = instance.PluginConfigurationPages;
  161. if (pages == null)
  162. {
  163. throw new InvalidOperationException(unavilableMessage);
  164. }
  165. if (request.PageType.HasValue)
  166. {
  167. pages = pages.Where(p => p.ConfigurationPageType == request.PageType.Value);
  168. }
  169. // Don't allow a failing plugin to fail them all
  170. var configPages = pages.Select(p =>
  171. {
  172. try
  173. {
  174. return new ConfigurationPageInfo(p);
  175. }
  176. catch (Exception ex)
  177. {
  178. Logger.ErrorException("Error getting plugin information from {0}", ex, p.GetType().Name);
  179. return null;
  180. }
  181. })
  182. .Where(i => i != null)
  183. .ToList();
  184. return ResultFactory.GetOptimizedResult(Request, configPages);
  185. }
  186. /// <summary>
  187. /// Gets the specified request.
  188. /// </summary>
  189. /// <param name="request">The request.</param>
  190. /// <returns>System.Object.</returns>
  191. public object Get(GetDashboardResource request)
  192. {
  193. var path = request.ResourceName;
  194. var contentType = MimeTypes.GetMimeType(path);
  195. var isHtml = IsHtml(path);
  196. if (isHtml && !_serverConfigurationManager.Configuration.IsStartupWizardCompleted)
  197. {
  198. if (path.IndexOf("wizard", StringComparison.OrdinalIgnoreCase) == -1)
  199. {
  200. Request.Response.Redirect("wizardstart.html");
  201. return null;
  202. }
  203. }
  204. path = path.Replace("scripts/jquery.mobile-1.4.4.min.map", "thirdparty/jquerymobile-1.4.4/jquery.mobile-1.4.4.min.map", StringComparison.OrdinalIgnoreCase);
  205. var localizationCulture = GetLocalizationCulture();
  206. // Don't cache if not configured to do so
  207. // But always cache images to simulate production
  208. if (!_serverConfigurationManager.Configuration.EnableDashboardResponseCaching &&
  209. !contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) &&
  210. !contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase))
  211. {
  212. return ResultFactory.GetResult(GetResourceStream(path, isHtml, localizationCulture).Result, contentType);
  213. }
  214. TimeSpan? cacheDuration = null;
  215. // Cache images unconditionally - updates to image files will require new filename
  216. // If there's a version number in the query string we can cache this unconditionally
  217. if (contentType.StartsWith("image/", StringComparison.OrdinalIgnoreCase) || contentType.StartsWith("font/", StringComparison.OrdinalIgnoreCase) || !string.IsNullOrEmpty(request.V))
  218. {
  219. cacheDuration = TimeSpan.FromDays(365);
  220. }
  221. var assembly = GetType().Assembly.GetName();
  222. var cacheKey = (assembly.Version + (localizationCulture ?? string.Empty) + path).GetMD5();
  223. return ResultFactory.GetStaticResult(Request, cacheKey, null, cacheDuration, contentType, () => GetResourceStream(path, isHtml, localizationCulture));
  224. }
  225. private string GetLocalizationCulture()
  226. {
  227. return _serverConfigurationManager.Configuration.UICulture;
  228. }
  229. /// <summary>
  230. /// Gets the resource stream.
  231. /// </summary>
  232. /// <param name="path">The path.</param>
  233. /// <param name="isHtml">if set to <c>true</c> [is HTML].</param>
  234. /// <param name="localizationCulture">The localization culture.</param>
  235. /// <returns>Task{Stream}.</returns>
  236. private async Task<Stream> GetResourceStream(string path, bool isHtml, string localizationCulture)
  237. {
  238. Stream resourceStream;
  239. if (path.Equals("scripts/all.js", StringComparison.OrdinalIgnoreCase))
  240. {
  241. resourceStream = await GetAllJavascript().ConfigureAwait(false);
  242. }
  243. else if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase))
  244. {
  245. resourceStream = await GetAllCss().ConfigureAwait(false);
  246. }
  247. else
  248. {
  249. resourceStream = GetRawResourceStream(path);
  250. }
  251. if (resourceStream != null)
  252. {
  253. // Don't apply any caching for html pages
  254. // jQuery ajax doesn't seem to handle if-modified-since correctly
  255. if (isHtml)
  256. {
  257. resourceStream = await ModifyHtml(resourceStream, localizationCulture).ConfigureAwait(false);
  258. }
  259. }
  260. return resourceStream;
  261. }
  262. /// <summary>
  263. /// Gets the raw resource stream.
  264. /// </summary>
  265. /// <param name="path">The path.</param>
  266. /// <returns>Task{Stream}.</returns>
  267. private Stream GetRawResourceStream(string path)
  268. {
  269. return _fileSystem.GetFileStream(GetDashboardResourcePath(path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true);
  270. }
  271. /// <summary>
  272. /// Determines whether the specified path is HTML.
  273. /// </summary>
  274. /// <param name="path">The path.</param>
  275. /// <returns><c>true</c> if the specified path is HTML; otherwise, <c>false</c>.</returns>
  276. private bool IsHtml(string path)
  277. {
  278. return Path.GetExtension(path).EndsWith("html", StringComparison.OrdinalIgnoreCase);
  279. }
  280. /// <summary>
  281. /// Modifies the HTML by adding common meta tags, css and js.
  282. /// </summary>
  283. /// <param name="sourceStream">The source stream.</param>
  284. /// <param name="userId">The user identifier.</param>
  285. /// <param name="localizationCulture">The localization culture.</param>
  286. /// <returns>Task{Stream}.</returns>
  287. private async Task<Stream> ModifyHtml(Stream sourceStream, string localizationCulture)
  288. {
  289. using (sourceStream)
  290. {
  291. string html;
  292. using (var memoryStream = new MemoryStream())
  293. {
  294. await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  295. html = Encoding.UTF8.GetString(memoryStream.ToArray());
  296. if (!string.IsNullOrWhiteSpace(localizationCulture))
  297. {
  298. var lang = localizationCulture.Split('-').FirstOrDefault();
  299. html = _localization.LocalizeDocument(html, localizationCulture, GetLocalizationToken);
  300. html = html.Replace("<html>", "<html lang=\"" + lang + "\">");
  301. }
  302. //try
  303. //{
  304. // var minifier = new HtmlMinifier(new HtmlMinificationSettings(true));
  305. // html = minifier.Minify(html).MinifiedContent;
  306. //}
  307. //catch (Exception ex)
  308. //{
  309. // Logger.ErrorException("Error minifying html", ex);
  310. //}
  311. }
  312. var version = GetType().Assembly.GetName().Version;
  313. html = html.Replace("<head>", "<head>" + GetMetaTags() + GetCommonCss(version) + GetCommonJavascript(version));
  314. var bytes = Encoding.UTF8.GetBytes(html);
  315. return new MemoryStream(bytes);
  316. }
  317. }
  318. private string GetLocalizationToken(string phrase)
  319. {
  320. return "${" + phrase + "}";
  321. }
  322. /// <summary>
  323. /// Gets the meta tags.
  324. /// </summary>
  325. /// <returns>System.String.</returns>
  326. private static string GetMetaTags()
  327. {
  328. var sb = new StringBuilder();
  329. sb.Append("<meta http-equiv=\"X-UA-Compatibility\" content=\"IE=Edge\">");
  330. sb.Append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, user-scalable=no\">");
  331. //sb.Append("<meta name=\"apple-mobile-web-app-capable\" content=\"yes\">");
  332. sb.Append("<meta name=\"mobile-web-app-capable\" content=\"yes\">");
  333. sb.Append("<meta name=\"application-name\" content=\"Media Browser\">");
  334. //sb.Append("<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black-translucent\">");
  335. sb.Append("<link rel=\"icon\" sizes=\"114x114\" href=\"css/images/touchicon114.png\" />");
  336. // http://developer.apple.com/library/ios/#DOCUMENTATION/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
  337. sb.Append("<link rel=\"apple-touch-icon\" href=\"css/images/touchicon.png\" />");
  338. sb.Append("<link rel=\"apple-touch-icon\" sizes=\"72x72\" href=\"css/images/touchicon72.png\" />");
  339. sb.Append("<link rel=\"apple-touch-icon\" sizes=\"114x114\" href=\"css/images/touchicon114.png\" />");
  340. sb.Append("<link rel=\"apple-touch-startup-image\" href=\"css/images/iossplash.png\" />");
  341. sb.Append("<link rel=\"shortcut icon\" href=\"css/images/favicon.ico\" />");
  342. return sb.ToString();
  343. }
  344. /// <summary>
  345. /// Gets the common CSS.
  346. /// </summary>
  347. /// <param name="version">The version.</param>
  348. /// <returns>System.String.</returns>
  349. private static string GetCommonCss(Version version)
  350. {
  351. var versionString = "?v=" + version;
  352. var files = new[]
  353. {
  354. "thirdparty/jquerymobile-1.4.4/jquery.mobile-1.4.4.min.css",
  355. "thirdparty/swipebox-master/css/swipebox.min.css" + versionString,
  356. "css/all.css" + versionString
  357. };
  358. var tags = files.Select(s => string.Format("<link rel=\"stylesheet\" href=\"{0}\" />", s)).ToArray();
  359. return string.Join(string.Empty, tags);
  360. }
  361. /// <summary>
  362. /// Gets the common javascript.
  363. /// </summary>
  364. /// <param name="version">The version.</param>
  365. /// <returns>System.String.</returns>
  366. private static string GetCommonJavascript(Version version)
  367. {
  368. var builder = new StringBuilder();
  369. var versionString = "?v=" + version;
  370. var files = new[]
  371. {
  372. "scripts/all.js" + versionString,
  373. "thirdparty/jstree1.0/jquery.jstree.min.js",
  374. "thirdparty/swipebox-master/js/jquery.swipebox.min.js" + versionString
  375. };
  376. var tags = files.Select(s => string.Format("<script src=\"{0}\"></script>", s)).ToArray();
  377. builder.Append(string.Join(string.Empty, tags));
  378. return builder.ToString();
  379. }
  380. /// <summary>
  381. /// Gets a stream containing all concatenated javascript
  382. /// </summary>
  383. /// <returns>Task{Stream}.</returns>
  384. private async Task<Stream> GetAllJavascript()
  385. {
  386. var memoryStream = new MemoryStream();
  387. var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);
  388. // jQuery + jQuery mobile
  389. await AppendResource(memoryStream, "thirdparty/jquery-2.1.1.min.js", newLineBytes).ConfigureAwait(false);
  390. await AppendResource(memoryStream, "thirdparty/jquerymobile-1.4.4/jquery.mobile-1.4.4.min.js", newLineBytes).ConfigureAwait(false);
  391. await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false);
  392. // This script produces errors in older versions of safari
  393. if ((Request.UserAgent ?? string.Empty).IndexOf("chrome/", StringComparison.OrdinalIgnoreCase) != -1)
  394. {
  395. await AppendResource(memoryStream, "thirdparty/cast_sender.js", newLineBytes).ConfigureAwait(false);
  396. }
  397. await AppendLocalization(memoryStream).ConfigureAwait(false);
  398. await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
  399. // Write the version string for the dashboard comparison function
  400. var versionString = string.Format("window.dashboardVersion='{0}';", _appHost.ApplicationVersion);
  401. var versionBytes = Encoding.UTF8.GetBytes(versionString);
  402. await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);
  403. await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
  404. var builder = new StringBuilder();
  405. foreach (var file in new[]
  406. {
  407. "thirdparty/apiclient/sha1.js",
  408. "thirdparty/apiclient/mediabrowser.apiclient.js",
  409. "thirdparty/apiclient/connectionmanager.js"
  410. })
  411. {
  412. using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath(file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  413. {
  414. using (var streamReader = new StreamReader(fs))
  415. {
  416. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  417. builder.Append(text);
  418. builder.Append(Environment.NewLine);
  419. }
  420. }
  421. }
  422. foreach (var file in GetScriptFiles())
  423. {
  424. var path = GetDashboardResourcePath("scripts/" + file);
  425. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  426. {
  427. using (var streamReader = new StreamReader(fs))
  428. {
  429. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  430. builder.Append(text);
  431. builder.Append(Environment.NewLine);
  432. }
  433. }
  434. }
  435. var js = builder.ToString();
  436. try
  437. {
  438. var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);
  439. js = result.MinifiedContent;
  440. }
  441. catch (Exception ex)
  442. {
  443. Logger.ErrorException("Error minifying javascript", ex);
  444. }
  445. var bytes = Encoding.UTF8.GetBytes(js);
  446. await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  447. memoryStream.Position = 0;
  448. return memoryStream;
  449. }
  450. private IEnumerable<string> GetScriptFiles()
  451. {
  452. return new[]
  453. {
  454. "extensions.js",
  455. "site.js",
  456. "librarybrowser.js",
  457. "librarylist.js",
  458. "editorsidebar.js",
  459. "librarymenu.js",
  460. "mediacontroller.js",
  461. "chromecast.js",
  462. "backdrops.js",
  463. "sync.js",
  464. "playlistmanager.js",
  465. "mediaplayer.js",
  466. "mediaplayer-video.js",
  467. "nowplayingbar.js",
  468. "nowplayingpage.js",
  469. "ratingdialog.js",
  470. "aboutpage.js",
  471. "alphapicker.js",
  472. "addpluginpage.js",
  473. "advancedconfigurationpage.js",
  474. "metadataadvanced.js",
  475. "autoorganizetv.js",
  476. "autoorganizelog.js",
  477. "channels.js",
  478. "channelslatest.js",
  479. "channelitems.js",
  480. "channelsettings.js",
  481. "dashboardgeneral.js",
  482. "dashboardpage.js",
  483. "dashboardsync.js",
  484. "device.js",
  485. "devices.js",
  486. "devicesupload.js",
  487. "directorybrowser.js",
  488. "dlnaprofile.js",
  489. "dlnaprofiles.js",
  490. "dlnasettings.js",
  491. "dlnaserversettings.js",
  492. "editcollectionitems.js",
  493. "edititemmetadata.js",
  494. "edititemimages.js",
  495. "edititemsubtitles.js",
  496. "playbackconfiguration.js",
  497. "cinemamodeconfiguration.js",
  498. "encodingsettings.js",
  499. "externalplayer.js",
  500. "favorites.js",
  501. "gamesrecommendedpage.js",
  502. "gamesystemspage.js",
  503. "gamespage.js",
  504. "gamegenrepage.js",
  505. "gamestudiospage.js",
  506. "homelatest.js",
  507. "indexpage.js",
  508. "itembynamedetailpage.js",
  509. "itemdetailpage.js",
  510. "itemgallery.js",
  511. "itemlistpage.js",
  512. "librarypathmapping.js",
  513. "reports.js",
  514. "librarysettings.js",
  515. "livetvchannel.js",
  516. "livetvchannels.js",
  517. "livetvguide.js",
  518. "livetvnewrecording.js",
  519. "livetvprogram.js",
  520. "livetvrecording.js",
  521. "livetvrecordinglist.js",
  522. "livetvrecordings.js",
  523. "livetvtimer.js",
  524. "livetvseriestimer.js",
  525. "livetvseriestimers.js",
  526. "livetvsettings.js",
  527. "livetvsuggested.js",
  528. "livetvstatus.js",
  529. "livetvtimers.js",
  530. "loginpage.js",
  531. "logpage.js",
  532. "medialibrarypage.js",
  533. "metadataconfigurationpage.js",
  534. "metadataimagespage.js",
  535. "metadatasubtitles.js",
  536. "metadatakodi.js",
  537. "moviegenres.js",
  538. "moviecollections.js",
  539. "movies.js",
  540. "movieslatest.js",
  541. "moviepeople.js",
  542. "moviesrecommended.js",
  543. "moviestudios.js",
  544. "movietrailers.js",
  545. "musicalbums.js",
  546. "musicalbumartists.js",
  547. "musicartists.js",
  548. "musicgenres.js",
  549. "musicrecommended.js",
  550. "musicvideos.js",
  551. "mypreferencesdisplay.js",
  552. "mypreferenceslanguages.js",
  553. "mypreferenceswebclient.js",
  554. "notifications.js",
  555. "notificationlist.js",
  556. "notificationsetting.js",
  557. "notificationsettings.js",
  558. "playlist.js",
  559. "playlists.js",
  560. "playlistedit.js",
  561. "plugincatalogpage.js",
  562. "pluginspage.js",
  563. "remotecontrol.js",
  564. "scheduledtaskpage.js",
  565. "scheduledtaskspage.js",
  566. "search.js",
  567. "serversecurity.js",
  568. "songs.js",
  569. "supporterkeypage.js",
  570. "supporterpage.js",
  571. "episodes.js",
  572. "thememediaplayer.js",
  573. "tvgenres.js",
  574. "tvlatest.js",
  575. "tvpeople.js",
  576. "tvrecommended.js",
  577. "tvshows.js",
  578. "tvstudios.js",
  579. "tvupcoming.js",
  580. "useredit.js",
  581. "userpassword.js",
  582. "myprofile.js",
  583. "userprofilespage.js",
  584. "userparentalcontrol.js",
  585. "userlibraryaccess.js",
  586. "wizardfinishpage.js",
  587. "wizardservice.js",
  588. "wizardstartpage.js",
  589. "wizardsettings.js",
  590. "wizarduserpage.js"
  591. };
  592. }
  593. private async Task AppendLocalization(Stream stream)
  594. {
  595. var js = "window.localizationGlossary=" + _jsonSerializer.SerializeToString(_localization.GetJavaScriptLocalizationDictionary(GetLocalizationCulture()));
  596. var bytes = Encoding.UTF8.GetBytes(js);
  597. await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  598. }
  599. /// <summary>
  600. /// Gets all CSS.
  601. /// </summary>
  602. /// <returns>Task{Stream}.</returns>
  603. private async Task<Stream> GetAllCss()
  604. {
  605. var files = new[]
  606. {
  607. "site.css",
  608. "chromecast.css",
  609. "mediaplayer.css",
  610. "mediaplayer-video.css",
  611. "librarymenu.css",
  612. "librarybrowser.css",
  613. "detailtable.css",
  614. "card.css",
  615. "tileitem.css",
  616. "metadataeditor.css",
  617. "notifications.css",
  618. "search.css",
  619. "pluginupdates.css",
  620. "remotecontrol.css",
  621. "userimage.css",
  622. "livetv.css",
  623. "nowplaying.css",
  624. "icons.css"
  625. };
  626. var builder = new StringBuilder();
  627. foreach (var file in files)
  628. {
  629. var path = GetDashboardResourcePath("css/" + file);
  630. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  631. {
  632. using (var streamReader = new StreamReader(fs))
  633. {
  634. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  635. builder.Append(text);
  636. builder.Append(Environment.NewLine);
  637. }
  638. }
  639. }
  640. var css = builder.ToString();
  641. //try
  642. //{
  643. // var result = new KristensenCssMinifier().Minify(builder.ToString(), false, Encoding.UTF8);
  644. // css = result.MinifiedContent;
  645. //}
  646. //catch (Exception ex)
  647. //{
  648. // Logger.ErrorException("Error minifying css", ex);
  649. //}
  650. var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(css));
  651. memoryStream.Position = 0;
  652. return memoryStream;
  653. }
  654. /// <summary>
  655. /// Appends the resource.
  656. /// </summary>
  657. /// <param name="outputStream">The output stream.</param>
  658. /// <param name="path">The path.</param>
  659. /// <param name="newLineBytes">The new line bytes.</param>
  660. /// <returns>Task.</returns>
  661. private async Task AppendResource(Stream outputStream, string path, byte[] newLineBytes)
  662. {
  663. path = GetDashboardResourcePath(path);
  664. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  665. {
  666. using (var streamReader = new StreamReader(fs))
  667. {
  668. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  669. var bytes = Encoding.UTF8.GetBytes(text);
  670. await outputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  671. }
  672. }
  673. await outputStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
  674. }
  675. }
  676. }