DashboardService.cs 27 KB

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