DashboardService.cs 28 KB

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