PackageCreator.cs 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. using MediaBrowser.Common.IO;
  2. using MediaBrowser.Controller.Configuration;
  3. using MediaBrowser.Controller.Localization;
  4. using MediaBrowser.Model.Logging;
  5. using MediaBrowser.Model.Serialization;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. using WebMarkupMin.Core.Minifiers;
  13. using WebMarkupMin.Core.Settings;
  14. namespace MediaBrowser.WebDashboard.Api
  15. {
  16. public class PackageCreator
  17. {
  18. private readonly IFileSystem _fileSystem;
  19. private readonly ILocalizationManager _localization;
  20. private readonly ILogger _logger;
  21. private readonly IServerConfigurationManager _config;
  22. private readonly IJsonSerializer _jsonSerializer;
  23. public PackageCreator(IFileSystem fileSystem, ILocalizationManager localization, ILogger logger, IServerConfigurationManager config, IJsonSerializer jsonSerializer)
  24. {
  25. _fileSystem = fileSystem;
  26. _localization = localization;
  27. _logger = logger;
  28. _config = config;
  29. _jsonSerializer = jsonSerializer;
  30. }
  31. public async Task<Stream> GetResource(string path,
  32. string mode,
  33. string localizationCulture,
  34. string appVersion,
  35. bool enableMinification)
  36. {
  37. var isHtml = IsHtml(path);
  38. Stream resourceStream;
  39. if (path.Equals("scripts/all.js", StringComparison.OrdinalIgnoreCase))
  40. {
  41. resourceStream = await GetAllJavascript(mode, localizationCulture, appVersion, enableMinification).ConfigureAwait(false);
  42. }
  43. else if (path.Equals("css/all.css", StringComparison.OrdinalIgnoreCase))
  44. {
  45. resourceStream = await GetAllCss(enableMinification).ConfigureAwait(false);
  46. }
  47. else
  48. {
  49. resourceStream = GetRawResourceStream(path);
  50. }
  51. if (resourceStream != null)
  52. {
  53. // Don't apply any caching for html pages
  54. // jQuery ajax doesn't seem to handle if-modified-since correctly
  55. if (isHtml)
  56. {
  57. resourceStream = await ModifyHtml(resourceStream, mode, localizationCulture, enableMinification).ConfigureAwait(false);
  58. }
  59. }
  60. return resourceStream;
  61. }
  62. /// <summary>
  63. /// Determines whether the specified path is HTML.
  64. /// </summary>
  65. /// <param name="path">The path.</param>
  66. /// <returns><c>true</c> if the specified path is HTML; otherwise, <c>false</c>.</returns>
  67. private bool IsHtml(string path)
  68. {
  69. return Path.GetExtension(path).EndsWith("html", StringComparison.OrdinalIgnoreCase);
  70. }
  71. /// <summary>
  72. /// Gets the dashboard UI path.
  73. /// </summary>
  74. /// <value>The dashboard UI path.</value>
  75. public string DashboardUIPath
  76. {
  77. get
  78. {
  79. if (!string.IsNullOrEmpty(_config.Configuration.DashboardSourcePath))
  80. {
  81. return _config.Configuration.DashboardSourcePath;
  82. }
  83. return Path.Combine(_config.ApplicationPaths.ApplicationResourcesPath, "dashboard-ui");
  84. }
  85. }
  86. /// <summary>
  87. /// Gets the dashboard resource path.
  88. /// </summary>
  89. /// <param name="virtualPath">The virtual path.</param>
  90. /// <returns>System.String.</returns>
  91. private string GetDashboardResourcePath(string virtualPath)
  92. {
  93. return Path.Combine(DashboardUIPath, virtualPath.Replace('/', Path.DirectorySeparatorChar));
  94. }
  95. /// <summary>
  96. /// Modifies the HTML by adding common meta tags, css and js.
  97. /// </summary>
  98. /// <param name="sourceStream">The source stream.</param>
  99. /// <param name="mode">The mode.</param>
  100. /// <param name="localizationCulture">The localization culture.</param>
  101. /// <param name="enableMinification">if set to <c>true</c> [enable minification].</param>
  102. /// <returns>Task{Stream}.</returns>
  103. public async Task<Stream> ModifyHtml(Stream sourceStream, string mode, string localizationCulture, bool enableMinification)
  104. {
  105. using (sourceStream)
  106. {
  107. string html;
  108. using (var memoryStream = new MemoryStream())
  109. {
  110. await sourceStream.CopyToAsync(memoryStream).ConfigureAwait(false);
  111. html = Encoding.UTF8.GetString(memoryStream.ToArray());
  112. if (!string.IsNullOrWhiteSpace(localizationCulture))
  113. {
  114. var lang = localizationCulture.Split('-').FirstOrDefault();
  115. html = _localization.LocalizeDocument(html, localizationCulture, GetLocalizationToken);
  116. html = html.Replace("<html>", "<html lang=\"" + lang + "\">");
  117. }
  118. if (enableMinification)
  119. {
  120. try
  121. {
  122. var minifier = new HtmlMinifier(new HtmlMinificationSettings());
  123. var result = minifier.Minify(html, false);
  124. if (result.Errors.Count > 0)
  125. {
  126. _logger.Error("Error minifying html: " + result.Errors[0].Message);
  127. }
  128. else
  129. {
  130. html = result.MinifiedContent;
  131. }
  132. }
  133. catch (Exception ex)
  134. {
  135. _logger.ErrorException("Error minifying html", ex);
  136. }
  137. }
  138. }
  139. var version = GetType().Assembly.GetName().Version;
  140. html = html.Replace("<head>", "<head>" + GetMetaTags(mode) + GetCommonCss(mode, version) + GetCommonJavascript(mode, version));
  141. var bytes = Encoding.UTF8.GetBytes(html);
  142. return new MemoryStream(bytes);
  143. }
  144. }
  145. private string GetLocalizationToken(string phrase)
  146. {
  147. return "${" + phrase + "}";
  148. }
  149. /// <summary>
  150. /// Gets the meta tags.
  151. /// </summary>
  152. /// <returns>System.String.</returns>
  153. private static string GetMetaTags(string mode)
  154. {
  155. var sb = new StringBuilder();
  156. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  157. {
  158. sb.Append("<meta http-equiv=\"Content-Security-Policy\" content=\"default-src *; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline' 'unsafe-eval'\">");
  159. }
  160. sb.Append("<meta http-equiv=\"X-UA-Compatibility\" content=\"IE=Edge\">");
  161. sb.Append("<meta name=\"format-detection\" content=\"telephone=no\">");
  162. sb.Append("<meta name=\"msapplication-tap-highlight\" content=\"no\">");
  163. sb.Append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1, user-scalable=no\">");
  164. sb.Append("<meta name=\"apple-mobile-web-app-capable\" content=\"yes\">");
  165. sb.Append("<meta name=\"mobile-web-app-capable\" content=\"yes\">");
  166. sb.Append("<meta name=\"application-name\" content=\"Emby\">");
  167. //sb.Append("<meta name=\"apple-mobile-web-app-status-bar-style\" content=\"black-translucent\">");
  168. sb.Append("<meta name=\"robots\" content=\"noindex, nofollow, noarchive\" />");
  169. // http://developer.apple.com/library/ios/#DOCUMENTATION/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
  170. sb.Append("<link rel=\"apple-touch-icon\" href=\"css/images/touchicon.png\" />");
  171. sb.Append("<link rel=\"apple-touch-icon\" sizes=\"72x72\" href=\"css/images/touchicon72.png\" />");
  172. sb.Append("<link rel=\"apple-touch-icon\" sizes=\"114x114\" href=\"css/images/touchicon114.png\" />");
  173. sb.Append("<link rel=\"apple-touch-startup-image\" href=\"css/images/iossplash.png\" />");
  174. sb.Append("<link rel=\"shortcut icon\" href=\"css/images/favicon.ico\" />");
  175. sb.Append("<meta name=\"msapplication-TileImage\" content=\"css/images/touchicon144.png\">");
  176. sb.Append("<meta name=\"msapplication-TileColor\" content=\"#23456B\">");
  177. return sb.ToString();
  178. }
  179. /// <summary>
  180. /// Gets the common CSS.
  181. /// </summary>
  182. /// <param name="mode">The mode.</param>
  183. /// <param name="version">The version.</param>
  184. /// <returns>System.String.</returns>
  185. private string GetCommonCss(string mode, Version version)
  186. {
  187. var versionString = !string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase) ? "?v=" + version : string.Empty;
  188. var files = new[]
  189. {
  190. "thirdparty/jquerymobile-1.4.5/jquery.mobile-1.4.5.min.css",
  191. "thirdparty/swipebox-master/css/swipebox.min.css" + versionString,
  192. "thirdparty/fontawesome/css/font-awesome.min.css" + versionString,
  193. "thirdparty/jstree3.0.8/themes/default/style.min.css",
  194. "css/all.css" + versionString
  195. };
  196. var tags = files.Select(s => string.Format("<link rel=\"stylesheet\" href=\"{0}\" />", s)).ToArray();
  197. return string.Join(string.Empty, tags);
  198. }
  199. /// <summary>
  200. /// Gets the common javascript.
  201. /// </summary>
  202. /// <param name="mode">The mode.</param>
  203. /// <param name="version">The version.</param>
  204. /// <returns>System.String.</returns>
  205. private string GetCommonJavascript(string mode, Version version)
  206. {
  207. var builder = new StringBuilder();
  208. var versionString = !string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase) ? "?v=" + version : string.Empty;
  209. var files = new List<string>
  210. {
  211. "scripts/all.js" + versionString,
  212. "thirdparty/swipebox-master/js/jquery.swipebox.min.js" + versionString
  213. };
  214. if (string.Equals(mode, "cordova", StringComparison.OrdinalIgnoreCase))
  215. {
  216. files.Insert(0, "cordova.js");
  217. }
  218. var tags = files.Select(s => string.Format("<script src=\"{0}\"></script>", s)).ToArray();
  219. builder.Append(string.Join(string.Empty, tags));
  220. return builder.ToString();
  221. }
  222. /// <summary>
  223. /// Gets a stream containing all concatenated javascript
  224. /// </summary>
  225. /// <returns>Task{Stream}.</returns>
  226. private async Task<Stream> GetAllJavascript(string mode, string culture, string version, bool enableMinification)
  227. {
  228. var memoryStream = new MemoryStream();
  229. var newLineBytes = Encoding.UTF8.GetBytes(Environment.NewLine);
  230. // jQuery + jQuery mobile
  231. await AppendResource(memoryStream, "thirdparty/jquery-2.1.1.min.js", newLineBytes).ConfigureAwait(false);
  232. await AppendResource(memoryStream, "thirdparty/jquerymobile-1.4.5/jquery.mobile-1.4.5.min.js", newLineBytes).ConfigureAwait(false);
  233. await AppendResource(memoryStream, "thirdparty/jquery.unveil-custom.js", newLineBytes).ConfigureAwait(false);
  234. await AppendResource(memoryStream, "thirdparty/cast_sender.js", newLineBytes).ConfigureAwait(false);
  235. await AppendResource(memoryStream, "thirdparty/browser.js", newLineBytes).ConfigureAwait(false);
  236. await AppendResource(memoryStream, "thirdparty/jstree3.0.8/jstree.js", newLineBytes).ConfigureAwait(false);
  237. await AppendLocalization(memoryStream, culture).ConfigureAwait(false);
  238. await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
  239. if (!string.IsNullOrWhiteSpace(mode))
  240. {
  241. var appModeBytes = Encoding.UTF8.GetBytes(string.Format("window.appMode='{0}';", mode));
  242. await memoryStream.WriteAsync(appModeBytes, 0, appModeBytes.Length).ConfigureAwait(false);
  243. }
  244. // Write the version string for the dashboard comparison function
  245. var versionString = string.Format("window.dashboardVersion='{0}';", version);
  246. var versionBytes = Encoding.UTF8.GetBytes(versionString);
  247. await memoryStream.WriteAsync(versionBytes, 0, versionBytes.Length).ConfigureAwait(false);
  248. await memoryStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
  249. var builder = new StringBuilder();
  250. foreach (var file in new[]
  251. {
  252. "thirdparty/apiclient/logger.js",
  253. "thirdparty/apiclient/md5.js",
  254. "thirdparty/apiclient/sha1.js",
  255. "thirdparty/apiclient/store.js",
  256. "thirdparty/apiclient/network.js",
  257. "thirdparty/apiclient/device.js",
  258. "thirdparty/apiclient/credentials.js",
  259. "thirdparty/apiclient/ajax.js",
  260. "thirdparty/apiclient/events.js",
  261. "thirdparty/apiclient/deferred.js",
  262. "thirdparty/apiclient/apiclient.js",
  263. "thirdparty/apiclient/connectservice.js",
  264. "thirdparty/apiclient/serverdiscovery.js",
  265. "thirdparty/apiclient/connectionmanager.js"
  266. })
  267. {
  268. using (var fs = _fileSystem.GetFileStream(GetDashboardResourcePath(file), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  269. {
  270. using (var streamReader = new StreamReader(fs))
  271. {
  272. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  273. builder.Append(text);
  274. builder.Append(Environment.NewLine);
  275. }
  276. }
  277. }
  278. foreach (var file in GetScriptFiles())
  279. {
  280. var path = GetDashboardResourcePath("scripts/" + file);
  281. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  282. {
  283. using (var streamReader = new StreamReader(fs))
  284. {
  285. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  286. builder.Append(text);
  287. builder.Append(Environment.NewLine);
  288. }
  289. }
  290. }
  291. var js = builder.ToString();
  292. if (enableMinification)
  293. {
  294. try
  295. {
  296. var result = new CrockfordJsMinifier().Minify(js, false, Encoding.UTF8);
  297. if (result.Errors.Count > 0)
  298. {
  299. _logger.Error("Error minifying javascript: " + result.Errors[0].Message);
  300. }
  301. else
  302. {
  303. js = result.MinifiedContent;
  304. }
  305. }
  306. catch (Exception ex)
  307. {
  308. _logger.ErrorException("Error minifying javascript", ex);
  309. }
  310. }
  311. var bytes = Encoding.UTF8.GetBytes(js);
  312. await memoryStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  313. memoryStream.Position = 0;
  314. return memoryStream;
  315. }
  316. private IEnumerable<string> GetScriptFiles()
  317. {
  318. return new[]
  319. {
  320. "extensions.js",
  321. "site.js",
  322. "librarybrowser.js",
  323. "librarylist.js",
  324. "editorsidebar.js",
  325. "librarymenu.js",
  326. "mediacontroller.js",
  327. "chromecast.js",
  328. "backdrops.js",
  329. "sync.js",
  330. "syncjob.js",
  331. "appservices.js",
  332. "playlistmanager.js",
  333. "mediaplayer.js",
  334. "mediaplayer-video.js",
  335. "nowplayingbar.js",
  336. "nowplayingpage.js",
  337. "taskbutton.js",
  338. "ratingdialog.js",
  339. "aboutpage.js",
  340. "alphapicker.js",
  341. "addpluginpage.js",
  342. "advancedconfigurationpage.js",
  343. "metadataadvanced.js",
  344. "autoorganizetv.js",
  345. "autoorganizelog.js",
  346. "channels.js",
  347. "channelslatest.js",
  348. "channelitems.js",
  349. "channelsettings.js",
  350. "connectlogin.js",
  351. "dashboardgeneral.js",
  352. "dashboardhosting.js",
  353. "dashboardpage.js",
  354. "device.js",
  355. "devices.js",
  356. "devicesupload.js",
  357. "directorybrowser.js",
  358. "dlnaprofile.js",
  359. "dlnaprofiles.js",
  360. "dlnasettings.js",
  361. "dlnaserversettings.js",
  362. "editcollectionitems.js",
  363. "edititemmetadata.js",
  364. "edititemimages.js",
  365. "edititemsubtitles.js",
  366. "playbackconfiguration.js",
  367. "cinemamodeconfiguration.js",
  368. "encodingsettings.js",
  369. "externalplayer.js",
  370. "favorites.js",
  371. "forgotpassword.js",
  372. "forgotpasswordpin.js",
  373. "gamesrecommendedpage.js",
  374. "gamesystemspage.js",
  375. "gamespage.js",
  376. "gamegenrepage.js",
  377. "gamestudiospage.js",
  378. "homelatest.js",
  379. "indexpage.js",
  380. "itembynamedetailpage.js",
  381. "itemdetailpage.js",
  382. "itemlistpage.js",
  383. "kids.js",
  384. "librarypathmapping.js",
  385. "reports.js",
  386. "librarysettings.js",
  387. "livetvchannel.js",
  388. "livetvchannels.js",
  389. "livetvguide.js",
  390. "livetvitems.js",
  391. "livetvnewrecording.js",
  392. "livetvprogram.js",
  393. "livetvrecording.js",
  394. "livetvrecordinglist.js",
  395. "livetvrecordings.js",
  396. "livetvtimer.js",
  397. "livetvseriestimer.js",
  398. "livetvseriestimers.js",
  399. "livetvsettings.js",
  400. "livetvsuggested.js",
  401. "livetvstatus.js",
  402. "livetvtimers.js",
  403. "loginpage.js",
  404. "logpage.js",
  405. "medialibrarypage.js",
  406. "metadataconfigurationpage.js",
  407. "metadataimagespage.js",
  408. "metadatasubtitles.js",
  409. "metadatanfo.js",
  410. "moviegenres.js",
  411. "moviecollections.js",
  412. "movies.js",
  413. "moviepeople.js",
  414. "moviesrecommended.js",
  415. "moviestudios.js",
  416. "movietrailers.js",
  417. "musicalbums.js",
  418. "musicalbumartists.js",
  419. "musicartists.js",
  420. "musicgenres.js",
  421. "musicrecommended.js",
  422. "musicvideos.js",
  423. "mypreferencesdisplay.js",
  424. "mypreferenceslanguages.js",
  425. "mypreferenceswebclient.js",
  426. "notifications.js",
  427. "notificationlist.js",
  428. "notificationsetting.js",
  429. "notificationsettings.js",
  430. "photos.js",
  431. "playlists.js",
  432. "playlistedit.js",
  433. "plugincatalogpage.js",
  434. "pluginspage.js",
  435. "remotecontrol.js",
  436. "scheduledtaskpage.js",
  437. "scheduledtaskspage.js",
  438. "search.js",
  439. "selectserver.js",
  440. "serversecurity.js",
  441. "songs.js",
  442. "streamingsettings.js",
  443. "supporterkeypage.js",
  444. "supporterpage.js",
  445. "syncactivity.js",
  446. "syncsettings.js",
  447. "episodes.js",
  448. "thememediaplayer.js",
  449. "tvgenres.js",
  450. "tvlatest.js",
  451. "tvpeople.js",
  452. "tvrecommended.js",
  453. "tvshows.js",
  454. "tvstudios.js",
  455. "tvupcoming.js",
  456. "useredit.js",
  457. "usernew.js",
  458. "myprofile.js",
  459. "userpassword.js",
  460. "userprofilespage.js",
  461. "userparentalcontrol.js",
  462. "userlibraryaccess.js",
  463. "wizardagreement.js",
  464. "wizardfinishpage.js",
  465. "wizardservice.js",
  466. "wizardstartpage.js",
  467. "wizardsettings.js",
  468. "wizarduserpage.js"
  469. };
  470. }
  471. private async Task AppendLocalization(Stream stream, string culture)
  472. {
  473. var js = "window.localizationGlossary=" + _jsonSerializer.SerializeToString(_localization.GetJavaScriptLocalizationDictionary(culture));
  474. var bytes = Encoding.UTF8.GetBytes(js);
  475. await stream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  476. }
  477. /// <summary>
  478. /// Appends the resource.
  479. /// </summary>
  480. /// <param name="outputStream">The output stream.</param>
  481. /// <param name="path">The path.</param>
  482. /// <param name="newLineBytes">The new line bytes.</param>
  483. /// <returns>Task.</returns>
  484. private async Task AppendResource(Stream outputStream, string path, byte[] newLineBytes)
  485. {
  486. path = GetDashboardResourcePath(path);
  487. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  488. {
  489. using (var streamReader = new StreamReader(fs))
  490. {
  491. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  492. var bytes = Encoding.UTF8.GetBytes(text);
  493. await outputStream.WriteAsync(bytes, 0, bytes.Length).ConfigureAwait(false);
  494. }
  495. }
  496. await outputStream.WriteAsync(newLineBytes, 0, newLineBytes.Length).ConfigureAwait(false);
  497. }
  498. /// <summary>
  499. /// Gets all CSS.
  500. /// </summary>
  501. /// <returns>Task{Stream}.</returns>
  502. private async Task<Stream> GetAllCss(bool enableMinification)
  503. {
  504. var files = new[]
  505. {
  506. "site.css",
  507. "chromecast.css",
  508. "mediaplayer.css",
  509. "mediaplayer-video.css",
  510. "librarymenu.css",
  511. "librarybrowser.css",
  512. "detailtable.css",
  513. "card.css",
  514. "tileitem.css",
  515. "metadataeditor.css",
  516. "notifications.css",
  517. "search.css",
  518. "pluginupdates.css",
  519. "remotecontrol.css",
  520. "userimage.css",
  521. "livetv.css",
  522. "nowplaying.css",
  523. "icons.css",
  524. "materialize.css"
  525. };
  526. var builder = new StringBuilder();
  527. foreach (var file in files)
  528. {
  529. var path = GetDashboardResourcePath("css/" + file);
  530. using (var fs = _fileSystem.GetFileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true))
  531. {
  532. using (var streamReader = new StreamReader(fs))
  533. {
  534. var text = await streamReader.ReadToEndAsync().ConfigureAwait(false);
  535. builder.Append(text);
  536. builder.Append(Environment.NewLine);
  537. }
  538. }
  539. }
  540. var css = builder.ToString();
  541. if (enableMinification)
  542. {
  543. try
  544. {
  545. var result = new KristensenCssMinifier().Minify(builder.ToString(), false, Encoding.UTF8);
  546. if (result.Errors.Count > 0)
  547. {
  548. _logger.Error("Error minifying css: " + result.Errors[0].Message);
  549. }
  550. else
  551. {
  552. css = result.MinifiedContent;
  553. }
  554. }
  555. catch (Exception ex)
  556. {
  557. _logger.ErrorException("Error minifying css", ex);
  558. }
  559. }
  560. var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(css));
  561. memoryStream.Position = 0;
  562. return memoryStream;
  563. }
  564. /// <summary>
  565. /// Gets the raw resource stream.
  566. /// </summary>
  567. /// <param name="path">The path.</param>
  568. /// <returns>Task{Stream}.</returns>
  569. private Stream GetRawResourceStream(string path)
  570. {
  571. return _fileSystem.GetFileStream(GetDashboardResourcePath(path), FileMode.Open, FileAccess.Read, FileShare.ReadWrite, true);
  572. }
  573. }
  574. }